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

Annotation of /trunk/src/App.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (hide annotations)
Mon Jul 29 17:28:17 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: text/javascript
File size: 6987 byte(s)
Added starboard feature
1 rakin 5 const { Client, Intents } = require("discord.js");
2     const { config } = require("dotenv");
3     const CommandManager = require("./CommandManager");
4     const path = require("path");
5     const fs = require("fs");
6     const Config = require("./Config");
7     const Database = require("./Database");
8     const Logger = require("./Logger");
9     const SpamFilter = require("./SpamFilter");
10 rakin 10 const server = require("./server");
11     const AntiRaid = require("./AntiRaid");
12 rakin 22 const MessageFilter = require("./MessageFilter");
13 rakin 23 const { random } = require("../commands/pixabay");
14 rakin 26 const AFKEngine = require("./AFKEngine");
15 rakin 33 const Starboard = require("./Starboard");
16 rakin 5
17     class App {
18     constructor(rootdir) {
19     global.app = App.app = this;
20     this.rootdir = rootdir;
21     this.loadConfig();
22 rakin 10 this.env = process.env;
23 rakin 5
24     this.client = new Client({
25     partials: ["CHANNEL"],
26     intents: [
27     Intents.FLAGS.GUILDS,
28     Intents.FLAGS.GUILD_MESSAGES,
29     Intents.FLAGS.DIRECT_MESSAGES,
30 rakin 10 Intents.FLAGS.DIRECT_MESSAGE_TYPING,
31     Intents.FLAGS.GUILD_PRESENCES,
32     Intents.FLAGS.GUILD_MEMBERS,
33 rakin 33 Intents.FLAGS.GUILD_BANS,
34     Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
35     Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
36 rakin 5 ]
37     });
38    
39     this.config = new Config();
40     this.db = new Database(path.resolve(__dirname, '..', 'database.db'));
41     this.commandManager = new CommandManager(path.resolve(__dirname, rootdir, "commands"));
42     this.logger = new Logger();
43     this.spamFilter = new SpamFilter();
44 rakin 10 this.antiRaid = new AntiRaid();
45 rakin 22 this.messageFilter = new MessageFilter();
46 rakin 26 this.afkEngine = new AFKEngine();
47 rakin 33 this.starboard = new Starboard();
48 rakin 5 this.boot();
49     }
50    
51     boot() {
52 rakin 33 const events = {
53     MESSAGE_REACTION_ADD: 'messageReactionAdd',
54     };
55    
56 rakin 5 this.on('ready', () => {
57     console.log("Logged in as " + this.client.user.tag);
58 rakin 23
59     this.client.user.setStatus(random(['dnd', 'idle']));
60     this.client.user.setActivity("over the server", { type: "WATCHING" });
61    
62 rakin 10 server();
63 rakin 5 });
64    
65 rakin 33 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 rakin 5 this.on('messageCreate', async (message) => {
85 rakin 11 if (message.author.bot || !message.guild || message.channel.type == 'dm') {
86 rakin 5 return;
87     }
88    
89     await (this.msg = message);
90    
91 rakin 20 await this.spamFilter.start(message);
92 rakin 22 await this.messageFilter.start(message, this.commandManager);
93 rakin 5
94     await this.commandManager.setMessage(message);
95    
96     const valid = await this.commandManager.valid();
97     const has = await this.commandManager.has();
98     const snippet = await this.commandManager.snippet();
99 rakin 11 const allowed = await this.commandManager.verify();
100 rakin 5
101 rakin 11 if (valid && has && allowed) {
102 rakin 5 await this.exec();
103     }
104     else if (valid && snippet !== undefined) {
105     await message.channel.send({
106 rakin 27 content: snippet.content,
107     files: snippet.files.map(f => {
108     return {
109     attachment: path.resolve(__dirname, '..', 'storage', f)
110     }
111     })
112 rakin 5 });
113     }
114     else if (valid && !has) {
115     await this.commandManager.notFound();
116     }
117 rakin 11 else if (valid && has && !allowed) {
118     await this.commandManager.notAllowed();
119     }
120 rakin 26 else if(!valid) {
121     await this.afkEngine.start(message);
122     }
123 rakin 5 });
124    
125     this.on("messageUpdate", async (oldMessage, newMessage) => {
126 rakin 25 if (oldMessage.author.bot || oldMessage.content === newMessage.content)
127 rakin 5 return;
128    
129     await this.logger.logEdit(oldMessage, newMessage);
130     });
131    
132 rakin 33 this.on("messageReactionAdd", async (reaction, message) => {
133     console.log('inside');
134 rakin 25
135 rakin 33 if (!reaction || !reaction.message) {
136     return;
137     }
138    
139     await (this.msg = reaction.message);
140     await this.starboard.handle(reaction, message);
141     });
142    
143 rakin 25 this.on('guildBanAdd', async (ban) => {
144     console.log('test');
145     await this.logger.logBanned(ban);
146     });
147    
148     this.on('guildBanRemove', async (ban) => {
149     console.log('test');
150     await this.logger.logUnbanned(ban);
151     });
152    
153 rakin 5 this.on("messageDelete", async (message) => {
154     if (message.author.bot)
155     return;
156    
157     await this.logger.logDelete(message);
158     });
159    
160     this.on("guildCreate", guild => {
161     console.log("Joined a new guild: " + guild.name);
162    
163     this.config.props[guild.id] = {
164     prefix: "-",
165     debug: false,
166     };
167    
168     this.config.write();
169     })
170    
171     this.on("guildDelete", guild => {
172     console.log("Left a guild: " + guild.name);
173     delete this.config.props[guild.id];
174     this.config.write();
175     })
176 rakin 10
177     this.on('guildMemberAdd', async (member) => {
178     console.log('Joined');
179     await this.antiRaid.start(member);
180 rakin 25 await this.logger.logJoined(member);
181 rakin 10 });
182 rakin 25
183     this.on('guildMemberRemove', async (member) => {
184     await this.logger.logLeft(member);
185     });
186 rakin 5 }
187    
188     loadConfig() {
189     if (fs.existsSync(path.join(__dirname, this.rootdir, '.env'))) {
190     console.log("Loading .env file");
191     config();
192     }
193     }
194    
195     on(event, handler) {
196     this.client.on(event, handler);
197     }
198    
199     exec() {
200     this.commandManager.exec();
201     }
202    
203     run() {
204     this.client.login(process.env.TOKEN);
205     }
206 rakin 10
207     tempFileCreate(name) {
208     const fullname = path.join(__dirname, '..', 'tmp', name);
209     const file = fs.createWriteStream(fullname);
210    
211     return {
212     name: fullname,
213     file
214     };
215     }
216 rakin 5 }
217    
218     module.exports = App;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26