-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
305 lines (269 loc) · 12.7 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Constants
const c = require("./config.json"), // Holds all sensitive tokens/passwords/etc.
fs = require("fs"),
Eris = require("eris"),
bot = new Eris(c.token, {
maxShards: c.shardCount,
getAllUsers: true,
disableEveryone: false,
defaultImageFormat: "png"
}),
s = require("snekfetch"),
cmdPrefix = c.prefix,
handler = require("recursive-readdir"), // Used to recursively get cmd files from folders
guildStatus = require("./util/guildInfo.js"),
modlogs = require("./util/modLogs.js"),
logger = require("./util/logger.js"); // Custom logger
let botInit = new Date(),
pref = cmdPrefix,
mentionPrefRegex = /<@(!)?403854965191344139>/;
/* Discord command definitions
/ Recursively goes through the commands folder and all sub folders to find command files
/ then pushes each file to the arrays below */
let commandLocations = [];
let commands = [];
if (c.debugMode) logger.verbose("Loading Commands")
handler("./commands/", (err, files) => {
for (i = 0; i < files.length; i++) {
if (files[i].endsWith(".js")) {
if (c.debugMode) logger.verbose(`Loaded command: ${files[i]}`)
let file = `./${files[i]}`
delete require.cache[require.resolve(file)];
commandLocations.push(file)
commands.push(require(file))
}
}
});
bot.on("ready", () => {
// Initial logs
let readyTime = new Date(),
startTime = Math.floor((readyTime - botInit) / 1000);
logger.success(` _____ |> Online.`);
logger.success(` |_ _|____ _ _ _ _ _ |> ${bot.guilds.size} servers connected`);
logger.success(` | ||_ / | | | | | | | | | |> ${bot.users.size} users`);
logger.success(` | | / /| |_| | |_| | |_| | |> ${bot.shards.size} shards connected`);
logger.success(` |_|/___|\\__,_|\\__, |\\__,_| |> ${commands.length} commands loaded`);
logger.success(` |___/ |> Took ${startTime} seconds to start`);
logger.success(`_______________________________________________________________________\n\n`);
// Setting bot status
bot.editStatus("online", {
name: c.playing,
type: 0
});
}); // End of ready event
bot.on("guildCreate", guild => {
logger.success(`Guild Joined! Now at ${bot.guilds.size}`);
guildStatus.update(bot, guild, true); // Sends guild join log to monitor channel
});
bot.on("guildDelete", guild => {
logger.verbose(`Guild Left. Now at ${bot.guilds.size}`);
guildStatus.update(bot, guild, false); // Sends guild leave log to monitor channel
});
bot.on("guildMemberAdd", async (guild, member) => {
if (c.debugMode) logger.verbose("Guild member Joined")
if (c.modLogs.enabled === true) {
modlogs.memberJoin(bot, guild, member, c.modLogs.channel); // Sends mod log to guild"s mod-log channel
}
if (c.welcomer.enabled === true) {
//welcomer.generate(bot, guild, member, c.welcomer.channel); // Generating welcome message for new users
}
if (c.autoRole.enabled === true) {
roleID = guild.roles.filter(r => r.name === c.autoRole.roleName)[0].id;
guild.addMemberRole(member.id, roleID).catch(e => {
return logger.error(e)
});
}
if (c.autoNick.enabled === true) {
if (c.debugMode) logger.verbose("Applying autonickname")
member.edit({
nick: c.autoNick.nickname
}).catch(e => {
return logger.error(e);
})
}
})
bot.on("guildMemberRemove", async (guild, member) => {
if (c.debugMode) logger.verbose("Guild Member Left")
if (c.modLogs.enabled === true) {
modlogs.memberLeave(bot, guild, member, c.modLogs.channel);
}
})
bot.on("messageReactionAdd", async (msg, emoji, adder) => {
if(c.downVoteDeletes.enabled === true) {
if(c.downVoteDeletes.channels.includes(msg.channel.id)) {
msg.channel.getMessageReaction(msg.id, "❌").then(r => {
if (r.length >= c.downVoteDeletes.limit) msg.delete()
})
}
}
})
bot.on("messageDelete", async msg => {
if (c.debugMode) logger.verbose("Message Deleted")
if (c.modLogs.enabled === true) {
modlogs.msgDeleteLog(bot, msg, c.modLogs.channel)
}
});
bot.on("messageUpdate", async (msg, oldMsg) => {
if (c.debugMode) logger.verbose("Message Update")
// Stop people from editing ads into their messages
if (c.adblock.enabled === true) {
if (msg.content.match(/\b(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite\/|gg)+\/*([A-Za-z_0-9]+)/g)) {
if (c.debugMode) logger.verbose(`Found Ad in edited message: ${msg.content}`)
let mod = msg.channel.permissionsOf(msg.author.id).has("banMembers");
let canDelete = msg.channel.permissionsOf(bot.user.id).has("manageMessages");
if (mod) return;
if (!canDelete) return;
msg.delete().then(m => {
msg.channel.createMessage(`<@${msg.author.id}> Please do not post invite links again.`)
})
}
}
if (c.linkBlocking.enabled === true) {
if (c.linkBlocking.channels.includes(msg.channel.id)) {
if (msg.content.match(/\bhttps?:\/\/\S+/i)) {
if (c.debugMode) logger.verbose(`Found Link in edited message: ${msg.content}`)
let manageMessages = msg.channel.permissionsOf(bot.user.id).has("manageMessages");
let moderator = msg.channel.permissionsOf(msg.author.id).has("banMembers")
if (moderator) return;
if (!manageMessages) return;
msg.delete().then(m => {
msg.channel.createMessage(`Sorry <@${msg.author.id}>, all links have been blocked in this channel by an administrator`)
})
}
}
}
});
bot.on("guildBanAdd", async (guild, user) => {
if (c.debugMode) logger.verbose("Guild Ban Added")
if (c.modLogs.enabled === true) {
modlogs.banAdd(bot, guild, user, c.modLogs.channel);
}
});
bot.on("guildBanRemove", async (guild, user) => {
if (c.debugMode) logger.verbose("Guild Ban Removed")
if (c.modLogs.enabled === true) {
modlogs.banRemove(bot, guild, user, c.modLogs.channel);
}
});
// Sets for command cooldowns
let masscmds = new Set();
let othercmds = new Set();
// Msg event
bot.on("messageCreate", async msg => {
if (c.debugMode) logger.verbose(`Message Sent: ${msg.content}`)
// Will not do anything if the author is a bot, or if the bot does not have perms to send messages
if (msg.author.bot) return;
let sendMessages = msg.channel.permissionsOf(bot.user.id).has("sendMessages");
let embedLinks = msg.channel.permissionsOf(bot.user.id).has("embedLinks");
if (!sendMessages) return;
if (!embedLinks) msg.channel.createMessage("This bot requires the `embedLinks`/`attachFiles` permissions in order for all of it's functions to work properly")
// Checking to see if channel is a poll channel
if (c.pollChannel.enabled === true && c.pollChannel.channel === msg.channel.id) {
if (c.debugMode) logger.verbose("PollChannel message sent")
msg.addReaction("✅")
msg.addReaction("❌")
if (c.debugMode) logger.verbose("Applied PollChannel reactions")
}
// Checking to see if channel has links blocked
if (c.linkBlocking.enabled === true) {
if (c.linkBlocking.channels.includes(msg.channel.id)) {
if (msg.content.match(/\bhttps?:\/\/\S+/i)) {
if (c.debugMode) logger.verbose(`Link Detected With LinkBlock: ${msg.content}`)
let manageMessages = msg.channel.permissionsOf(bot.user.id).has("manageMessages");
let moderator = msg.channel.permissionsOf(msg.author.id).has("banMembers")
if (moderator) return;
if (!manageMessages) return;
msg.delete().then(m => {
msg.channel.createMessage(`Sorry <@${msg.author.id}>, all links have been blocked in this channel by an administrator`)
})
}
}
}
// Checking to see if ads are blocked for that guild
if (c.adblock.enabled === true) {
if (msg.content.match(/\b(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite\/|gg)+\/*([A-Za-z_0-9]+)/g)) {
if (c.debugMode) logger.verbose(`Ad detected with adBlock: ${msg.content}`)
let mod = msg.channel.permissionsOf(msg.author.id).has("banMembers");
let canDelete = msg.channel.permissionsOf(bot.user.id).has("manageMessages");
if (mod) return;
if (!canDelete) return;
msg.delete().then(m => {
msg.channel.createMessage(`<@${msg.author.id}> Please do not post invite links again.`)
})
}
}
// Reload function for commands
if (msg.content === `${pref}rl` && c.devs.includes(msg.author.id)) {
if (c.debugMode) logger.verbose("Reloading Commands...")
commandLocations = [];
commands = [];
handler("./commands/", (err, files) => {
for (i = 0; i < files.length; i++) {
if (files[i].endsWith(".js")) {
let file = `./${files[i]}`
if (c.debugMode) logger.verbose(`Command Loaded: ${file[i]}`)
delete require.cache[require.resolve(file)];
commandLocations.push(file)
commands.push(require(file))
}
}
})
if (c.debugMode) logger.verbose("Commands Reloaded")
return msg.channel.createMessage("Reloaded Commands")
}
// Checking to see if the bot is mentioned
let mentionPref = pref
if (msg.content.match(mentionPrefRegex)) {
mentionPref = msg.content.match(mentionPrefRegex)[0]
}
// Command handling
if (msg.content.indexOf(pref) !== 0 && msg.content.indexOf(mentionPref) !== 0) return;
if (msg.content.startsWith(mentionPref)) {
executeCmd(mentionPref);
} else {
executeCmd(pref);
}
// Function used to actually execute commands
async function executeCmd(thePrefix) {
// Defining args and command
const args = msg.content.slice(thePrefix.length).trim().split(/ +/g);
const command = args.shift().toString().toLowerCase();
if (c.debugMode) logger.verbose(`Command Ran: ${command}`)
// Cycling through commands to find a match
for (i = 0; commands.length > i; i++) {
if (commands[i].command == command) {
// Checking for category-specific requirements
if (commands[i].category === "Developer" && !c.devs.includes(msg.author.id)) return msg.channel.createMessage("You must be a developer to use this command");
// Checking to see if the author and bot actually have permissions to execute this command effectively
let perms = msg.channel.permissionsOf(msg.author.id).has(commands[i].permission) && msg.channel.permissionsOf(bot.user.id).has(commands[i].botPermission);
if (!perms && !c.devs.includes(msg.author.id)) return msg.channel.createMessage(`You require the \`${commands[i].permission}\` permission, and the bot requires the \`${commands[i].botPermission}\` permission.`)
// if command is a mass command to add/remove/respond to cooldown. Mass cooldown is an hour
if (commands[i].command.startsWith("mass")) {
if (c.debugMode) logger.verbose("Mass Command Ran")
if (masscmds.has(msg.author.id)) {
cooldown = c.massCooldown
return msg.channel.createMessage("Please wait before running another `mass` command")
} else {
masscmds.add(msg.author.id);
setTimeout(() => masscmds.delete(msg.author.id), c.massCooldown);
}
} else { // Otherwise use the default cooldown of 3 second
if (c.debugMode) logger.verbose("Regular Command Ran")
if (othercmds.has(msg.author.id)) {
return msg.channel.createMessage("Please wait before running another command")
} else {
othercmds.add(msg.author.id);
setTimeout(() => othercmds.delete(msg.author.id), c.cooldown);
}
}
// Run the command
await commands[i].execute(bot, msg, args, commands, logger, c, s);
break;
}
}
}
});
bot.connect(); // Connect to Discord
// Catch uncaught errors
process.on("unhandledRejection", e => logger.uncaughtError(e))
process.on("uncaughtException", e => logger.uncaughtError(e))