aboutsummaryrefslogtreecommitdiff
path: root/commands/util/track.js
blob: 91b9e7f0c5aead334d980205f1c75552dac31a74 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const fs = require('fs')
const path = require("path");
let config_loc = __filename+".json"
const { PermissionsBitField } = require('discord.js');
let config = JSON.parse(fs.readFileSync(config_loc))
let db = require("../../src/db")
const {upload_limit} = require("../../src/util")
module.exports = {
    name : "track",
    command: ["track"],
    mod_only: false,
    config:config,
    config_loc:config_loc,
    async main (client,Discord,message,args){
        if((args[0]=="reg"||args[0]=="unreg")&&args.length < 1){
            return message.reply("please enter a word to (un)register")
        }
        let action = args[0];
        args.shift();
        this.exec(client,message,{action:action,other:args})
    },
    s_options:[{type:"sub",name:"opt-in",options:[]},
            {type:"sub",name:"opt-out",options:[]},
            {type:"sub",name:"del",options:[]},
            {type:"sub",name:"list",options:[]},
            {type:"sub",name:"reg",options:[{type:"string",name:"word",desc:"word to add",required:true,autocomplete:false}]},
            {type:"sub",name:"unreg",options:[{type:"string",name:"word",desc:"word to remove",required:true,autocomplete:
                async (message)=>{
                    let list = []
                    let d = (await db.Track.findAll({where:{user:message.author.id}}))[0]
                    if(d==null) return [];
                    for(let u of JSON.parse(d.words)) list.push(u.word);
                    return list;
                }
            }]},
            ],
    async s_main (client,Discord,interaction){
        let action = interaction.options.getSubcommand()
        let mess = interaction.options.getString("word")
        await this.exec(client,interaction,{action:action,other:[mess]})
        //await interaction.reply({ content:'sent', ephemeral: true })
        //interaction.deleteReply()
    },
    async exec(client,message,info){
        let words;
        let qu = {where:{user:message.author.id}}
        let entry = (await db.Track.findAll(qu))
        switch(info.action){
            case "opt-in":
                if(entry.length==0){
                    await db.Track.create({
                        user:message.author.id,
                        words:"[]",
                        track:true,
                    })
                    return message.reply("registered!")
                }
                
                await db.Track.update({track:true},qu);
                return message.reply("tracking enabled!")
                break;
            case "opt-out":
                if(entry.length==0){
                    return message.reply("you are not opted in")
                }
                
                await db.Track.update({track:false},qu);
                return message.reply("tracking disabled!")
                break;
            case "del":
                if(entry.length==0){
                    return message.reply("you are not opted in")
                }
                
                await db.Track.destroy(qu);
                return message.reply("entry deleted")
                break;
            case "reg":
                if(entry.length==0){
                    return message.reply("you are not opted in")
                }
                
                for(let o in info.other){
                    info.other[o] = {word:info.other[o].toLowerCase(),count:0}
                }

                words = JSON.parse(entry[0].words)
                await db.Track.update({words:JSON.stringify([...words,...info.other])},qu);
                return message.reply("added "+info.other.length+" words")
                break;
            case "unreg":
                if(entry.length==0){
                    return message.reply("you are not opted in")
                }
                
                words = JSON.parse(entry[0].words)
                let newwords = []
                let removed = 0;
                for(let w of words){
                    if(!info.other.includes(w.word.toLowerCase())) newwords.push(w);
                    else removed++;
                        
                }
                await db.Track.update({words:JSON.stringify(newwords)},qu);
                return message.reply("removed "+removed+" words"+(info.other.length>removed?", "+(info.other.length-removed)+" not found":""))
                break;
            case "list":
                if(entry.length==0){
                    return message.reply("you are not opted in")
                }
                let mmsg = "word  |  count\n-------------\n";
                for(let o of JSON.parse(entry[0].words)){
                    mmsg += o.word + " | " + o.count + "\n";
                }
                let filename = "/tmp/"+entry[0].user+".json"
                fs.writeFileSync(filename,mmsg)
                let stats = fs.statSync(filename)
                if(stats.size / (1024*1024) > upload_limit(message.guild))
                    return message.reply("file too large:( file is "+stats.size / (1024*1024)+"mb")
                return message.reply({files:[filename]})
                break;
        }
        //return info.id.send(info.echo)
    }
}