1 |
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 |
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 { |
19 |
constructor(rootdir) { |
20 |
global.app = App.app = this; |
21 |
this.rootdir = rootdir; |
22 |
this.loadConfig(); |
23 |
this.env = process.env; |
24 |
|
25 |
this.client = new Client({ |
26 |
partials: ["CHANNEL"], |
27 |
intents: [ |
28 |
Intents.FLAGS.GUILDS, |
29 |
Intents.FLAGS.GUILD_MESSAGES, |
30 |
Intents.FLAGS.DIRECT_MESSAGES, |
31 |
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 |
|
40 |
this.config = new Config(); |
41 |
this.db = new Database(path.resolve(__dirname, '..', 'database.db')); |
42 |
this.commandManager = new CommandManager(path.resolve(__dirname, rootdir, "commands")); |
43 |
this.logger = new Logger(); |
44 |
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(); |
50 |
} |
51 |
|
52 |
boot() { |
53 |
const events = { |
54 |
MESSAGE_REACTION_ADD: 'messageReactionAdd', |
55 |
}; |
56 |
|
57 |
this.on('ready', () => { |
58 |
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) => { |
90 |
if (message.author.bot || !message.guild || message.channel.type == 'dm') { |
91 |
return; |
92 |
} |
93 |
|
94 |
await (this.msg = message); |
95 |
|
96 |
await this.spamFilter.start(message); |
97 |
await this.messageFilter.start(message, this.commandManager); |
98 |
|
99 |
await this.commandManager.setMessage(message); |
100 |
|
101 |
const valid = await this.commandManager.valid(); |
102 |
const has = await this.commandManager.has(); |
103 |
const snippet = await this.commandManager.snippet(); |
104 |
const allowed = await this.commandManager.verify(); |
105 |
|
106 |
if (valid && has && allowed) { |
107 |
await this.exec(); |
108 |
} |
109 |
else if (valid && snippet !== undefined) { |
110 |
await message.channel.send({ |
111 |
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) { |
120 |
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) => { |
131 |
if (oldMessage.author.bot || !oldMessage.guild || oldMessage.channel.type == 'dm' || oldMessage.content === newMessage.content) |
132 |
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); |
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) => { |
166 |
if (message.author.bot || !message.guild || message.channel.type == 'dm') |
167 |
return; |
168 |
|
169 |
await this.logger.logDelete(message); |
170 |
}); |
171 |
|
172 |
this.on("guildCreate", guild => { |
173 |
console.log("Joined a new guild: " + guild.name); |
174 |
|
175 |
this.config.props[guild.id] = { |
176 |
prefix: "-", |
177 |
debug: false, |
178 |
}; |
179 |
|
180 |
this.config.write(); |
181 |
}) |
182 |
|
183 |
this.on("guildDelete", guild => { |
184 |
console.log("Left a guild: " + guild.name); |
185 |
delete this.config.props[guild.id]; |
186 |
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() { |
201 |
if (fs.existsSync(path.join(__dirname, this.rootdir, '.env'))) { |
202 |
console.log("Loading .env file"); |
203 |
config(); |
204 |
} |
205 |
} |
206 |
|
207 |
on(event, handler) { |
208 |
this.client.on(event, handler); |
209 |
} |
210 |
|
211 |
exec() { |
212 |
this.commandManager.exec(); |
213 |
} |
214 |
|
215 |
run() { |
216 |
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; |