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

Annotation of /trunk/src/App.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (hide annotations)
Mon Jul 29 17:28:16 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: text/javascript
File size: 5337 byte(s)
Added AFK engine
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 5
16     class App {
17     constructor(rootdir) {
18     global.app = App.app = this;
19     this.rootdir = rootdir;
20     this.loadConfig();
21 rakin 10 this.env = process.env;
22 rakin 5
23     this.client = new Client({
24     partials: ["CHANNEL"],
25     intents: [
26     Intents.FLAGS.GUILDS,
27     Intents.FLAGS.GUILD_MESSAGES,
28     Intents.FLAGS.DIRECT_MESSAGES,
29 rakin 10 Intents.FLAGS.DIRECT_MESSAGE_TYPING,
30     Intents.FLAGS.GUILD_PRESENCES,
31     Intents.FLAGS.GUILD_MEMBERS,
32 rakin 25 Intents.FLAGS.GUILD_BANS
33 rakin 5 ]
34     });
35    
36     this.config = new Config();
37     this.db = new Database(path.resolve(__dirname, '..', 'database.db'));
38     this.commandManager = new CommandManager(path.resolve(__dirname, rootdir, "commands"));
39     this.logger = new Logger();
40     this.spamFilter = new SpamFilter();
41 rakin 10 this.antiRaid = new AntiRaid();
42 rakin 22 this.messageFilter = new MessageFilter();
43 rakin 26 this.afkEngine = new AFKEngine();
44 rakin 5 this.boot();
45     }
46    
47     boot() {
48     this.on('ready', () => {
49     console.log("Logged in as " + this.client.user.tag);
50 rakin 23
51     this.client.user.setStatus(random(['dnd', 'idle']));
52     this.client.user.setActivity("over the server", { type: "WATCHING" });
53    
54 rakin 10 server();
55 rakin 5 });
56    
57     this.on('messageCreate', async (message) => {
58 rakin 11 if (message.author.bot || !message.guild || message.channel.type == 'dm') {
59 rakin 5 return;
60     }
61    
62     await (this.msg = message);
63    
64 rakin 20 await this.spamFilter.start(message);
65 rakin 22 await this.messageFilter.start(message, this.commandManager);
66 rakin 5
67     await this.commandManager.setMessage(message);
68    
69     const valid = await this.commandManager.valid();
70     const has = await this.commandManager.has();
71     const snippet = await this.commandManager.snippet();
72 rakin 11 const allowed = await this.commandManager.verify();
73 rakin 5
74 rakin 11 if (valid && has && allowed) {
75 rakin 5 await this.exec();
76     }
77     else if (valid && snippet !== undefined) {
78     await message.channel.send({
79     content: snippet.content
80     });
81     }
82     else if (valid && !has) {
83     await this.commandManager.notFound();
84     }
85 rakin 11 else if (valid && has && !allowed) {
86     await this.commandManager.notAllowed();
87     }
88 rakin 26 else if(!valid) {
89     await this.afkEngine.start(message);
90     }
91 rakin 5 });
92    
93     this.on("messageUpdate", async (oldMessage, newMessage) => {
94 rakin 25 if (oldMessage.author.bot || oldMessage.content === newMessage.content)
95 rakin 5 return;
96    
97     await this.logger.logEdit(oldMessage, newMessage);
98     });
99    
100 rakin 25
101     this.on('guildBanAdd', async (ban) => {
102     console.log('test');
103     await this.logger.logBanned(ban);
104     });
105    
106     this.on('guildBanRemove', async (ban) => {
107     console.log('test');
108     await this.logger.logUnbanned(ban);
109     });
110    
111 rakin 5 this.on("messageDelete", async (message) => {
112     if (message.author.bot)
113     return;
114    
115     await this.logger.logDelete(message);
116     });
117    
118     this.on("guildCreate", guild => {
119     console.log("Joined a new guild: " + guild.name);
120    
121     this.config.props[guild.id] = {
122     prefix: "-",
123     debug: false,
124     };
125    
126     this.config.write();
127     })
128    
129     this.on("guildDelete", guild => {
130     console.log("Left a guild: " + guild.name);
131     delete this.config.props[guild.id];
132     this.config.write();
133     })
134 rakin 10
135     this.on('guildMemberAdd', async (member) => {
136     console.log('Joined');
137     await this.antiRaid.start(member);
138 rakin 25 await this.logger.logJoined(member);
139 rakin 10 });
140 rakin 25
141     this.on('guildMemberRemove', async (member) => {
142     await this.logger.logLeft(member);
143     });
144 rakin 5 }
145    
146     loadConfig() {
147     if (fs.existsSync(path.join(__dirname, this.rootdir, '.env'))) {
148     console.log("Loading .env file");
149     config();
150     }
151     }
152    
153     on(event, handler) {
154     this.client.on(event, handler);
155     }
156    
157     exec() {
158     this.commandManager.exec();
159     }
160    
161     run() {
162     this.client.login(process.env.TOKEN);
163     }
164 rakin 10
165     tempFileCreate(name) {
166     const fullname = path.join(__dirname, '..', 'tmp', name);
167     const file = fs.createWriteStream(fullname);
168    
169     return {
170     name: fullname,
171     file
172     };
173     }
174 rakin 5 }
175    
176     module.exports = App;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26