-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
542 lines (478 loc) · 22.8 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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
require('dotenv-flow').config();
const firebaseUtil = require('./db/firebase/firebaseUtil');
// const Commando = require('discord.js-commando');
const Discord = require('discord.js');
const winston = require('winston');
const fs = require('fs');
const discordServices = require('./discord-services');
const BotGuildModel = require('./classes/Bot/bot-guild');
const Verification = require('./classes/Bot/Features/Verification/verification');
const { StringPrompt } = require('advanced-discord.js-prompts');
const Sentry = require('@sentry/node');
const Tracing = require('@sentry/tracing');
const { LogLevel, SapphireClient } = require('@sapphire/framework');
const Pronouns = require('./commands/a_utility/pronouns');
const RoleSelector = require('./commands/a_utility/role-selector');
const StartReport = require('./commands/hacker_utility/start-report');
/**
* The Main App module houses the bot events, process events, and initializes
* the bot. It also handles new members and greets them.
* @module MainApp
*/
/**
* Returns the config settings depending on the command line args.
* Read command line args to know if prod, dev, or test and what server
* First arg is one of prod, dev or test
* the second is the test server, but the first one must be test
* @returns {Map} config settings
*/
function getConfig() {
if (process.env.NODE_ENV === 'DEV') {
// Default dev
return JSON.parse(process.env.DEV);
} else if (process.env.NODE_ENV === 'PROD') {
// Production
return JSON.parse(process.env.PROD);
} else if (process.env.NODE_ENV === 'TEST') {
// Test
const testConfig = JSON.parse(process.env.TEST);
let server = process.env.SERVER;
if (server === '1') {
return testConfig['ONE'];
} else if (server === '2') {
return testConfig['TWO'];
} else if (server === '3') {
return testConfig['THREE'];
} else if (server === '4') {
return testConfig['FOUR'];
}
}
// exit if no configs are loaded!
console.log('No configs were found for given args.');
process.exit(0);
}
const config = getConfig();
const isLogToConsole = config['consoleLog'];
if (config['sentryLog']) {
Sentry.init({
dsn: 'https://[email protected]/6062151',
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
}
const bot = new SapphireClient({
defaultPrefix: '!',
caseInsensitiveCommands: true,
logger: {
level: LogLevel.Debug
},
shards: 'auto',
intents: [
'GUILDS',
'GUILD_MEMBERS',
'GUILD_BANS',
'GUILD_EMOJIS_AND_STICKERS',
'GUILD_VOICE_STATES',
'GUILD_MESSAGES',
'GUILD_MESSAGE_REACTIONS',
'DIRECT_MESSAGES',
'DIRECT_MESSAGE_REACTIONS'
],
});
const customLoggerLevels = {
levels: {
error: 0,
warning: 1,
command: 2,
event: 3,
userStats: 4,
verbose: 5,
debug: 6,
silly: 7,
},
colors: {
error: 'red',
warning: 'yellow',
command: 'blue',
event: 'green',
userStats: 'magenta',
verbose: 'cyan',
debug: 'white',
silly: 'black',
}
};
// the main logger to use for general errors
const mainLogger = createALogger('main', 'main', true, isLogToConsole);
winston.addColors(customLoggerLevels.colors);
/**
* Register all the commands except for help and unknown since we have our own.
*/
// bot.registry
// .registerDefaultTypes()
// .registerGroup('a_boothing', 'boothing group for admins')
// .registerGroup('a_activity', 'activity group for admins')
// .registerGroup('a_start_commands', 'advanced admin commands')
// .registerGroup('a_utility', 'utility commands for admins')
// .registerGroup('hacker_utility', 'utility commands for users')
// .registerGroup('verification', 'verification commands')
// .registerGroup('attendance', 'attendance commands')
// .registerGroup('stamps', 'stamp related commands')
// .registerGroup('utility', 'utility commands')
// .registerGroup('essentials', 'essential commands for any guild', true)
// .registerDefaultGroups()
// .registerDefaultCommands({
// unknownCommand: false,
// help: false,
// })
// .registerCommandsIn(__dirname + '/commands');
/**
* Runs when the bot finishes the set up and is ready to work.
*/
bot.once('ready', async () => {
mainLogger.warning('The bot ' + bot.user.username + ' has started and is ready to hack!');
bot.user.setActivity('nwplus.github.io/Factotum');
// initialize firebase
const adminSDK = JSON.parse(process.env.NWPLUSADMINSDK);
firebaseUtil.initializeFirebaseAdmin('Factotum', adminSDK, process.env.FIREBASE_URL);
mainLogger.warning('Connected to nwFirebase successfully!', { event: 'Ready Event' });
firebaseUtil.connect('Factotum');
// make sure all guilds have a botGuild, this is in case the bot goes offline and its added
// to a guild. If botGuild is found, make sure only the correct commands are enabled.
const guildsArr = Array.from(bot.guilds.cache);
for (const [_, guild] of guildsArr) {
// create the logger for the guild
createALogger(guild.id, guild.name, false, isLogToConsole);
let botGuild = await firebaseUtil.getInitBotInfo(guild.id);
if (!botGuild) {
await newGuild(guild);
mainLogger.verbose(`Created a new botGuild for the guild ${guild.id} - ${guild.name} on bot ready.`, { event: 'Ready Event' });
} else {
// set all non guarded commands to not enabled for the guild
// bot.registry.groups.forEach((group, key, map) => {
// if (!group.guarded) guild.setGroupEnabled(group, false);
// });
// await botGuild.setCommandStatus(bot);
mainLogger.verbose(`Found a botGuild for ${guild.id} - ${guild.name} on bot ready.`, { event: 'Ready Event' });
if (botGuild.isSetUpComplete) {
mainLogger.verbose('Trying to restore existing pronoun command message');
/** @type {Pronouns} */
const pronounsCommand = bot.stores.get('commands').get('pronouns');
const pronounsError = await pronounsCommand.tryRestoreReactionListeners(guild);
if (pronounsError) {
mainLogger.warning(pronounsError);
} else {
mainLogger.verbose('Restored pronoun command message');
}
/** @type {StartVerification} */
const startVerificationCommand = bot.stores.get('commands').get('start-verification');
const verificationError = await startVerificationCommand.tryRestoreReactionListeners(guild);
if (verificationError) {
mainLogger.warning(verificationError);
} else {
mainLogger.verbose('Restored start verification command message');
}
/** @type {StartMentorCave} */
const mentorCaveCommand = bot.stores.get('commands').get('start-mentor-cave');
const mentorCaveError = await mentorCaveCommand.tryRestoreReactionListeners(guild);
if (mentorCaveError) {
mainLogger.warning(mentorCaveError);
} else {
mainLogger.verbose('Restored mentor cave command message');
}
/** @type {RoleSelector} */
const roleSelectorCommand = bot.stores.get('commands').get('role-selector');
const roleSelectorError = await roleSelectorCommand.tryRestoreReactionListeners(guild);
if (mentorCaveError) {
mainLogger.warning(roleSelectorError);
} else {
mainLogger.verbose('Restored role selector command message');
}
/** @type {StartReport} */
const startReportCommand = bot.stores.get('commands').get('start-report');
const startReportError = await startReportCommand.tryRestoreReactionListeners(guild);
if (startReportError) {
mainLogger.warning(startReportError);
} else {
mainLogger.verbose('Restored start report command message');
}
}
guild.commandPrefix = botGuild.prefix;
}
}
});
/**
* Runs when the bot is added to a guild.
*/
bot.on('guildCreate', /** @param {Discord.Guild} guild */(guild) => {
mainLogger.warning(`The bot was added to a new guild: ${guild.id} - ${guild.name}.`, { event: 'Guild Create Event' });
newGuild(guild);
// create a logger for this guild
createALogger(guild.id, guild.name);
});
/**
* Will set up a new guild.
* @param {Discord.Guild} guild
* @private
*/
async function newGuild(guild) {
// set all non guarded commands to not enabled for the new guild
// bot.registry.groups.forEach((group, key, map) => {
// if (!group.guarded) guild.setGroupEnabled(group, false);
// });
// create a botGuild object for this new guild.
await firebaseUtil.createInitBotInfoDoc(guild.id);
}
/**
* Runs when the bot is removed from a server.
*/
bot.on('guildDelete', async (guild) => {
mainLogger.warning(`The bot was removed from the guild: ${guild.id} - ${guild.name}`);
});
/**
* Runs when the bot runs into an error.
*/
bot.on('error', (error) => {
mainLogger.error(`Bot Error: ${error.name} - ${error.message}.`, { event: 'Error', data: error});
});
/**
* Runs when the bot runs into an error when running a command.
*/
bot.on('commandError', (command, error, message) => {
winston.loggers.get(message.channel?.guild?.id || 'main').error(`Command Error: In command ${command.name} got uncaught rejection ${error.name} : ${error.message}`, { event: 'Error', data: error});
});
/**
* Runs when a message is sent in any server the bot is running in.
*/
// bot.on('message', async message => {
// if (message?.guild) {
// let botGuild = await BotGuild.findById(message.guild.id);
// // Deletes all messages to any channel in the black list with the specified timeout
// // this is to make sure that if the message is for the bot, it is able to get it
// // bot and staff messages are not deleted
// if (botGuild.blackList.has(message.channel.id)) {
// if (!message.author.bot && !discordServices.checkForRole(message.member, botGuild.roleIDs.staffRole)) {
// winston.loggers.get(message.guild.id).verbose(`Deleting message from user ${message.author.id} due to being in the blacklisted channel ${message.channel.name}.`);
// (new Promise(res => setTimeout(res, botGuild.blackList.get(message.channel.id)))).then(() => discordServices.deleteMessage(message));
// }
// }
// }
// });
/**
* Runs when a new member joins a guild the bot is running in.
*/
bot.on('guildMemberAdd', async member => {
let botGuild = await firebaseUtil.getInitBotInfo(member.guild.id);
member.roles.add(botGuild.verification.guestRoleID);
// if the guild where the user joined is complete then greet and verify.
// also checks to make sure it does not greet bots
// if (botGuild.isSetUpComplete && !member.user.bot) {
// try {
// winston.loggers.get(member.guild.id).userStats('A new user joined the guild and is getting greeted!');
// await greetNewMember(member, botGuild);
// } catch (error) {
// await fixDMIssue(error, member, botGuild);
// }
// } else {
// winston.loggers.get(member.guild.id).warning('A new user joined the guild but was not greeted because the bot is not set up!');
// }
});
bot.on('commandRun', (command, promise, message, args) => {
winston.loggers.get(message?.guild?.id || 'main').command(`The command ${command.name} with args ${args} is being run from the channel ${message.channel} with id ${message.channel.id}
triggered by the message with id ${message.id} by the user with id ${message.author.id}`);
});
/**
* Runs when an unknown command is triggered.
*/
bot.on('unknownCommand', (message) => winston.loggers.get(message?.guild?.id || 'main').command(`An unknown command has been triggered in the channel ${message.channel.name} with id ${message.channel.id}. The message had the content ${message.cleanContent}.`));
/**
* Logs in the bot
*/
bot.login(config.token).catch(console.error);
/**
* Runs when the node process has an uncaught exception.
*/
process.on('uncaughtException', (error) => {
console.log(
'Uncaught Rejection, reason: ' + error.name +
'\nmessage: ' + error.message +
'\nfile: ' + error.fileName +
'\nline number: ' + error.lineNumber +
'\nstack: ' + error.stack
);
if (config['sentryLog']) {
Sentry.captureException(error);
}
});
/**
* Runs when the node process has an unhandled rejection.
*/
process.on('unhandledRejection', (error, promise) => {
console.log('Unhandled Rejection at:', promise,
'Unhandled Rejection, reason: ' + error.name +
'\nmessage: ' + error.message +
'\nfile: ' + error.fileName +
'\nline number: ' + error.lineNumber +
'\nstack: ' + error.stack
);
if (config['sentryLog']) {
Sentry.captureException(error);
}
});
/**
* Runs when the node process is about to exit and quit.
*/
process.on('exit', () => {
mainLogger.warning('Node is exiting!');
if (config['sentryLog']) {
Sentry.captureMessage('Node is exiting!');
}
});
/**
* Will create a default logger to use.
* @param {String} loggerName
* @param {String} [loggerLabel=''] - usually a more readable logger name
* @param {Boolean} [handleRejectionsExceptions=false] - will handle rejections and exceptions if true
* @param {Boolean} [LogToConsole=false] - will log all levels to console if true
* @returns {winston.Logger}
*/
function createALogger(loggerName, loggerLabel = '', handelRejectionsExceptions = false, logToConsole = false) {
// custom format
let format = winston.format.printf(info => `${info.timestamp} [${info.label}] ${info.level}${info?.event ? ' <' + info.event + '>' : ''} : ${info.message} ${info?.data ? 'DATA : ' + info.data : '' }`);
// create main logs directory if not present
if (!fs.existsSync('./logs')) fs.mkdirSync('./logs');
// create the directory if not present
if (!fs.existsSync(`./logs/${loggerName}`)) fs.mkdirSync(`./logs/${loggerName}`);
let logger = winston.loggers.add(loggerName, {
levels: customLoggerLevels.levels,
transports: [
new winston.transports.File({ filename: `./logs/${loggerName}/logs.log`, level: 'silly' }),
new winston.transports.File({ filename: `./logs/${loggerName}/debug.log`, level: 'debug' }),
new winston.transports.File({ filename: `./logs/${loggerName}/verbose.log`, level: 'verbose' }),
new winston.transports.File({ filename: `./logs/${loggerName}/userStats.log`, level: 'userStats' }),
new winston.transports.File({ filename: `./logs/${loggerName}/event.log`, level: 'event' }),
new winston.transports.File({ filename: `./logs/${loggerName}/command.log`, level: 'command' }),
new winston.transports.File({ filename: `./logs/${loggerName}/warning.log`, level: 'warning' }),
new winston.transports.File({ filename: `./logs/${loggerName}/error.log`, level: 'error', handleExceptions: handelRejectionsExceptions, handleRejections: handelRejectionsExceptions, }),
...(logToConsole ? [new winston.transports.Console({
level: 'silly',
format: winston.format.combine(
winston.format.colorize({ level: true }),
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.splat(),
winston.format.label({ label: loggerLabel}),
format,
),
handleExceptions: true,
handleRejections: true,
})] : []),
],
exitOnError: false,
format: winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.splat(),
winston.format.label({ label: loggerLabel}),
format,
)
});
return logger;
}
/**
* Greets a member!
* @param {Discord.GuildMember} member - the member to greet
* @param {BotGuildModel} botGuild
* @throws Error if the user has server DMs off
*/
async function greetNewMember(member, botGuild) {
let verifyEmoji = '🍀';
var embed = new Discord.MessageEmbed()
.setTitle(`Welcome to the ${member.guild.name} Server!`)
.setDescription('We are very excited to have you here!')
.addField('Have a question?', 'Visit the #welcome-support channel to talk with our staff!')
.addField('Want to learn more about what I can do?', 'Use the !help command anywhere and I will send you a message!')
.setColor(botGuild.colors.embedColor);
if (botGuild.verification.isEnabled) embed.addField('**VERIFY YOUR EMAIL** to get more access!', 'React to this message with ' + verifyEmoji + ' and follow my instructions!');
let msg = await member.send(embed);
// if verification is on then give guest role and let user verify
if (botGuild.verification.isEnabled) {
discordServices.addRoleToMember(member, botGuild.verification.guestRoleID);
let askedAboutCodex = false;
msg.react(verifyEmoji);
let verifyCollector = msg.createReactionCollector((reaction, user) => !user.bot && reaction.emoji.name === verifyEmoji);
verifyCollector.on('collect', async (reaction, user) => {
try {
var email = await StringPrompt.single({prompt: 'Please send me your email associated to this event!', channel: member.user.dmChannel, userId: member.id, time: 30, cancelable: true});
} catch (error) {
discordServices.sendEmbedToMember(member, {
title: 'Verification Error',
description: 'Email was not provided, please try again!'
}, true);
return;
}
try {
await Verification.verify(member, email, member.guild, botGuild);
if (!askedAboutCodex && await firebaseUtil.checkCodexActive(member.guild.id)
&& discordServices.checkForRole(member, botGuild.verification.verificationRoles.get('hacker'))) {
try {
discordServices.askBoolQuestion(member,botGuild, 'One more thing!',
'Would you like to receive free [Codex beta](https://openai.com/blog/openai-codex/) access, courtesy of our sponsor OpenAI (first come first served, while supplies last)?\n\n' +
'Open AI is giving out prizes to the best 2 projects using Codex or GPT-3:\n' +
'- 1st place: $120 worth of credits(2 million words in GPT-3 DaVinci)\n' +
'- 2nd place: $60 worth of credits (1 million words in GPT-3 DaVinci)\n\n' +
'If you would like a Codex code, please react with a 👍',
'Thanks for indicating your interest, you have been added to the list! If you are selected to receive an API key, you will get an email.', email);
askedAboutCodex = true;
} catch (error) {
discordServices.sendEmbedToMember(member, {
title: 'Oops, something went wrong',
description: 'Please contact an nwPlus member'
}, false);
}
}
} catch (error) {
discordServices.sendEmbedToMember(member, {
title: 'Verification Error',
description: 'Email provided is not valid! Please try again.'
}, true);
}
});
}
// if verification is off, then just give member role
else {
discordServices.addRoleToMember(member, botGuild.roleIDs.memberRole);
}
}
/**
* Will let the member know how to fix their DM issue.
* @param {Error} error - the error
* @param {Discord.GuildMember} member - the member with the error
* @param {BotGuildModel} botGuild
* @throws Error if the given error is not a DM error
*/
async function fixDMIssue(error, member, botGuild) {
if (error.code === 50007) {
let logger = winston.loggers.get(member.guild.id);
logger.warning(`A new user with id ${member.id} joined the guild but was not able to be greeted, we have asked him to fix the issues!`);
let channelID = botGuild.verification?.welcomeSupportChannelID || botGuild.channelIDs.botSupportChannel;
member.guild.channels.resolve(channelID).send('<@' + member.id + '> I couldn\'t reach you :(.' +
'\n* Please turn on server DMs, explained in this link: https://support.discord.com/hc/en-us/articles/217916488-Blocking-Privacy-Settings-' +
'\n* Once this is done, please react to this message with 🤖 to let me know!').then(msg => {
msg.react('🤖');
const collector = msg.createReactionCollector((reaction, user) => user.id === member.id && reaction.emoji.name === '🤖');
collector.on('collect', (reaction, user) => {
reaction.users.remove(user.id);
try {
greetNewMember(member, botGuild);
collector.stop();
msg.delete();
logger.userStats(`A user with id ${member.id} was able to fix the DM issue and was greeted!`);
} catch (error) {
member.guild.channels.resolve(channelID).send('<@' + member.id + '> Are you sure you made the changes? I couldn\'t reach you again 😕').then(msg => msg.delete({ timeout: 8000 }));
}
});
});
} else {
throw error;
}
}