aboutsummaryrefslogtreecommitdiff
path: root/events/message.js
blob: 5c4d6acc4da8198c7acf41597a57d34798586f7e (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const fs = require('fs')
const path = require("path");
let db = require("../src/db");
const { parse_inp } = require("../src/util")
const { channel } = require('diagnostics_channel');
const { PermissionsBitField } = require('discord.js');
const { EmbedBuilder } = require("discord.js");
const util = require("../src/util")
let settings = require('../src/settings')
const llog = require("../src/logg")
let config_loc = __filename+".json"
module.exports = {
    name : "messageCreate",
    config_loc : config_loc,
    async main (client,Discord){
        let config = JSON.parse(fs.readFileSync(config_loc))
        client.on("messageCreate", async (message) => {
            llog.debug("message " + message.author.id)
            if(message.guild==null)
                return require("./dm").main(client,Discord,message)
            if(!settings["allowed-servers"].includes(message.guild.id)||message.author.bot||message.member==null)
                return;

            
            //spam messages
            /*for(let i = 0; i < global.recent_messages.length; i++){
                let diff = util.diff((new Date(message.createdTimestamp)).getUTCSeconds(),(new Date(global.recent_messages[i].createdTimestamp)).getUTCSeconds());

                if(diff>2){
                    global.recent_messages.splice(i,1);
                    i = -1;
                }
            }

            let matching_ids = [];
            for(let i = 0; i < global.recent_messages.length; i++){
                let m = global.recent_messages[i];
                if((m.content==message.content||util.similarity(m.content,message.content))&&m.author==message.author){
                    matching_ids.push(m);
                }
            }
            global.recent_messages.push(message)
            if(false && matching_ids.length > 3){
                //let temp_msg = global.recent_messages;
                global.recent_messages = global.recent_messages.filter(x => x.author != message.author)
                let could_timeout = true
                try{
                    await message.member.timeout(60000)
                } catch (e) {
                    could_timeout = false;
                }
                for(let mm of matching_ids){
                    //mm.delete();
                    mm.delete().then().catch();
                }
                
                let embed = new EmbedBuilder()
                    .setColor(settings.defaultColor)
                    .setTitle("Spam:(")
                    .setDescription("<@"+message.author.id+"> sent "+matching_ids.length+" messages, similar to or matching \n`"+message.content+"`")

                global.channels["admin-chan"].send({ embeds: [embed]})
            }*/
            //done w/ spam

            //track message
            let utrack = await db.Track.findAll({where:{user:message.author.id,track:true}})
            if(utrack.length!=0){
                utrack = utrack[0];
                let words = JSON.parse(utrack.words);
                for(let w of words){
                    if(message.content.toLowerCase().includes(w.word)) w.count+=message.content.split(w.word).length-1;
                }
                db.Track.update({words:JSON.stringify(words)},{where:{user:message.author.id,track:true}})
            }
            //done w/ track

            //handle sticky messages
            let stickies = await db.Sticky.findAll();
            for(let s of stickies){
                if(message.channel.id==s.channel && message.author.id!="762561860150362142"){
                    let m;
                    if(s.embed){
                        let embed = new EmbedBuilder()
                            .setDescription(s.message)
                            .setColor(s.embed_color)
                        m = await message.channel.send({embeds:[embed]})
                    } else {
                        m = await message.channel.send(s.message)
                    }
                    if(s.last_message!='null'){
                        try{
                            let msg = await message.channel.messages.fetch(s.last_message)
                            msg.delete()
                            
                        } catch(e){}
                    }
                    db.Sticky.update({last_message: m.id},{ where: {id: s.id}})
                }
                
            }
            //done w/ sticky

            //handle auto reactions
            let a_react = await db.Auto_React.findAll();
            for(let a of a_react){
                if(message.channel.id==a.channel && message.author.id!="762561860150362142"){
                    try{
                        let m = await message.react(a.emote)
                    } catch(e){}
                }
                
            }
            //done w/ auto reactions
            
            //deal with commands
            let remove = function(msg) {setTimeout(async()=>{try{await msg.delete()}catch(e){}},config["error-timeout"].value)}
            let date = new Date()
            let uid = message.member.id;
            let mod = util.is_mod(message.member);//message.member.permissions!=null&&message.member.permissions?.has(PermissionsBitField.Flags.KickMembers)
            if(message.content.startsWith("sns ")&&(!config["restrict-channels"].value.includes(message.channel.id)||(mod))){
                let com_string = message.content.split(" ")
                com_string.shift()
                let found = false;
                let com_call = com_string[0].trim()
                //keep this as a for loop incase we want to do sequential commands
                for(let com of global.commands){
                    if(com.command.includes(com_call)){
                        found = true;
                        if(com.mod_only&&!mod){
                            message.reply(config["error-message-auth"].value).then((msg)=>{remove(msg)})
                            break;
                        }
                        com_string.shift()
                        com_string = parse_inp(com_string.join(" "))
                        try{
                            if(((!com.config.restrict||com.config.restrict.length==0||com.config.restrict.includes(message.channel))&&
                                (!com.config.restricted||com.config.restricted.length==0||!com.config.restricted.includes(message.channel)))||mod)
                                if(mod||com.config.cooldown==null||com.last_command[uid]==null||(date.getTime()-com.last_command[uid].getTime())/1000>com.config.cooldown){
                                    await com.main(client,Discord,message,com_string,com_call)
                                    com.last_command[uid] = new Date()
                                } else message.reply("this command is on cooldown for "+((date.getTime()-com.last_command[uid].getTime())/1000).toFixed(2)+"/"+com.config.cooldown+"s").then((msg)=>{remove(msg)})
                            else
                                message.reply(config["error-message-not-here"].value).then((msg)=>{remove(msg)})
                        } catch(e) {
                            message.reply(config["error-message"].value + "\n```"+e.stack+"```").then((msg)=>{remove(msg)})
                            llog.error(e.stack)
                        }
                        break;
                    }
                }
                if(!found)
                    message.reply(config["error-message-not-found"].value).then((msg)=>{remove(msg)})
            }
            //done w/ commands
        })
    },
}