-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
600 lines (516 loc) · 19.8 KB
/
index.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
require('dotenv').config()
const fetch = require('node-fetch')
const { Client, Intents, Permissions } = require('discord.js');
const webserver = require('./express.js')
const { users, url } = require('./db.js')
const config = require('./config.js')
const verification = require('./verification.js');
const whois = require('./whois.js');
const scratchWhois = require('./scratch-whois.js');
const banana = require('./banana.js')
const report = require('./report.js');
const massPing = require('./mass-ping.js')
const Agenda = require("agenda");
const errorHandle = require('./error-handle.js');
const warn = require('./warn.js');
const agenda = new Agenda({ db: { address: url } });
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
],
partials: ["CHANNEL"]
});
client.on('ready', async () => {
console.log(`Logged in as ${client.user.tag}! Setting up scheduler.`);
// initialize config
config.init();
// start webserver
webserver.start(client);
// setup adgenda
agenda.define("cleanup", async (job) => {
try {
console.log(`Running cleanup job`);
// a cleanup job to make sure that all verified users get their verified role and super active users get their super active role
console.log(`Cleaning up verified users`);
let allUsers = await users.find()
// get the guild
let guild = await client.guilds.cache.get(process.env.GUILD_ID);
// get the verified role
let verifiedRole = guild.roles.cache.get(process.env.VERIFIED_ROLE_ID);
let allMembersWithRole = verifiedRole.members
// remove the role if the user is not verified
for (let member of allMembersWithRole) {
let user = allUsers.find(u => u.discord == member[1].user.id)
if (!user) {
console.log(`Removing role from ${member[1].user.username}`);
await member[1].roles.remove(verifiedRole);
}
}
// add the role to everyone who needs it
for (let user of allUsers) {
// get the member
let [member, error] = await errorHandle(guild.members.fetch(user.discord));
if (!member || error) {
// console.log(`Could not find member ${user.discord}`);
} else {
// console.log(`Found member ${member.user.tag}`);
// assign the verified role to the user
await member.roles.add(verifiedRole);
console.log(`Added verified role to ${member.user.tag}`);
}
}
// super active role:
console.log('Begin super active role allocation')
let mee6Data = await fetch(`https://mee6.xyz/api/plugins/levels/leaderboard/${process.env.GUILD_ID}`).then(r=>r.json());
let activeRole = guild.roles.cache.get(process.env.ACTIVE_ROLE_ID);
let allMembersWithActiveRole = activeRole.members
const activeThreshold = config.settings.ACTIVE_THRESHOLD; // the top X users from the mee6 leaderboard
// remove the role if the user is not within the active threshold
let topUsers = mee6Data.players.slice(0, activeThreshold);
for (let member of allMembersWithActiveRole) {
// if the user is not in the leaderboard, remove the role
let found = topUsers.find(u => u.id == member[1].user.id)
if (!found) {
console.log(`Removing role from ${member[1].user.username}`);
await member[1].roles.remove(activeRole);
}
}
// add the role to everyone who needs it
for (let user of topUsers) {
// get the member
let [member, error] = await errorHandle(guild.members.fetch(user.id));
if (!member || error) {
// console.log(`Could not find member ${user.discord}`);
} else {
// console.log(`Found member ${member.user.tag}`);
// assign the verified role to the user
await member.roles.add(activeRole);
console.log(`Added active role to ${member.user.tag}`);
}
}
}
catch (err) {
console.log(err);
}
});
await agenda.start();
console.log(`Scheduler started.`);
await agenda.every("1 hour", "cleanup");
console.log(`Scheduled cleanup. Setting up slash commands.`);
// set up slash commands
let commands = await client.application.commands.set([
{
name: 'test',
description: 'A test command',
},
{
name: 'verify',
description: 'Link your Scratch username to your Discord account',
options: [
{
name: 'username',
type: 'STRING',
description: 'Your Scratch username',
required: true
}
]
},
{
name: 'whois',
description: '(admin) See linked Scratch account for a user (Output is visible to everyone)',
defaultPermission: false,
options: [
{
name: 'user',
type: 'USER',
description: 'user',
required: true
}
]
},
{
name: 'scratchwhois',
description: '(admin) See linked Discord accounts for a Scratch username (Output is visible to everyone)',
defaultPermission: false,
options: [
{
name: 'user',
type: 'STRING',
description: 'Scratch username',
required: true
}
]
},
{
name: 'bio',
description: 'Set a bio that appears on your ID card',
options: [
{
name: 'bio',
type: 'STRING',
description: 'New bio',
required: true
}
]
},
{
name: 'id',
description: 'Show your linked Scratch accounts',
},
{
name: 'adminremove',
defaultPermission: false,
description: '(admin) Remove a linked Scratch account for a Discord user',
options: [
{
name: 'discord',
type: 'USER',
description: 'user',
required: true
},
{
name: 'scratch',
type: 'STRING',
description: 'Scratch username',
required: true
}
]
},
{
name: 'adminadd',
defaultPermission: false,
description: '(admin) Bypass verification steps and linked Scratch account for a Discord user',
options: [
{
name: 'discord',
type: 'USER',
description: 'user',
required: true
},
{
name: 'scratch',
type: 'STRING',
description: 'Scratch username',
required: true
}
]
},
{
name: "apply",
description: 'Apply to become a moderator',
options: [
{
name: 'timezone',
type: 'STRING',
description: 'Your timezone',
required: true
},
{
name: 'age',
type: "INTEGER",
description: 'Your age',
required: true
},
{
name: "application",
type: "STRING",
description: 'Your full application',
required: true
}
]
},
{
name: "banana",
description: 'Image effects',
options: [
{
name: 'effect',
type: 'STRING',
description: 'Effect to apply',
choices: [
{
name: "banana",
value: "banana"
},
{
name: "gaming",
value: "gaming"
},
],
required: true
},
{
name: 'user',
type: 'USER',
description: 'Avatar',
required: false
}
]
},
{
name: "bean",
description: 'bean somebody',
options: [
{
name: 'user',
type: 'USER',
description: 'Who to bean',
required: true
}
]
},
{
name: "report",
description: 'Report something rule breaking',
options: [
{
name: 'title',
type: 'STRING',
description: "Title your report",
required: true
},
{
name: 'message',
type: 'STRING',
description: "Let us know what's wrong",
required: true
},
]
},
{
name: "warn",
defaultPermission: false,
description: '(admin) Warn a user for breaking the rules',
options: [
{
name: 'user',
type: 'USER',
description: 'user',
required: true
},
{
name: 'message',
type: 'STRING',
description: "What did they do this time",
required: true
},
]
},
{
name: "infractions",
defaultPermission: false,
description: '(admin) List warns given to a user',
options: [
{
name: 'user',
type: 'USER',
description: 'user',
required: true
}
]
}
], process.env.GUILD_ID)
console.log(`Slash commands set up. Now setting permissions.`);
// loop over the commands and for each that contains admin in the description, set the permissions to require the moderator role
/* for (let command of commands) {
console.log(`Setting permissions for ${command[1].name}`);
if (command[1].description.includes("admin")) {
let permissions = [
{
id: process.env.MODERATOR_ROLE_ID,
type: 'ROLE',
permission: true,
},
];
await command[1].permissions.set({ permissions });
console.log(`Set permissions for ${command[1].name}`);
} else {
// reset permissions
await command[1].permissions.set({ permissions: [] });
console.log(`Reset permissions for ${command[1].name}`);
}
} */
console.log(`Reminder to setup permissions.`);
});
client.on('interactionCreate', async interaction => {
if (interaction.isCommand()) {
commandHandler(interaction);
} else if (interaction.isButton()) {
buttonHandler(interaction);
} else {
await interaction.reply({ content: 'Unknown interaction :(' });
}
});
client.on('messageCreate', async (message) => {
// check if the message starts with "!banana"
if (message.content.startsWith('!banana')) {
// initiate the bananaifier
const funEnabled = config.settings.FUN_ENABLED === "true";
if (!funEnabled) {
return message.channel.send('Fun is disabled :(');
}
let useMention = !!message.mentions.members.first()
console.log(useMention)
let user = useMention ? message.mentions.members.first().user : message.member.user
message.channel.sendTyping()
let bananad = await banana.bananaify(user.displayAvatarURL({ size: 4096, dynamic: true }))
message.channel.send({
files: [
{ attachment: bananad }
]
})
}
// mass ping detection
// detect if a message contains 10 or more mentions and is sent by a non-bot
if(!message?.mentions?.members?.size) return;
let threshold = parseInt(config.settings.PING_THRESHOLD) || 10;
// let found = await users.findOne({ discord: message.member.id });
// if (found) {
// threshold += 5;
// }
if (message.mentions.members.size >= threshold && !message.member.user.bot) {
// add the user to the mass ping list
let logChannel = await message.guild.channels.fetch(process.env.LOG_CHANNEL_ID);
if (!massPing.pingers.includes(message.member.user.id)) {
massPing.addPinger(message.member.user.id);
await logChannel.send(`${message.member.user.tag} was spamming with ${message.mentions.members.size} mentions. (this is ${message.mentions.members.size-threshold} over the limit) If they do this again, they will be muted. user: <@${message.author.id}>`);
} else {
// this isnt their first warning.
// we will now kick them (to the rules channel) and mute them, by adding them the PINGER_ROLE_ID
let pingerRole = await message.guild.roles.fetch(process.env.PINGER_ROLE_ID);
await logChannel.send(`${message.member.user.tag} was muted for spamming with ${message.mentions.members.size} mentions. (this is ${message.mentions.members.size-threshold} over the limit). user: <@${message.author.id}>`);
await message.member.roles.add(pingerRole);
// await message.member.kick()
}
}
})
const commandHandler = async (interaction) => {
if (interaction.commandName === 'verify') {
// verify user
let response = await verification.start(interaction.member.user.id, interaction.options.getString('username'))
await interaction.reply(response);
} else if (interaction.commandName == 'whois') {
// find a users linked Scratch account
if (!interaction.member.roles.cache.get(process.env.MODERATOR_ROLE_ID)) return interaction.reply({ content: 'You do not have permission to use this command!', ephemeral: true });
let response = await whois(interaction.options.getUser('user'))
await interaction.reply(response);
} else if (interaction.commandName == 'id') {
// a list of all linked Scratch accounts for a user
let response = await whois(interaction.member.user)
await interaction.reply(response);
} else if (interaction.commandName == 'scratchwhois') {
// find a users linked Discord account
if (!interaction.member.roles.cache.get(process.env.MODERATOR_ROLE_ID)) return interaction.reply({ content: 'You do not have permission to use this command!', ephemeral: true })
let response = await scratchWhois(interaction.options.getString('user'))
await interaction.reply(response);
} else if (interaction.commandName == 'adminremove') {
// remove a linked Scratch account for a Discord user
if (!interaction.member.roles.cache.get(process.env.MODERATOR_ROLE_ID)) return interaction.reply({ content: 'You do not have permission to use this command!', ephemeral: true });
let discord = interaction.options.getUser('discord');
let scratch = interaction.options.getString('scratch');
let user = await users.findOne({ discord: discord.id })
if (!user) return interaction.reply({ content: 'User not found in database', ephemeral: true });
if (!user.scratch.includes(scratch)) return interaction.reply({ content: `Scratch username ${scratch} not linked to Discord account ${discord.tag}`, ephemeral: true });
user.scratch = user.scratch.filter(i => i !== scratch)
user.updated = Date.now()
if (user.scratch.length == 0) {
await users.remove({ discord: discord.id })
await interaction.reply({ content: "Gone, reduced to atoms." })
} else {
// otherwise save the user
await users.update({ discord: discord.id }, { $set: user })
let response = await whois(discord)
return await interaction.reply(response);
}
} else if (interaction.commandName == 'adminadd') {
// bypass verification and add a linked Scratch account for a Discord user
if (!interaction.member.roles.cache.get(process.env.MODERATOR_ROLE_ID)) return interaction.reply({ content: 'You do not have permission to use this command!', ephemeral: true });
let discord = interaction.options.getUser('discord');
let discordID = discord.id;
let scratchName = interaction.options.getString('scratch');
let existingUser = await users.findOne({ discord: discord.id })
if (existingUser) {
// the user exists (already verified as another account, we should update their scratch array
// but first lets check if they are already verified as them
if (existingUser.scratch.includes(scratchName)) {
return interaction.reply({ content: `${discord.tag} is already verified as ${scratchName}.`, ephemeral: true })
}
// ok now we are sure its okay to update their scratch array.. lets do it!
let user = existingUser
user.scratch.push(scratchName)
user.updated = Date.now()
await users.update({ discord: discordID }, { $set: user })
// that was epic. lets tell the user they're verified
return interaction.reply({ content: `${discord.tag} is now verified as ${scratchName}.`, ephemeral: true })
} else {
// we should create a new user
user = await users.insert({ discord: discordID, scratch: [scratchName], updated: Date.now() })
// that was epic. lets tell the user they're verified
return interaction.reply({ content: `${discord.tag} is now verified as ${scratchName}.`, ephemeral: true })
}
} else if (interaction.commandName == "bio") {
// set bio
let user = await users.findOne({ discord: interaction.member.user.id })
if (!user) return interaction.reply({ content: `You aren't verified yet. Use /verify to get started.`, ephemeral: true });
let bio = interaction.options.getString('bio');
if (bio.length > 250) return interaction.reply({ content: "Bio is too long. Max length is 250 characters.", ephemeral: true });
user.bio = bio;
user.updated = Date.now()
await users.update({ discord: interaction.member.user.id }, { $set: user });
// log this
let logChannel = interaction.guild.channels.cache.get(process.env.LOG_CHANNEL_ID)
logChannel.send({ content: `${interaction.user.username} (${interaction.user.id}) changed their bio to\`\`\`${bio}\`\`\`` })
return interaction.reply({ content: `Bio set to ${bio}. Use /id to see it.`, ephemeral: true });
} else if (interaction.commandName == "apply") {
let application = interaction.options.getString('application');
let age = interaction.options.get('age').value;
let tz = interaction.options.get('timezone').value;
// get scratch usernames (or tell the user they need to be verified first)
let user = await users.findOne({ discord: interaction.user.id })
if (!user) {
// return interaction.reply({ content: `Thanks for applying, however your application could not be processed. Please ensure you are verified with griffbot (run /verify) before applying.`, ephemeral: true });
}
let logChannel = interaction.guild.channels.cache.get(process.env.APPLICATION_LOG_CHANNEL_ID)
const applicationsOpen = config.settings.APPLICATIONS_OPEN === "true";
// if applications are closed or there is no logChannel, tell the user
if (!applicationsOpen || !logChannel) {
return interaction.reply("Thanks for applying, however applications are currently closed. Please try again later.")
}
logChannel.send({ content: `${interaction.user.username} (${interaction.user.id}) applied.\n\nAge: ${age}\nTimezone: ${tz}\n\nApplication:\n> ${application}\n\nScratch accounts: \n${user ? user.scratch.map(i => '- ' + i).join('\n') : "none found"}\ngriffbot bio: ${user?.bio || '[not set]'}` })
return interaction.reply({ content: `Thanks for applying.`, ephemeral: true });
} else if (interaction.commandName == "report") {
report.takeInteraction(interaction)
} else if (interaction.commandName == "warn") {
warn.takeInteraction(interaction)
} else if (interaction.commandName == "infractions") {
warn.takeInfractionsInteraction(interaction)
} else if (interaction.commandName == "banana") {
// banana is the image fx module
banana.takeInteraction(interaction)
} else if (interaction.commandName == "bean") {
// banana is the image fx module
interaction.reply(`<@${interaction.options.getUser('user').id}> got beaned`)
} else {
await interaction.reply('Unknown command');
}
}
const buttonHandler = async (interaction) => {
if (interaction.customId === 'continue') {
let response = await verification.check(interaction.member)
await interaction.reply(response);
} else if (interaction.customId === 'mark-resolved') {
report.takeResolveInteraction(interaction)
} else if (interaction.customId === 'mark-open') {
report.takeReopenInteraction(interaction)
} else if (interaction.customId === 'delete-warn') {
warn.takeDeleteInteraction(interaction)
} else {
await interaction.reply('Unknown button');
}
}
client.login(process.env.DISCORD_TOKEN);