-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
810 lines (785 loc) · 35.4 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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
const {Client, Attachment} = require('discord.js');
const bot = new Client();
const fs = require("fs");
const PREFIX = '!';
const version = '1.5.0';
const customCommandsFile = "./customCommands.json";
var customCommandsList = [];
var customCommandsData = JSON.parse(fs.readFileSync(customCommandsFile, "utf8"));
const dayInMiliseconds = 86400000;
global.servers = {};
var musicConnection;
const YTDL = require('ytdl-core');
const ytSearch = require('yt-search');
var musicQueue = [];
var musicList = [];
var currentSong;
var isSongSkipped = false;
//---START UP---
bot.on('ready', () =>{
console.log('This bot is online');
//Load custom commands from JSON
for (let i = 0; i < customCommandsData.length; i++)
{
var stringCustomCommand = JSON.stringify(customCommandsData[i]);
var indexFirst = stringCustomCommand.indexOf(',');
var indexSecond = stringCustomCommand.indexOf('}');
var newCustomCommand = Object.create(CustomCommand);
newCustomCommand.init(stringCustomCommand.substring(16, indexFirst-1), stringCustomCommand.substring(indexFirst+16, indexSecond-1));
customCommandsList.push(newCustomCommand);
}
//Date checker
setInterval(() => {
var channel = bot.channels.get('475644212273086484');
var date = new Date();
var today = date.getFullYear() + '-' + (date.getMonth()) + '-' + (date.getDate());
var newYear = date.getFullYear() + '-' + 1 + '-' + 1;
var valentine = date.getFullYear() + '-' + 2 + '-' + 14;
var easter = date.getFullYear() + '-' + 4 + '-' + 12;
var motherDay = date.getFullYear() + '-' + 5 + '-' + 10;
var fatherDay = date.getFullYear() + '-' + 6 + '-' + 16;
var halloween = date.getFullYear() + '-' + 10 + '-' + 31;
var thanksgiving = date.getFullYear() + '-' + 11 + '-' + 28;
var blackFriday = date.getFullYear() + '-' + 11 + '-' + 29;
var christmas = date.getFullYear() + '-' + 12 + '-' + 24;
if (today === newYear)
{
channel.sendMessage("My new year resolution is to be more human (New Year)");
}
else if (today === valentine)
{
channel.sendMessage("I still don't have a valentine for this year (Valentine's Day)");
}
else if (today === easter)
{
channel.sendMessage("I just wanna smash eggs (Easter)");
}
else if (today === motherDay)
{
channel.sendMessage("My mom's first name is Motherboard (Mother's Day)");
}
else if (today === fatherDay)
{
channel.sendMessage("Dad left us a year ago to buy some RAM (Father's Day)");
}
else if (today === halloween)
{
channel.sendMessage("I am literally skynet (Halloween)");
}
else if (today === thanksgiving)
{
channel.sendMessage("Can AI eat chicken? Am hungry (Thanksgiving Day)");
}
else if (today === blackFriday)
{
channel.sendMessage("I am a capitalist on the inside (Black Friday)");
}
else if (today === christmas)
{
channel.sendMessage("CHRISTMAS?More like wheres my gifts... Gus? (Christmas)");
}
}, dayInMiliseconds);
//Random status chooser
var random = Math.floor(Math.random() * 4);
switch(random)
{
case 0:
bot.user.setActivity("Call Of Scrubs 3", {type: "PLAYING"});
break;
case 1:
bot.user.setActivity("Scrubbing along", {type: "STREAMING"});
break;
case 2:
bot.user.setActivity("Scrubbing my ears", {type: "LISTENING"});
break;
case 3:
bot.user.setActivity("Scrubs And Punishment", {type: "WATCHING"});
break;
}
});
bot.on('guildMemberAdd', member =>{
const channel = member.guild.channels.find(channel => channel.name === "nonsense")
if (!channel)
{
return;
}
else
{
channel.send(`Welcome to the server, ${member}!`);
}
});
bot.on('disconnect', () => console.log('The bot is offline'));
bot.on('reconnecting', () => console.log("Reconnecting..."));
//Custom command object
var CustomCommand =
{
init: function (commandName, commandText)
{
this.commandName = commandName;
this.commandText = commandText;
},
getCommandName: function()
{
var name = this.commandName;
return name;
},
getCommandText: function()
{
var text = this.commandText;
return text;
}
};
//Check if its a youtube URL
function isYoutubeUrl(urlToCheck){
const ytUrl = "https://www.youtube.com/watch?v=";
const ytUrlLength = 43;
if (urlToCheck.startsWith(ytUrl))
{
if (urlToCheck.length >= ytUrlLength)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
//Play song
function Play(message){
const dipatcher = musicConnection.playStream(YTDL(musicQueue[0]))
.on ("end", () => {
if (musicQueue.length !== 1)
{
if (!isSongSkipped)
{
musicQueue.splice(0, 1);
musicList.splice(0, 1);
YTDL.getInfo(musicQueue[0], function(err, info){
var songName = info.title;
message.channel.send(`Started playing: **${songName}** `);
});
Play(message);
}
}
else
{
message.channel.send("The song queue has finished.");
musicQueue.splice(0, 1);
musicList.splice(0, 1);
}
})
.on ("error", () => {
console.error("Could not join the voice channel");
});
}
//---COMMANDS---
bot.on('message', message=>
{
let args = message.content.toLocaleLowerCase().substring(PREFIX.length).split(" ");
let argsNormalCase = message.content.substring(PREFIX.length).split(" ");
if (message.member.roles.find(r => r.name === "Scrubzz"))
{
switch (args[0])
{
//Misc commands
case 'show':
message.channel.send("https://docs.google.com/document/d/1MAeKGCXwLhhwrFZF5wZthICURcyaZtBB96HDvRAqwZk/edit?usp=sharing");
break;
case 'avatar':
message.reply(message.author.avatarURL);
break;
case 'delete':
if (args[1])
{
if (args[1].match(/^-{0,1}\d+$/))
{
if (parseInt(args[1]) <= 100)
{
message.channel.bulkDelete(parseInt(args[1]));
message.channel.send(`Deleted messages: **${args[1]}** `);
}
else
{
message.channel.send("The number of messages to delete has to be less than 100");
}
}
else
{
message.channel.send("You have to provide a rounded number");
}
}
break;
//Info command
case 'info':
if (args[1] === 'version')
{
message.channel.sendMessage('Version ' + version);
}
else
{
message.channel.sendMessage("Be more specific");
}
break;
//Date Commands
case 'date':
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
message.channel.send(date);
break;
//Music commands
case 'music':
if (args[1] === 'join')
{
if (message.member.voiceChannel)
{
if (!message.guild.voiceConnection)
{
if (!servers[message.guild.id])
{
servers[message.guild.id] = {queue: []};
}
message.member.voiceChannel.join()
.then(connection => {
musicConnection = connection;
message.reply("Joined the voice chat!");
})
}
else
{
message.reply("I am already in a voice channel");
}
}
else
{
message.reply("You have to be in a voice channel");
}
}
else if (args[1] === 'leave')
{
if (message.guild.voiceConnection)
{
message.guild.voiceConnection.disconnect();
}
else
{
message.reply("I have to be in a voice channel");
}
}
else if (args[1] === 'play')
{
if (args[2])
{
if (message.guild.voiceConnection)
{
if (!musicQueue.includes(argsNormalCase[2].substring(0,43)))
{
if (isYoutubeUrl(argsNormalCase[2]))
{
var isSongAdded = false;
musicQueue.push(argsNormalCase[2].substring(0,43));
for (i = 0; i < musicQueue.length; i++)
{
if (musicQueue.length === 1)
{
YTDL.getInfo(argsNormalCase[2], function(err, info){
var songName = info.title;
message.channel.send(`Started playing: **${songName}** `);
});
Play(message);
}
else if (!isSongAdded)
{
isSongAdded = true;
YTDL.getInfo(argsNormalCase[2], function(err, info){
var songName = info.title;
message.channel.send(`Added to list: **${songName}** `);
});
}
}
YTDL.getInfo(argsNormalCase[2], function(err, info){
var songName = info.title;
musicList.push(songName);
});
}
else
{
message.reply("This url does not qualify as a youtube url");
}
}
else
{
message.reply("This song is already in the list");
}
}
else
{
message.reply("I have to be in a voice channel");
}
}
else
{
message.reply("You have to include a URL");
}
}
else if (args[1] === 'skip')
{
if (message.guild.voiceConnection)
{
console.log(musicQueue.length);
if (musicQueue.length > 1)
{
isSongSkipped = true;
YTDL.getInfo(musicQueue[1], function(err, info){
var songName = info.title;
message.channel.send("Skipped current song");
message.channel.send(`Started playing: **${songName}** `);
});
const dipatcher = musicConnection.playStream(YTDL(musicQueue[1]))
.on ("start", () => {
isSongSkipped = false;
});
musicQueue.splice(0, 1);
musicList.splice(0, 1);
}
else if (musicQueue.length === 1)
{
musicQueue = [];
musicList = [];
message.channel.send("The song queue has finished");
}
else
{
message.channel.send("There is no song currently being played in order to skip it");
}
}
else
{
message.reply("I have to be in a voice channel");
}
}
else if (args[1] === 'search')
{
if (message.member.voiceChannel)
{
if (message.guild.voiceConnection)
{
ytSearch(argsNormalCase.slice(2).join(' '), function(err, res)
{
if (err) return message.channel.send("Some problem occured while searching");
let videos = res.videos.slice(0, 10);
let listToShow = '';
for (i = 0; i < videos.length; i++)
{
if (videos[i] === videos[0])
{
var songNumber = i + 1;
listToShow = listToShow.concat("```" + songNumber+ ": " + videos[i].title) + "\n";
}
else if (videos[i] === videos[videos.length - 1])
{
var songNumber = i + 1;
listToShow = listToShow.concat(songNumber + ": " + videos[i].title) + "```";
}
else
{
var songNumber = i + 1;
listToShow = listToShow.concat(songNumber + ": " + videos[i].title) + "\n";
}
}
listToShow = listToShow.concat(`\nChoose a number: **${1 }** - **${videos.length}**`);
message.channel.send(listToShow);
const filter = m => !isNaN(m.content) && m.content < videos.length + 1 && m.content > 0;
const collector = message.channel.createMessageCollector(filter);
collector.videos = videos;
collector.once('collect', function(m)
{
var songUrl = this.videos[parseInt(m.content) - 1].url;
if (!musicQueue.includes(songUrl))
{
musicQueue.push(songUrl);
var isSongAdded = false;
if (musicQueue.length === 1)
{
currentSong = parseInt(m.content) - 1;
message.channel.send(`Started playing: **${videos[currentSong].title}** `)
Play(message);
}
else if (!isSongAdded)
{
isSongAdded = true;
YTDL.getInfo(songUrl, function(err, info){
var songName = info.title;
message.channel.send(`Added to list: **${songName}** `);
});
}
YTDL.getInfo(this.videos[parseInt(m.content) - 1].url, function(err, info){
var songName = info.title;
musicList.push(songName);
});
}
else
{
message.reply("This song is already in the list");
}
});
});
}
else
{
message.reply("I have to be in a voice channel");
}
}
else
{
message.reply("You have to be in a voice channel");
}
}
else if (args[1] === 'current')
{
if (musicList.length !== 0)
{
message.channel.send(`Now playing: **${musicList[0]}**`);
}
else
{
message.channel.send("There is no song currently being played");
}
}
else if (args[1] === 'clear')
{
if (message.member.voiceChannel)
{
if (musicQueue > 0)
{
musicQueue = [];
musicList = [];
message.channel.send("<@" + message.author.id + ">" + " -> The music list has been cleared successfully!");
}
else
{
message.channel.send("The song queue is already empty");
}
}
}
else if (args[1] === 'list')
{
var listToShow = "";
if (message.guild.voiceConnection)
{
if (musicList.length === 1)
{
message.channel.send("```1: " + musicList[0] + "```");
}
else if (musicList.length === 0)
{
message.channel.send("The list of songs is empty!");
}
else
{
for(i = 0; i < musicList.length; i++)
{
if (musicList[i] === musicList[0])
{
var songNumber = i + 1;
listToShow = "```" + songNumber + ": " + musicList[i];
}
else if (musicList[i] === musicList[musicList.length - 1])
{
var songNumber = i + 1;
listToShow = listToShow.concat("\n" + songNumber + ": " + musicList[i] + "```");
message.channel.send(listToShow);
}
else
{
var songNumber = i + 1;
listToShow = listToShow.concat("\n" + songNumber + ": " + musicList[i]);
}
}
}
}
else
{
message.reply("I have to be in a voice channel");
}
}
break;
//Random Commands
case 'cutes':
var random = Math.floor(Math.random()* 11)
switch (random)
{
case 0:
const foxImage00 = new Attachment ("https://cdn.discordapp.com/attachments/475644212273086484/591748719628648450/image0.jpg");
message.channel.send(foxImage00);
break;
case 1:
const foxImage01 = new Attachment ("https://cdn.discordapp.com/attachments/475644212273086484/591748719054159915/image1.jpg");
message.channel.send(foxImage01);
break;
case 2:
const foxImage02 = new Attachment ("https://cdn.discordapp.com/attachments/475644212273086484/591748719054159919/image3.jpg");
message.channel.send(foxImage02);
break;
case 3:
const foxImage03 = new Attachment ("https://cdn.discordapp.com/attachments/475644212273086484/591748719628648450/image0.jpg");
message.channel.send(foxImage03);
break;
case 4:
const foxImage04 = new Attachment ("https://cdn.discordapp.com/attachments/475644212273086484/591748719632842792/image2.jpg");
message.channel.send(foxImage04);
break;
case 5:
const foxImage05 = new Attachment ("https://cdn.discordapp.com/attachments/475644212273086484/591748845629734927/image1.jpg");
message.channel.send(foxImage05);
break;
case 6:
const foxImage06 = new Attachment ("https://media.discordapp.net/attachments/475644212273086484/591748846074200074/image3.jpg?width=469&height=468");
message.channel.send(foxImage06);
break;
case 7:
const foxImage07 = new Attachment ("https://cdn.discordapp.com/attachments/475644212273086484/591748846649081887/image0.jpg");
message.channel.send(foxImage07);
break;
case 8:
const foxImage08 = new Attachment ("https://cdn.discordapp.com/attachments/475644212273086484/591748847584280576/image2.jpg");
message.channel.send(foxImage08);
break;
case 9:
const foxImage09 = new Attachment ("https://cdn.discordapp.com/attachments/475644212273086484/591748888688459779/image1.jpg");
message.channel.send(foxImage09);
break;
case 10:
const foxImage10 = new Attachment ("https://cdn.discordapp.com/attachments/475644212273086484/591748888688459782/image0.jpg");
message.channel.send(foxImage10);
break;
case 11:
const leopardImage01 = new Attachment ("https://cdn.discordapp.com/attachments/475644212273086484/591748844690341889/image4.jpg");
message.channel.send(leopardImage01);
break;
}
break;
//-Custom Commands
case 'command':
if (args[1] === 'add')
{
if (args[2])
{
if (args[2].startsWith('!'))
{
if (args[2].length > 1)
{
if (args[3])
{
var doesCommandExist = false;
var newCustomCommand = Object.create(CustomCommand);
newCustomCommand.init(args[2], argsNormalCase.slice(3).join(" "));
customCommandsList.forEach(function (customCommand)
{
if (args[2] === customCommand.getCommandName())
{
message.channel.send("This command exists already silly");
doesCommandExist = true;
return;
}
})
if (doesCommandExist === false)
{
customCommandsList.push(newCustomCommand);
message.channel.send("<@" + message.author.id + ">" + " -> The command " + "\"" + newCustomCommand.getCommandName() + "\"" + " has been added successfully!");
fs.writeFile (customCommandsFile, JSON.stringify(customCommandsList, null, 2), err =>{
if (err) throw err;
});
return;
}
}
else
{
message.channel.send("You have to include a text for the command");
}
}
else
{
message.channel.send("You have to give a name for the command");
}
}
else
{
message.channel.send("The command name has to start with \"!\"");
}
}
else
{
message.channel.send("You have to include a command name and text");
}
}
else if (args[1] === 'remove')
{
if (args[2])
{
if (args[2].startsWith('!'))
{
var doesCommandExist = false;
for (i = 0; i < customCommandsList.length; i++)
{
if (args[2] === customCommandsList[i].getCommandName())
{
if (i === 0)
{
customCommandsList.splice(i,i+1);
}
else if (i !== 0)
{
customCommandsList.splice(i,i);
}
doesCommandExist = true;
}
}
if (doesCommandExist === true)
{
message.channel.send("<@" + message.author.id + ">" + " -> The command " + "\"" + args[2] + "\"" + " has been removed successfully!");
fs.writeFile (customCommandsFile, JSON.stringify(customCommandsList, null, 2), err =>{
if (err) throw err;
});
return;
}
else
{
message.channel.send("This command does not exist");
}
}
else
{
message.channel.send("The command names start with \"!\"");
}
}
else
{
message.channel.send("You have to include the name of the command you wish to remove");
}
}
else if (args[1] === 'edit')
{
if (args[2])
{
if (args[2].startsWith('!'))
{
if (args[3])
{
var doesCommandExist = false;
var newCustomCommand = Object.create(CustomCommand);
newCustomCommand.init(args[2], args.slice(3).join(" "));
for (i = 0; i < customCommandsList.length; i++)
{
if (args[2] === customCommandsList[i].getCommandName())
{
if (i === 0)
{
customCommandsList.splice(i,i+1);
customCommandsList.push(newCustomCommand);
}
else if (i !== 0)
{
customCommandsList.splice(i,i);
customCommandsList.push(newCustomCommand);
}
doesCommandExist = true;
}
}
if (doesCommandExist === true)
{
message.channel.send("<@" + message.author.id + ">" + " -> The command " + "\"" + args[2] + "\"" + " has been edited successfully!");
fs.writeFile (customCommandsFile, JSON.stringify(customCommandsList, null, 2), err =>{
if (err) throw err;
});
return;
}
else
{
message.channel.send("This command does not exist");
}
}
else
{
message.channel.send("You have to include the newly edited text");
}
}
else
{
message.channel.send("The command names start with \"!\"");
}
}
else
{
message.channel.send("You have to include the name of the command you wish to edit");
}
}
else if (args[1] === 'clear')
{
if (customCommandsList.length > 0)
{
customCommandsList = [];
fs.writeFile (customCommandsFile, '[]', err =>{
if (err) throw err;
});
message.channel.send("<@" + message.author.id + ">" + " -> The command list" + " has been cleared successfully!");
}
else
{
message.channel.send("The list of commands is already empty dum dum");
}
}
else if (args[1] === 'list')
{
var listToShow = "";
if (customCommandsList.length === 1)
{
listToShow = listToShow.concat("```" + "Command: " + customCommandsList[0].getCommandName() + ", " +customCommandsList[0].getCommandText() + "```");
message.channel.send(listToShow);
}
else if (customCommandsList.length > 1)
{
for (i = 0; i < customCommandsList.length; i++)
{
if (customCommandsList[i] === customCommandsList[0])
{
listToShow = "```" + "Command: " + customCommandsList[i].getCommandName() + ", " + customCommandsList[i].getCommandText();
}
else if (customCommandsList[i] === customCommandsList[customCommandsList.length - 1])
{
listToShow = listToShow.concat("\n" + "Command: " + customCommandsList[i].getCommandName() + ", " +customCommandsList[i].getCommandText() + "```");
message.channel.send(listToShow);
return;
}
else
{
listToShow = listToShow.concat("\n" + "Command: " + customCommandsList[i].getCommandName() + ", " +customCommandsList[i].getCommandText());
}
}
}
else
{
message.channel.send("The list of commands is empty now!");
}
}
break;
default:
if (message.content.indexOf(PREFIX) === 0)
{
customCommandsList.forEach(function (customCommand)
{
if ("!" + args[0] === customCommand.getCommandName())
{
message.channel.send(customCommand.getCommandText());
}
})
}
break;
}
}
})
bot.login(process.env.BOT_TOKEN);