/[sudobot]/trunk/src/App.js
ViewVC logotype

Diff of /trunk/src/App.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 5 by rakin, Mon Jul 29 17:28:11 2024 UTC revision 45 by rakin, Mon Jul 29 17:28:20 2024 UTC
# Line 7  const Config = require("./Config"); Line 7  const Config = require("./Config");
7  const Database = require("./Database");  const Database = require("./Database");
8  const Logger = require("./Logger");  const Logger = require("./Logger");
9  const SpamFilter = require("./SpamFilter");  const SpamFilter = require("./SpamFilter");
10    const server = require("./server");
11    const AntiRaid = require("./AntiRaid");
12    const MessageFilter = require("./MessageFilter");
13    const { random } = require("../commands/pixabay");
14    const AFKEngine = require("./AFKEngine");
15    const Starboard = require("./Starboard");
16    const { runTimeouts, setTimeoutv2 } = require("./setTimeout");
17    
18  class App {  class App {
19      constructor(rootdir) {      constructor(rootdir) {
20          global.app = App.app = this;          global.app = App.app = this;
21          this.rootdir = rootdir;          this.rootdir = rootdir;
22          this.loadConfig();          this.loadConfig();
23            this.env = process.env;
24    
25          this.client = new Client({          this.client = new Client({
26              partials: ["CHANNEL"],              partials: ["CHANNEL"],
# Line 20  class App { Line 28  class App {
28                  Intents.FLAGS.GUILDS,                  Intents.FLAGS.GUILDS,
29                  Intents.FLAGS.GUILD_MESSAGES,                  Intents.FLAGS.GUILD_MESSAGES,
30                  Intents.FLAGS.DIRECT_MESSAGES,                  Intents.FLAGS.DIRECT_MESSAGES,
31                  Intents.FLAGS.DIRECT_MESSAGE_TYPING                  Intents.FLAGS.DIRECT_MESSAGE_TYPING,
32                    Intents.FLAGS.GUILD_PRESENCES,
33                    Intents.FLAGS.GUILD_MEMBERS,
34                    Intents.FLAGS.GUILD_BANS,
35                    Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
36                    Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
37              ]              ]
38          });          });
39    
# Line 29  class App { Line 42  class App {
42          this.commandManager = new CommandManager(path.resolve(__dirname, rootdir, "commands"));          this.commandManager = new CommandManager(path.resolve(__dirname, rootdir, "commands"));
43          this.logger = new Logger();          this.logger = new Logger();
44          this.spamFilter = new SpamFilter();          this.spamFilter = new SpamFilter();
45            this.antiRaid = new AntiRaid();
46            this.messageFilter = new MessageFilter();
47            this.afkEngine = new AFKEngine();
48            this.starboard = new Starboard();
49          this.boot();          this.boot();
50      }      }
51    
52      boot() {      boot() {
53            const events = {
54                MESSAGE_REACTION_ADD: 'messageReactionAdd',
55            };
56            
57          this.on('ready', () => {          this.on('ready', () => {
58              console.log("Logged in as " + this.client.user.tag);              console.log("Logged in as " + this.client.user.tag);
59    
60                this.client.user.setStatus(random(['dnd', 'idle']));
61                this.client.user.setActivity("over the server", { type: "WATCHING" });
62    
63                server();
64                
65                runTimeouts();
66    
67                // setTimeoutv2(path.resolve(__dirname, '../queues/send.js'), 10000, "Hello world");
68            });
69    
70            this.on('raw', async event => {
71                if (!events.hasOwnProperty(event.t))
72                    return;
73            
74                const { d: data } = event;
75                const user = this.client.users.cache.find(i => i.id === data.user_id);
76                const channel = this.client.channels.cache.find(i => i.id === data.channel_id) || await user.createDM();
77            
78                if (channel.messages.cache.has(data.message_id))
79                    return;
80            
81                const message = await channel.messages.fetch(data.message_id);
82            
83                const emojiKey = (data.emoji.id) ? `${data.emoji.name}:${data.emoji.id}` : data.emoji.name;
84                const reaction = message.reactions.cache.get(emojiKey);
85            
86                this.client.emit(events[event.t], reaction, user);
87          });          });
88    
89          this.on('messageCreate', async (message) => {          this.on('messageCreate', async (message) => {
90              if (message.author.bot) {              if (message.author.bot || !message.guild || message.channel.type == 'dm') {
91                  return;                  return;
92              }              }
93    
94              await (this.msg = message);              await (this.msg = message);
95                            
96              //await this.spamFilter.start(message);              await this.spamFilter.start(message);
97                await this.messageFilter.start(message, this.commandManager);
98    
99              await this.commandManager.setMessage(message);              await this.commandManager.setMessage(message);
100    
101              const valid = await this.commandManager.valid();              const valid = await this.commandManager.valid();
102              const has = await this.commandManager.has();              const has = await this.commandManager.has();
103              const snippet = await this.commandManager.snippet();              const snippet = await this.commandManager.snippet();
104                const allowed = await this.commandManager.verify();
105                            
106              if (valid && has) {              if (valid && has && allowed) {
107                  await this.exec();                  await this.exec();
108              }              }
109              else if (valid && snippet !== undefined) {              else if (valid && snippet !== undefined) {
110                  await message.channel.send({                  await message.channel.send({
111                      content: snippet.content                      content: snippet.content,
112                        files: snippet.files.map(f => {
113                            return {
114                                attachment: path.resolve(__dirname, '..', 'storage', f)
115                            }
116                        })
117                  });                  });
118              }              }
119              else if (valid && !has) {              else if (valid && !has) {
120                  await this.commandManager.notFound();                  await this.commandManager.notFound();
121              }              }
122                else if (valid && has && !allowed) {
123                    await this.commandManager.notAllowed();
124                }
125                else if(!valid) {
126                    await this.afkEngine.start(message);
127                }
128          });          });
129    
130          this.on("messageUpdate", async (oldMessage, newMessage) => {          this.on("messageUpdate", async (oldMessage, newMessage) => {
131              if (oldMessage.author.bot)              if (oldMessage.author.bot || !oldMessage.guild || oldMessage.channel.type == 'dm' || oldMessage.content === newMessage.content)
132                  return;                  return;
133    
134                let msg = await this.msg;
135                await (this.msg = newMessage);
136            
137                await this.spamFilter.basic(newMessage);
138                await this.messageFilter.start(newMessage, this.commandManager);
139    
140              await this.logger.logEdit(oldMessage, newMessage);              await this.logger.logEdit(oldMessage, newMessage);
141                await (this.msg = msg);
142            });
143    
144            this.on("messageReactionAdd", async (reaction, message) => {
145                console.log('inside');
146    
147                if (!reaction || !reaction.message || !message.guild || message.channel.type == 'dm') {
148                    return;
149                }
150                
151                await (this.msg = reaction.message);
152                await this.starboard.handle(reaction, message);
153            });
154    
155            this.on('guildBanAdd', async (ban) => {
156                console.log('test');
157                await this.logger.logBanned(ban);
158            });
159    
160            this.on('guildBanRemove', async (ban) => {
161                console.log('test');
162                await this.logger.logUnbanned(ban);
163          });          });
164    
165          this.on("messageDelete", async (message) => {          this.on("messageDelete", async (message) => {
166              if (message.author.bot)              if (message.author.bot || !message.guild || message.channel.type == 'dm')
167                  return;                  return;
168    
169              await this.logger.logDelete(message);              await this.logger.logDelete(message);
# Line 95  class App { Line 185  class App {
185              delete this.config.props[guild.id];              delete this.config.props[guild.id];
186              this.config.write();              this.config.write();
187          })          })
188    
189            this.on('guildMemberAdd', async (member) => {
190                console.log('Joined');
191                await this.antiRaid.start(member);
192                await this.logger.logJoined(member);
193            });
194    
195            this.on('guildMemberRemove', async (member) => {
196                await this.logger.logLeft(member);
197            });
198      }      }
199    
200      loadConfig() {      loadConfig() {
# Line 115  class App { Line 215  class App {
215      run() {      run() {
216          this.client.login(process.env.TOKEN);          this.client.login(process.env.TOKEN);
217      }      }
218    
219        tempFileCreate(name) {
220            const fullname = path.join(__dirname, '..', 'tmp', name);
221            const file = fs.createWriteStream(fullname);
222    
223            return {
224                name: fullname,
225                file
226            };
227        }
228  }  }
229    
230  module.exports = App;  module.exports = App;

Legend:
Removed from v.5  
changed lines
  Added in v.45

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26