aboutsummaryrefslogtreecommitdiff
path: root/commands/mod/search.js
blob: e81c3c80c3b94721f1cfdb0b34a6ec203999cb17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const Discord = require("discord.js")
const { EmbedBuilder } = require("discord.js");
const { PermissionsBitField } = require('discord.js');
const settings = require("../../src/settings")
const {similarity} = require("../../src/util")
const fs = require('fs')
let config_loc = __filename+".json"
let config = JSON.parse(fs.readFileSync(config_loc))
module.exports = {
  name: "search",
  command: ["search"],
  mod_only:true,
  config:config,
  config_loc:config_loc,
  async main(client,Discord,message,args) {
    let per = 60;
    for(let a of args){
        if(a.length>=2&&a[a.length-1]=="%"&&a[a.length-2]!="%"){
            a[a.length-1]=""
            per = parseInt(a)
            args.splice(args.indexOf(a), 1);
        }
    }
    let dec = per/100
    let mem = Object.fromEntries(await message.guild.members.fetch())
    let found = []
    let count = 0
    for(let k of Object.keys(mem)){
        let s = similarity(mem[k].user.username.toLowerCase(),args[0].toLowerCase())
        if(mem[k].nickname!=null){
            s=Math.max(s,similarity(mem[k].nickname.toLowerCase(),args[0].toLowerCase()))
        }
        if(s>dec){
            found.push({sim:s,id:mem[k].id})
            count++;
        }
    }
    if(count==0)
        return message.reply("no usernames/nicknames found (within a "+per+"% similarity)")
    for(let i = 0; i<count-1; i++){
        if(i<0)i=0
        if(found[i+1].sim>found[i].sim){
            let t = found[i+1]
            found[i+1] = found[i]
            found[i] = t 
            i-=2;
        }
    }
    let p_found = ""
    for(let i = 0; i!=count; i++){
        let new_m = "(<@"+found[i].id+"> @ " + Math.floor(found[i].sim * 100) + "%) "
        if((p_found+new_m).length>1024-3){
            p_found+="..."
            break;
        }
        p_found+=new_m
    }
    message.channel.send("loading ids").then(async(m)=>{
        await m.edit(p_found)
        m.delete()
    })
    let embed = new EmbedBuilder()
        .setTitle("Usernames close to '"+args[0]+"' (≥"+per+"%)")
        .setDescription(p_found)
        .setColor(settings.defaultColor)
        .setFooter({text:count+' user(s) found • Up to '+Math.floor(found[0].sim * 100) + '% similarity'})
    message.channel.send({embeds:[embed]})
    
  }
};