-
Notifications
You must be signed in to change notification settings - Fork 8
/
bot.js
executable file
·425 lines (331 loc) · 15.1 KB
/
bot.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const Discord = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { SlashCommandBuilder } = require('@discordjs/builders');
require('./logging');
////////////////////////////////////////////////////////////////////////////////
//////// CONFIG ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
const Store = require('data-store');
const store = new Store({ path: __dirname+'/CONFIG.json' });
const CONFIG = Object.assign({},store.get('config'));
CONFIG.botName = CONFIG.botName||'Discord Bot';
CONFIG.emojiTimeout = CONFIG.emojiTimeout||500;
CONFIG.debug = CONFIG.hasOwnProperty('debug')?CONFIG.debug:true;
CONFIG.logIncomingEvents = CONFIG.hasOwnProperty('logIncomingEvents')?CONFIG.logIncomingEvents:true;
CONFIG.exitOnUncaughtException = CONFIG.hasOwnProperty('exitOnUncaughtException')?CONFIG.exitOnUncaughtException:true;
global.CONFIG = CONFIG;
global.CONFIGSTORE = store;
//make sure token is configured
if (!CONFIG.token || CONFIG.token == 'PASTE YOUR TOKEN HERE' || CONFIG.token == '') {
console.log('toke',CONFIG.token)
try {
console.log(fs.readFileSync(__dirname+'/intro.txt','utf-8'));
} catch (err) {
console.log("\n Oof, we've got a problem. I think you dont have the right permissions to read things in this folder. Try running this command:\n chmod -R 755 .\nMore Info:\n\n");
console.error(err);
}
store.set('config.token', 'PASTE YOUR TOKEN HERE');
process.exit();
}
log('booting up...');
//const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'], intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_BANS,
Discord.Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Discord.Intents.FLAGS.GUILD_VOICE_STATES,
Discord.Intents.FLAGS.GUILD_MESSAGES,
Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Discord.Intents.FLAGS.GUILD_MEMBERS,
Discord.Intents.FLAGS.DIRECT_MESSAGES,
Discord.Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
]});
////////////////////////////////////////////////////////////////////////////////
//////// MODULE PROCESSING /////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var modules = [];
//when an event is triggered, search for a matching module and execute it
function checkModules (event, user, message, reaction, data) {
//ignore events from bots
if (user.bot) return;
console.log(event[0], message?message.channel.type:'');
//print messages to console
if (CONFIG.logIncomingEvents) {
if (event.includes('voice')) console.log(' ',event[0].toUpperCase(),user.username+':');
else if (event == 'dm') console.log(' ',event[0].toUpperCase(),user.username+':',reaction?reaction._emoji.name:message.content);
else if (message && message?.channel) console.log(' ',event[0].toUpperCase(),'#'+message.channel.name.toUpperCase(),user.username+':',reaction?reaction._emoji.name:message.content);
}
//loop through each defined module until a matching one is found
let foundMatch = false;
for (var i = 0; i < modules.length; i++) {
let module = modules[i];
//continue searching if any of the properties don't match
if (module.command) continue; //should be a slash command instead
if (module.event != event) continue; //wrong event
if (module.channel != '*' && module.channel != message.channel.id && !module.channel.includes(message.channel.id)) continue; //wrong channel
if (module.permissions && message.member && !message.member.permissions.has(module.permissions)) continue; //message was from a bot
if (module.pingBot && [...message.mentions.users.values()].filter(u => u.id == client.user.id) < 1) continue; //bot was not pinged
if (message && !module.filter.test(message.content)) continue; //filter mismatch
//rate limit
if (module.rateLimit) {
let lastTriggered = store.get('lastTriggered.'+module.name);
//if the command is over the rate limit
if (new Date() - new Date(lastTriggered) < 1000*60*module.rateLimit) {
if (typeof module.overLimit == 'function') module.overLimit(message, user);
continue;
}
}
console.log('\t\tEXECUTING MODULE:',module.name)
//execute module
try {
var result = module.func(message, user, reaction || data);
} catch (err) {
console.error(err);
log({module: module.name, error:err});
continue;
}
if (result == 'CONTINUE') continue;
store.set('lastTriggered.'+module.name, new Date());
if (!module.stopOnMatch) continue;
//stop looking for bot matches
foundMatch = true;
break;
}
//bot was pinged but not matched, react confused
if (!foundMatch && event=='message' && [...message.mentions.users.values()].filter(u => u.id == client.user.id) > 0) {
react(message,'hmm');
}
}
function checkSlashCommand(user, command, interaction) {
//console.log(interaction);
console.log('s', command);
//loop through modules to find matching command
for (let i = 0; i < modules.length; i++) {
let module = modules[i];
//command doesn't match, continue searching
if (!module.command) continue;
if (module.command.name !== command) continue;
//check if commands
if (module.channel != '*' && module.channel != interaction.channelId && !module.channel.includes(interaction.channelId)) continue; //wrong channel
//execute module
try {
module.func(interaction, user);
} catch (err) {
log({module: module.name, error:err});
continue;
}
}
}
class Module {
constructor (name, event, options, func) {
//defaults
this.name = name;
this.event = event;
this.options = {};
this.func = func;
//make sure required fields are there
if (!this.func || !this.event || !this.name) return log({module: this.name||'unnamed module', error: new Error('module missing required fields')});
//allow user to pass just a regex filter for options
if (options instanceof RegExp) options = {filter: options};
//set options
this.filter = options.filter || /.+/;
this.channel = options.channel || '*';
this.pingBot = options.pingBot || false;
this.stopOnMatch = options.stopOnMatch || true;
this.rateLimit = options.rateLimit;
this.overLimit = options.overLimit || function () {};
this.permissions = options.permissions;
this.command = options.command ? {
name: options.command,
type: options.commandType || 1,
description: options.description || this.command,
options: options.options || []
} : false;
if (options.default_member_permissions) this.command.default_member_permissions = options.default_member_permissions;
//show warning if the g flag was added to filter, as it breaks .test()
if (this.filter.flags.includes('g'))
log('\x1b[1m'+'\x1b[37m'+'['+this.name.toUpperCase()+']'+'\x1b[33m'+' WARNING:'+'\x1b[0m','including g flag on filters will most likely break things');
//keep track of when it was last triggered for ratelimits
this.lastTriggered = store.get('lastTriggered.'+this.name);
if (!this.lastTriggered) store.set('lastTriggered.'+this.name, new Date());
//add to array of modules
modules.push(this);
}
}
////////////////////////////////////////////////////////////////////////////////
//////// EVENT LISTENERS ///////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
client.on('messageCreate', (message) => {
if (message.channel.type == 'dm')
checkModules('dm',message.author, message);
else
checkModules('message',message.author, message);
});
client.on('messageReactionAdd', (reaction, user) => {
console.log('react')
checkModules('react',user,reaction.message,reaction);
});
client.on('messageReactionRemove', (reaction, user) => {
checkModules('unreact',user,reaction.message,reaction);
});
client.on('guildMemberAdd', (guildMember) => {
console.log('guildMemberAdd',guildMember.user);
checkModules('join',guildMember.user);
});
// slash commands
client.on('interactionCreate', (interaction, user) => {
if (!interaction.isCommand()) return;
checkSlashCommand(user,interaction.commandName,interaction);
});
// context menu right click app commands
client.on('interactionCreate', (interaction, user) => {
if (!interaction.isMessageContextMenu()) return;
checkSlashCommand(user,interaction.commandName,interaction);
});
// command chat input select dropdown command
client.on('interactionCreate', (interaction, user) => {
if (!interaction.isSelectMenu()) return;
console.log('selection menu',interaction.isSelectMenu(), interaction.customId, interaction.values, interaction);
checkSlashCommand(user,interaction.customId,interaction);
});
client.on('voiceStateUpdate', (oldState, newState) => {
//user left a channel
if (newState.channelID === null) checkModules('voiceLeave', newState.member.user, null, null, oldState);
//user joined a channel
else if (oldState.channelID === null) checkModules('voiceJoin', newState.member.user, null, null, newState);
//user moved from one channel to the other
else if (oldState.channelID !== newState.channelID) {
checkModules('voiceLeave', newState.member.user, null, null, oldState);
checkModules('voiceJoin', newState.member.user, null, null, newState);
}
});
////////////////////////////////////////////////////////////////////////////////
//////// UTILITY FUNCTIONS /////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//send a message
function send(message, text) {
message.channel.send(String(text));
}
//add a reaction to a message
function react(message, emojiArray) {
//if not an array, convert to an array
if (!Array.isArray(emojiArray)) emojiArray = [emojiArray];
//loop through each option, adding the emoji
for (var i = 0; i < emojiArray.length; i++) {
console.log('\tsending emoji',(i+1)+'/'+emojiArray.length,emojiArray[i]);
let e = emojiArray[i];
//do a timeout since doing them all at once makes them display in a random order
setTimeout(()=>{
//try to find an emoji with a matching name on the server
var matchedEmoji = message.guild.emojis.cache.find(emoji => emoji.name === e);
//emoji was found, send that
if (matchedEmoji)
message.react(matchedEmoji)
.catch(()=>{throw new Error('failed to react with '+matchedEmoji)});
//emoji not found (assume generic emoji and try to send)
else
message.react(e)
.catch(()=>{throw new Error('emoji '+matchedEmoji+' not found')});
}, CONFIG.emojiTimeout*i);
}
}
//send a single emoji message
function sendEmoji(message, emojiName) {
var emoji = message.guild.emojis.find(emoji => emoji.name === emojiName);
message.channel.send('<:'+emoji.name+':'+emoji.id+'>')
.catch(console.warn);
}
//pick a random item from an array
function pickRandom (optionsArray) {
return optionsArray[Math.floor(Math.random()*optionsArray.length)];
}
//make functions global so they're available in included modules
global.Module = Module;
global.send = send;
global.react = react;
global.sendEmoji = sendEmoji;
global.pickRandom = pickRandom;
global.client = client;
global.Discord = Discord;
////////////////////////////////////////////////////////////////////////////////
//////// STARTUP ///////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//load each module in modules folder
glob.sync('/modules/*.js', {root: __dirname}).forEach(filepath => {
let extension = path.extname(filepath);
let file = path.basename(filepath,extension);
//require patch, and catch any init errors so they can be printed to the console
try {
require(filepath);
}
catch (err) {
log({module: file, error:err});
}
log({module:file},'loaded');
});
//catches and logs error messages not caught by trycatch around require()
process.on('uncaughtException', function(err){
let filename = 'unknown file';
try {
let logLineDetails = ((err.stack).split("at ")[1]).trim();
let firstLine = logLineDetails = /\((.+):\d+:\d+\)/gi.exec(logLineDetails)[1];
let extension = path.extname(firstLine);
filename = path.basename(firstLine,extension);
} catch (e) {}
//log
log({module: filename, error:err});
console.log('\n\n This error was produced by an uncaught exeption, the bot may be in an usable state so it\'s shutting down. To keep the bot running, you can either set exitOnUncaughtException to false in the config (not reccomended), or you can use a process manager to restart the bot when it crashes (such as PM2).');
if (CONFIG.exitOnUncaughtException) process.exit();
});
//when bot is connected
client.once('ready', () => {
if (CONFIG.guildId && CONFIG.siteLogChannelId) client.guilds.cache.get(CONFIG.guildId).channels.cache.get(CONFIG.siteLogChannelId).send('Bot started');
//store guild info
global.guild = client.guilds.cache.first();
//if bot name hasn't been set yet, store it
if (!store.get('config').botName) {
store.set('config.botName',client.user.username);
log('set bots name to',client.user.username);
}
log('connected to',guild.name,'as',client.user.username);
loadSlashCommands(client.user.id);
});
////////////////////////////////////////////////////////////////////////////////
//////// COMMANDS //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
const rest = new REST({ version: '9' }).setToken(CONFIG.token);
async function loadSlashCommands (clientId) {
let commandsList = [];
try {
console.log('Started refreshing application (/) commands.');
//loop through modules
modules.forEach(module => {
if (!module.command) return;
if (typeof module.command.type == 'string') return;
if (module.command.name !== module.command.name.toLowerCase()) return log({module: module.name}, 'command "'+module.command.name+'" must be lowercase');
commandsList.push(module.command);
console.log('added command /'+module.command.name)
//add command
//global.guild.commands.create(module.commandOptions).then(e=>log('command added: /'+module.command));
});
//request commands update
//console.log('Command List:', commandsList)
//await rest.put(Routes.applicationCommands(clientId), {body: commandsList});
await rest.put(Routes.applicationGuildCommands(clientId,store.get('config.guildId')), {body: commandsList} );
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
//console.error('Raw Errors:', JSON.stringify(error?.rawError?.errors, null,2));
Object.keys(error?.rawError?.errors).forEach(commandIndex => {
console.error('Error with command:', commandsList[commandIndex]?.name, JSON.stringify(error?.rawError?.errors[commandIndex], null,2));
});
//if (error?.code == 50035) return console.log('There is one or more duplicate command names: ', commandsList.map(c=>c.name).filter(c => {return commandsList.map(c=>c.name).filter(x => x == c).length > 1}));
}
}
//log bot in
client.login(CONFIG.token);
/*global log, guild*/