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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26