-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
487 lines (403 loc) · 21.5 KB
/
main.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
const { timeStamp } = require('console');
const Discord = require('discord.js');
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION']});
require('dotenv-flow').config();
const config = {
//you must create .env file (like .env_sample) and create values
//DEVELOPMENT MODE:
//WORKING IN DEV BRANCH. REPLACE DEV VALUES AND MERGE TO MASTER
token: process.env.TOKEN,
owner: process.env.OWNER,
prefix: process.env.PREFIX,
codeblock: process.env.CODEBLOCK,
generalChat: process.env.GENERALCHAT,
testingChat: process.env.TESTINGCHAT,
registration: process.env.REGISTRATION,
verification: process.env.VERIFICATION,
server: process.env.SERVERNAME,
teams: process.env.TEAMS,
};
//command files using other .js files
const fs = require('fs');
const { type } = require('os');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
//listens to see if bot is online (in CMD, type: node .)
client.once('ready', () => {
console.log('_________________________________________________________________');
console.log('Cody (DEV) is online!');
var userID = config.owner;
var hello = 'ACTIVE MODE: RUNNING FROM GITHUB!\n🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥';
client.users.cache.get(userID).send(hello);
client.user.email
});
//makes bot listen to messages and commands
client.on('message', message=>{
//checks if message has codeblock format
if(!message.content.startsWith(config.codeblock) || message.author.bot) return;
//checks if message has Prefix command for CodyBot
//finds unwanted characters (`, \n), replaces them with spaces, and removes spaces
const inBlock = /[`\n]+/g;
const existing = message.content.replace(inBlock, ' ').trim();
//only substrings out the code block quotes
console.log('1) Message.content = ' + message.content);
console.log('2) existing = :::' + existing + '::'); //check for start-end spaces
const args = existing.slice(config.prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
console.log('3) args = ' + args + ':::' + args.length + ':::' + (typeof args));
for (arg in args){
console.log('\t\t3[' + arg + ']) args = :::' + args[arg] + ':::' + (args[arg].length) + ':::' + (typeof args[arg]));
}
console.log('4) command = ' + command);
console.log('-------------------------------------');
console.log('message id:) ' + message);
console.log('channel.name:) ' + message.channel.name);
console.log('-------------------------------------');
//Dynamically analyze commands
if (!client.commands.has(command)) return message.channel.send('```> Error: Command does not exist.```');
try {
client.commands.get(command).execute(message, args, client);
}catch (error) {
console.error(error);
message.channel.send('```> Error: No response. Please try again.```');
}
});
client.on('messageReactionAdd', async (reaction, user) => {
let teamSignup = async() => {
let emojiName = reaction.emoji.name;
let appDev = reaction.message.guild.roles.cache.find(role => role.name === 'App Dev Team');
let collegeLoops = reaction.message.guild.roles.cache.find(role => role.name === 'College Loops Team');
let competitive = reaction.message.guild.roles.cache.find(role => role.name === 'Competitive Team');
let cyber = reaction.message.guild.roles.cache.find(role => role.name === 'Cyber Team');
let gameDev = reaction.message.guild.roles.cache.find(role => role.name === 'Game Dev Team');
let hackathon = reaction.message.guild.roles.cache.find(role => role.name === 'Hackathon Team');
let member = reaction.message.guild.members.cache.find(member => member.id === user.id);
try {
if (emojiName === 'AppDevTeam') {
await member.roles.add(appDev.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
else if (emojiName === 'CollegeLoopsTeam') {
await member.roles.add(collegeLoops.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
else if (emojiName === 'CompetitiveTeam') {
await member.roles.add(competitive.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
else if (emojiName === 'CybersecurityTeam') {
await member.roles.add(cyber.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
else if (emojiName === 'GameDevTeam') {
await member.roles.add(gameDev.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
else if (emojiName === 'HackathonTeam') {
await member.roles.add(hackathon.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
}
catch(err) {
console.log('TEAM ROLE ADD ERROR:\n' + err);
}
}
let studentRegistration = async() => {
if (reaction.message.content != '' && reaction.emoji.name === '❓'){
let extractID = reaction.message.content.replace(/[<@!]+|[a-zA-Z .>]+|&[0-9]+>/g, '')
let updateMember = reaction.message.guild.members.cache.find(member => member.id === extractID);
updateMember.send(`URGENT ${updateMember}` + '!\n\nI am reaching out to you because you were TEMPORARILY granted the \`@ Florida Poly Student\` role. Now, I need to verify that you are a Florida Poly Student.\n\nLet us begin the verification process to maintain this role. You will need to access the Cody Terminal in order to proceed. The Cody Terminal is accessed by typing in special commands in codeblocks. Codeblocks can be activated by typing three backquote symbols (**\\`\\`\\`**) before and after. The backquote symbol is located below the **ESC** key.\n\nAll commands must be typed like this example:\t***\\`\\`\\`>_cody <command>\\`\\`\\`***\n\n***Please enter the following commands, replacing with your information and removing < and >:***\n\`\`\`>_cody verify <Florida_Poly_student_email> <first_name> <last_name> <last_4_of_studentID>\`\`\`')
return
}
let emojiName = reaction.emoji.name;
let memberRole = reaction.message.guild.roles.cache.find(role => role.name === '👍 Member');
let phoenixRole = reaction.message.guild.roles.cache.find(role => role.name === '🔥 Florida Poly Student');
let id = reaction.message.embeds[0].fields[0].value.replace(/(<@|>)+/g, '')
let member = reaction.message.guild.members.cache.find(member => member.id === id);
//check is user is still member, if not then do nothing.
if (!member.roles.cache.some(role => role.id === memberRole.id)){
//change message color
const statusColor = reaction.message.embeds[0]
.setTitle('__**STUDENT VERIFICATION: ERROR**__')
.setColor('#FFFF00')
.setDescription(`An ERROR occured! Some time has passed and the user is no longer a ${memberRole}. Student Verification cannot continue. A message has been sent to the user.\n\nPlease give user some time to regain membership status.`);
reaction.message.edit(statusColor)
member.send(`URGENT ${member}!\n\nYour verification request has an error. Some time has passed since your verification request and you no longer have the \`@ Member\` role. Student verification cannot continue.\n\nIn order to recieve the \`@ Florida Poly Student\` role, you must be a \`@ Member\`. Please go the \`# Registration\` channel and agree to Programming_Club's server rules.`)
return
}
try {
if(emojiName === 'PhoenixPride') {
//change message color
const statusColor = reaction.message.embeds[0]
.setTitle('__**STUDENT VERIFICATION: APPROVED**__')
.setColor('#532D8E')
.setDescription(`User has been ***APPROVED*** the ${phoenixRole} role by <@${user.id}>. The user's nickname has been changed and a notification has been sent.`);
reaction.message.edit(statusColor)
//change userName
const updateName = reaction.message.embeds[0].fields[1].value
await member.setNickname(updateName)
.catch((error) => console.error('NICKNAME CHANGE failed:' + error));
//add role to member
await member.roles.add(phoenixRole.id)
.catch(() => console.error('ADD PHOENIX role failed.'));
//send user DM
member.send(`🎊Congrats, ${member}!🎉\n\nYour verification request has been APPROVED. You have been GRANTED the \`@ Florida Poly Student\` role which allows you to get full access of this server.\n\nHere's something to keep in mind while at Florida Poly:\n***"You only get out what you put in. Don't expect more until you do more."***\n\nHope this helps as you prepare for your career. In the meantime, enjoy your time at Florida Poly!\n\n**Go Phoenix!**`)
const attachment = new Discord.MessageAttachment('./images/PhoenixPride.gif');
member.send(attachment)
}
else if(emojiName === '❌') {
//change message color
const statusColor = reaction.message.embeds[0]
.setTitle('__**STUDENT VERIFICATION: DENIED**__')
.setColor('#F03A17')
.setDescription(`User has been ***DENIED*** the ${phoenixRole} role by <@${user.id}>. User has been notified.`);
reaction.message.edit(statusColor)
//send user DM
member.send('Oh no!\n\nSorry, but it looks like your verification request has been DENIED.\n\nIf you feel like this is an error, please reach out to a member of eboard to help you resolve this issue.\n\nHave a nice day!')
}
else {
try {
await reaction.message.reactions.removeAll()
.catch((error) => console.error('NICKNAME CHANGE failed:' + error));
} catch (error) {
console.error('Failed to remove reactions.');
}
}
}
catch(err) {
console.log('GRANT FLPOLY STUDENT ROLE ERROR:\n' + err);
}
}
let applyRegistration = async () => {
let emojiName = reaction.emoji.name;
let memberRole = reaction.message.guild.roles.cache.find(role => role.name === '👍 Member');
let phoenixRole = reaction.message.guild.roles.cache.find(role => role.name === '🔥 Florida Poly Student');
let member = reaction.message.guild.members.cache.find(member => member.id === user.id);
//checks if a member has a certain role
let userPHOENIX = member.roles.cache.some(role => role.id === phoenixRole.id)
let userMEMBER = member.roles.cache.some(role => role.id === memberRole.id)
try {
//if user added 👍 AND is not a MEMBER
if((emojiName === '👍') && (!userMEMBER && !userPHOENIX)) {
console.info('SELECTED PATH A: A| 1 F F')
//give MEMBER role
await member.roles.add(memberRole.id)
.catch(() => console.error('ADD MEMBER role failed.'));
}
//if user added Phoenix AND is a MEMBER
else if((emojiName === 'PhoenixPride') && (userMEMBER && !userPHOENIX)) {
console.info('SELECTED PATH B: A|2 T F')
//send user DM to request verification ()
member.send('Welcome, DEV!\n\nMy name is **Cody**. I am Programming Club\'s smart bot assistant.\n\nLet\'s begin the verification process for obtaining the `@ Florida Poly student` role. This role allows you to get full access to this server. You will need to access the Cody Terminal in order to proceed. The Cody Terminal is accessed by typing in special commands in codeblocks. Codeblocks can be activated by typing three backquote symbols (**\\`\\`\\`**) before and after. The backquote symbol is located below the **ESC** key.\n\nAll commands must be typed like this example:\t***\\`\\`\\`>_cody <command>\\`\\`\\`***\n\n***Please enter the following commands, replacing with your information and removing < and >:***\n\`\`\`>_cody verify <Florida_Poly_student_email> <first_name> <last_name> <last_4_of_studentID>\`\`\`')
}
else {
console.info('SELECTED PATH C: A|2 F F')
//remove PHOENIX role
await member.roles.remove(phoenixRole.id)
.catch(() => console.error('PHOENIX role does not exist for User.'));
try {
let msg = await reaction.message.fetch();
const userReactions = await msg.reactions.cache.filter(reaction => reaction.users.cache.has(member.id));
//it removes all reactions that was reacted (can be many selected)
for (const react of userReactions.values()) {
await react.users.remove(member.id);
console.info('ALL Reactions Removed')
}
} catch (error) {
console.error('Failed to remove reactions.');
}
//remove MEMBER role
await member.roles.remove(memberRole.id)
.catch(() => console.error('MEMBER role does not exist for User.'));
}
}
catch(err) {
console.log('FLPOLY STUDENT ROLE ERROR:\n' + err);
}
}
if(reaction.message.partial)
{
//console.log(reaction.message)
try {
let msg = await reaction.message.fetch();
if(msg.id === config.registration)
{
console.log("Message is a partial, but is Now Cached")
applyRegistration();
}
if (msg.channel.id === config.verification)
{
console.log("Message is a partial, but is Now Cached");
studentRegistration();
}
if (msg.id === config.teams)
{
console.log("Message is a partial, but is Now Cached");
teamSignup();
}
}
catch(err) {
console.log(err);
}
}
else
{
console.log("Not a partial. Already Cached.");
if(reaction.message.id === config.registration) {
applyRegistration();
}
if (reaction.message.channel.id === config.verification)
{
studentRegistration();
}
if (reaction.message.id === config.teams)
{
teamSignup();
}
}
});
client.on('messageReactionRemove', async (reaction, user) => {
let teamRemoval = async() => {
console.log('TEAMS SIGNUP START HERE')
let emojiName = reaction.emoji.name;
let appDev = reaction.message.guild.roles.cache.find(role => role.name === 'App Dev Team');
let collegeLoops = reaction.message.guild.roles.cache.find(role => role.name === 'College Loops Team');
let competitive = reaction.message.guild.roles.cache.find(role => role.name === 'Competitive Team');
let cyber = reaction.message.guild.roles.cache.find(role => role.name === 'Cyber Team');
let gameDev = reaction.message.guild.roles.cache.find(role => role.name === 'Game Dev Team');
let hackathon = reaction.message.guild.roles.cache.find(role => role.name === 'Hackathon Team');
let member = reaction.message.guild.members.cache.find(member => member.id === user.id);
try {
if (emojiName === 'AppDevTeam') {
await member.roles.remove(appDev.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
else if (emojiName === 'CollegeLoopsTeam') {
await member.roles.remove(collegeLoops.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
else if (emojiName === 'CompetitiveTeam') {
await member.roles.remove(competitive.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
else if (emojiName === 'CybersecurityTeam') {
await member.roles.remove(cyber.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
else if (emojiName === 'GameDevTeam') {
await member.roles.remove(gameDev.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
else if (emojiName === 'HackathonTeam') {
await member.roles.remove(hackathon.id)
.catch(() => console.error('ADD TEAMS role failed.'));
}
}
catch(err) {
console.log('TEAM ROLE REMOVAL ERROR:\n' + err);
}
}
let removeRegistration = async () => {
let emojiName = reaction.emoji.name;
let memberRole = reaction.message.guild.roles.cache.find(role => role.name === '👍 Member');
let phoenixRole = reaction.message.guild.roles.cache.find(role => role.name === '🔥 Florida Poly Student');
let member = reaction.message.guild.members.cache.find(member => member.id === user.id);
let userPHOENIX1 = member.roles.cache.some(role => role.id === phoenixRole.id)
let userMEMBER1 = member.roles.cache.some(role => role.id === memberRole.id)
try {
if((emojiName === 'PhoenixPride') && (userMEMBER1 && userPHOENIX1)) {
console.info('SELECTED PATH E: R|2 T T')
try {
await member.roles.remove(phoenixRole.id)
.catch(() => console.error('REMOVE STUDENT role failed.'));
console.log('REMOVE STUDENT role success.')
} catch (error) {
console.error('Failed role removal - PHOENIXPRIDE.');
}
}
else if ((emojiName === '👍') && (userMEMBER1 && !userPHOENIX1)) {
console.info('SELECTED PATH F: R|1 T F')
try {
await member.roles.remove(memberRole.id)
.catch(() => console.error('REMOVE STUDENT role failed.'));
console.log('REMOVE STUDENT role success.')
} catch (error) {
console.error('Failed role removal - PHOENIXPRIDE.');
}
}
else {
console.info('SELECTED PATH D: R|1 T T')
//removes Phoenix role
await member.roles.remove(phoenixRole.id)
.catch(() => console.error('PHOENIX role does not exist for User.'));
try {
let msg = await reaction.message.fetch();
const userReactions = await msg.reactions.cache.filter(reaction => reaction.users.cache.has(member.id));
//it removes all reactions that was reacted (can be many selected)
for (const react of userReactions.values()) {
await react.users.remove(member.id);
console.info('ALL Reactions Removed')
}
} catch (error) {
console.error('Failed to remove reactions.');
}
//remove MEMBER role
await member.roles.remove(memberRole.id)
.catch(() => console.error('MEMBER role does not exist for User.'));
//remove all remaining roles (if users have higher positions)
const roleList = member.roles.cache
.sort((a, b) => b.position - a.position)
.map(role => role.id.toString())
await member.roles.remove(roleList)
.catch(() => console.error('ALL roles were NOT REMOVED'));
}
}
catch(err) {
console.log('removeRegistration() - THERE IS AN ERROR:' + err);
}
}
if(reaction.message.partial)
{
//console.log(reaction.message)
try {
let msg = await reaction.message.fetch();
if(msg.id === config.registration)
{
console.log("Message is a partial, but is Now Cached")
removeRegistration();
}
if (msg.channel.id === config.verification)
{
return
}
if (msg.id === config.teams)
{
teamRemoval();
}
}
catch(err) {
console.log(err);
}
}
else
{
console.log("Not a partial. Already Cached.");
if(reaction.message.id === config.registration) {
removeRegistration();
}
if (reaction.message.channel.id === config.verification)
{
return
}
if (reaction.message.id === config.teams)
{
teamRemoval();
}
}
})
//Keep in last line of file
client.login(config.token);