-
Notifications
You must be signed in to change notification settings - Fork 7
/
picabot.js
executable file
·631 lines (623 loc) · 21 KB
/
picabot.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
require("dotenv").config();
const Discord = require("discord.js");
const ytdl = require("ytdl-core");
const fs = require("fs");
const {google} = require("googleapis");
const youtube = google.youtube("v3");
const bot = new Discord.Client();
const prefix = "!";
const botChannelName = "pica-commands";
var botChannel;
var fortunes = ["It is certain", "It is decidedly so", "Without a doubt", "Yes definitely", "You may rely of it", "As I see it, yes", "Most likely", "Outlook good", "Yes", "Signs point to yes", "Reply hazy try again", "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again", "Dont count on it", "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful"];
var dispatcher;
var songQueue = [];
var currentSongIndex = 0;
var previousSongIndex = 0;
var shuffle = false;
var autoremove = false;
var commands = {
"help": {
"usage": "<command> | -a or --all",
"description": "Gives you a list of commands you can use or details on specific command(s)",
"process": function(message, args){
var commandKeys = Object.keys(commands);
if(args.length === 0){
var commandList = "";
for(var i = 0; i < commandKeys.length - 1; i++){
commandList += `\`${commandKeys[i]}\`, `;
}
commandList += `and \`${commandKeys[commandKeys.length - 1]}\``;
botChannel.send("My current commands are: " + commandList, {reply: message});
botChannel.send(`You can use \`${prefix}help <command>\` to learn more about a command!`);
} else{
var helpList = "";
if(args[0] === "-a" || args[0] === "--all"){
for(var i = 0; i < commandKeys.length; i++){
helpList += `\`!${commandKeys[i]} ${commands[commandKeys[i]].usage}\`: ${commands[commandKeys[i]].description}\n`;
}
} else{
for(var i = 0; i < args.length; i++){
try{
helpList += `\`!${args[i]} ${commands[args[i]].usage}\`: ${commands[args[i]].description}\n`;
} catch(e){
helpList += `\`!${args[i]}\`: Not a command\n`;
}
}
}
botChannel.send(helpList, {reply: message});
}
}
},
"bot": {
"usage": "",
"description": "Tells you information about the bot",
"process": function(message, args){
botChannel.send("I am a discord bot for didney worl who has an appetite for non-nutritive substances", {reply: message});
botChannel.send("If you have any suggestions or command ideas for me tell @Crumster or your local amin");
}
},
"ping": {
"usage": "",
"description": "Pings the bot, useful for seeing if it's alive",
"process": function(message, args){
botChannel.send("Pong :ping_pong:", {reply: message});
}
},
"roll": {
"usage": "<amount>d<sides>+<modifier>",
"description": "Rolls DnD style dice",
"process": function(message, args){
if(Math.floor(Math.random() * 100 + 1) === 1){
botChannel.send("You tried to roll a `die` :game_die: and got: `rick`", {reply: message});
botChannel.send("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
return;
}
if(args.length === 0){
botChannel.send(`You rolled \`1d6\` :game_die: and got: \`${Math.floor(Math.random() * 6 + 1)}\``, {reply: message});
} else{
for(var i = 0; i < args.length; i++){
var regex = args[i].match(/^(\d*)d(\d+)\+?(\d*)$/);
if(regex === null){
botChannel.send(`\`${args[i]}\` is not a valid die`, {reply: message});
} else{
if(regex[1] === "") regex[1] = 1;
if(regex[3] === "") regex[3] = 0;
var rolls = "(";
var roll;
var sum = 0;
for(var j = 0; j < regex[1] - 1; j++){
roll = Math.floor(Math.random() * Number.parseInt(regex[2]) + 1);
sum += roll;
rolls += roll + ", ";
}
roll = Math.floor(Math.random() * Number.parseInt(regex[2]) + 1);
sum += roll;
rolls += roll + ") + " + regex[3] + " = " + (sum + Number.parseInt(regex[3]));
message.channel.send(`You rolled \`${args[i]}\` :game_die: and got: \`${rolls}\``, {reply: message});
}
}
}
}
},
"8ball": {
"usage": "",
"description": "Asks a magic 8ball for a fortune",
"process": function(message, args){
botChannel.send(fortunes[Math.floor(Math.random() * fortunes.length)], {reply: message});
}
},
"save": {
"usage": "<key> <message>",
"description": "Saves a personalized message with a given key",
"process": function(message, args){
botChannel.send("**Disclaimer:** your message will not be permanantly saved and will delete upon bot restart (for now)", {reply: message});
if(args.length < 2){
botChannel.send(`Save a message with \`${prefix}save <key> <message>\``, {reply: message});
return;
}
var key = args[0];
var messageToSave = "";
for(var i = 0; i < args.length - 2; i++){
messageToSave += args[i + 1] + " ";
}
messageToSave += args[args.length - 1];
fs.readFile("save.json", "utf8", function(err, data){
if(err) throw err;
var save = JSON.parse(data);
if(save[message.author.username] === undefined){
save[message.author.username] = {};
}
save[message.author.username][key] = messageToSave;
fs.writeFile("save.json", JSON.stringify(save), "utf8", function(err){
if(err) throw err;
botChannel.send(`Your message has been saved as \`${key}\`! :tada:`, {reply: message});
});
});
}
},
"recall": {
"usage": "<key>",
"description": "Lists your saved messages or recalls a saved message with a given key",
"process": function(message, args){
fs.readFile("save.json", "utf8", function(err, data){
if(err) throw err;
var save = JSON.parse(data);
if(args.length === 0){
var messageKeys;
var savedMessages = "";
try{
messageKeys = Object.keys(save[message.author.username]);
} catch(e){
botChannel.send("You have no saved messages, try saving one!", {reply: message});
return;
}
if(messageKeys.length === 0){
botChannel.send("You have no saved messages, try saving one!", {reply: message});
return;
}
for(var i = 0; i < messageKeys.length - 1; i++){
savedMessages += messageKeys[i] + ", ";
}
savedMessages += messageKeys[messageKeys.length - 1];
botChannel.send("Your saved messages are: " + savedMessages, {reply: message});
} else{
var key = args[0];
var recalledMessage;
try{
recalledMessage = save[message.author.username][key];
} catch(e){
botChannel.send(`You don't have a saved message with the key \`${key}\``, {reply: message});
return;
}
botChannel.send(recalledMessage, {reply: message});
}
});
}
},
"delete": {
"usage": "<key>",
"description": "Deletes a saved message with a given key",
"process": function(message, args){
fs.readFile("save.json", "utf8", function(err, data){
if(err) throw err;
var save = JSON.parse(data);
if(args.length === 0){
botChannel.send(`Delete a saved message with \`${prefix}delete <key>\``, {reply: message});
return;
} else{
var key = args[0];
try{
delete save[message.author.username][key];
} catch(e){
botChannel.send(`You don't have a saved message with the key \`${key}\``, {reply: message});
return;
}
fs.writeFile("save.json", JSON.stringify(save), "utf8", function(err){
if(err) throw err;
botChannel.send(`Your message \`${key}\` has been deleted! :tada:`, {reply: message});
});
}
});
}
},
"insult": {
"usage": "",
"description": "(NOT DONE) Call the bot to your voice channel to deliver a special insult",
"process": function(message, args){
botChannel.send("There are currently no insults :sob:", {reply: message});
}
},
"addsong": {
"usage": "<link>",
"description": "Adds a song to the song queue via a youtube link",
"process": function(message, args){
if(message.member.voiceChannel !== undefined){
addSong(message, args[0]);
} else{
botChannel.send("You can't hear my music if you're not in a voice channel :cry:", {reply: message});
}
}
},
"yt": {
"usage": "<query>",
"description": "Searches for a youtube video to add to the song queue",
"process": function(message, args){
if(message.member.voiceChannel !== undefined){
if(args.length > 0){
var query = "";
for(var i = 0; i < args.length - 1; i++){
query += args[i] + " ";
}
query += " " + args[args.length - 1];
youtube.search.list({
key: process.env.GOOGLEAPIKEY,
q: query,
type: "video",
maxResults: "1",
part: "snippet"
}).then(res => {
if(res.data.items.length){
addSong(message, "https://www.youtube.com/watch?v=" + res.data.items[0].id.videoId);
} else {
message.reply(`There were no results for \`${query}\``);
}
}).catch(error => {
botChannel.send("There was an error searching for your song :cry:", {reply: message});
console.log("Error: " + err);
});
} else{
botChannel.send(`You can search for a youtube song with \`${prefix}yt <query>\``, {reply: message});
}
} else{
botChannel.send("You can't hear my music if you're not in a voice channel :cry:", {reply: message});
}
}
},
"play": {
"usage": "",
"description": "Resumes the current song",
"process": function(message, args){
if(message.member.voiceChannel !== undefined){
if(songQueue.length > 0){
if(dispatcher.paused){
dispatcher.resume();
botChannel.send("Song resumed! :play_pause:", {reply: message});
} else{
botChannel.send("Song is already playing", {reply: message});
}
} else{
botChannel.send("No song is in the queue", {reply: message});
}
} else{
botChannel.send("You can't hear my music if you're not in a voice channel :cry:", {reply: message});
}
}
},
"pause": {
"usage": "",
"description": "Pauses the current song",
"process": function(message, args){
if(message.member.voiceChannel !== undefined){
if(songQueue.length > 0){
if(!dispatcher.paused){
dispatcher.pause();
botChannel.send("Song paused! :pause_button:", {reply: message});
} else{
botChannel.send("Song is already paused", {reply: message});
}
} else{
botChannel.send("No song is in the queue", {reply: message});
}
} else{
botChannel.send("You can't hear my music if you're not in a voice channel :cry:", {reply: message});
}
}
},
"prev": {
"usage": "<amount>",
"description": "Skips back in the queue by a certain amount of songs",
"process": function(message, args){
if(message.member.voiceChannel !== undefined){
if(songQueue.length > 0){
previousSongIndex = currentSongIndex;
var amount = Number.parseInt(args[0]);
if(Number.isInteger(amount)){
currentSongIndex -= amount;
} else{
currentSongIndex--;
}
if(currentSongIndex < 0){
currentSongIndex = 0;
}
dispatcher.end("prev");
} else{
botChannel.send("There are no more songs :sob:", {reply: message});
}
} else{
botChannel.send("You can't hear my music if you're not in a voice channel :cry:", {reply: message});
}
}
},
"next": {
"usage": "<amount>",
"description": "Skips ahead in the queue by a certain amount of songs",
"process": function(message, args){
if(message.member.voiceChannel !== undefined){
if(songQueue.length > 0){
previousSongIndex = currentSongIndex;
var amount = Number.parseInt(args[0]);
if(Number.isInteger(amount)){
currentSongIndex += amount;
} else{
currentSongIndex++;
}
if(currentSongIndex > songQueue.length - 1){
currentSongIndex = songQueue.length - 1;
//bot.user.setGame(currentSong.title);
//Workaround since above wouldn't work
bot.user.setPresence({ game: { name: "", type: 0 } });
message.member.voiceChannel.leave();
botChannel.send("Finished playing the song queue");
}
dispatcher.end("next");
} else{
botChannel.send("There are no more songs :sob:", {reply: message});
}
} else{
botChannel.send("You can't hear my music if you're not in a voice channel :cry:", {reply: message});
}
}
},
"goto": {
"usage": "<index>",
"description": "Skips to a certain song in the queue by its index",
"process": function(message, args){
if(message.member.voiceChannel !== undefined){
if(songQueue.length > 0){
var index = Number.parseInt(args[0]);
if(Number.isInteger(index)){
previousSongIndex = currentSongIndex;
currentSongIndex = index - 1;
if(currentSongIndex < 0){
currentSongIndex = 0;
} else if(currentSongIndex > songQueue.length - 1){
currentSongIndex = songQueue.length - 1;
}
dispatcher.end("goto");
} else{
botChannel.send(`\`${args[0]}\` is an invalid index`, {reply: message});
}
} else{
botChannel.send("There are no more songs :sob:", {reply: message});
}
} else{
botChannel.send("You can't hear my music if you're not in a voice channel :cry:", {reply: message});
}
}
},
"random": {
"usage": "",
"description": "Chooses a random song from the queue to play.",
"process": function(message, args){
if(message.member.voiceChannel !== undefined){
if(songQueue.length > 0){
currentSongIndex = Math.floor(Math.random() * songQueue.length);
dispatcher.end("random");
} else{
botChannel.send("There are no more songs :sob:", {reply: message});
}
} else{
botChannel.send("You can't hear my music if you're not in a voice channel :cry:", {reply: message});
}
}
},
"clear": {
"usage": "<index>",
"description": "Clears the song queue or a specific song in the queue",
"process": function(message, args){
if(message.member.voiceChannel !== undefined){
if(songQueue.length === 0){
botChannel.send("There are no songs to clear", {reply: message});
} else if(args.length > 0){
var index = Number.parseInt(args[0]);
if(Number.isInteger(index)){
botChannel.send(`\`${songQueue[index - 1].title}\` has been removed from the song queue`, {reply: message});
songQueue.splice(index - 1, 1);
if(index - 1 <= currentSongIndex){
currentSongIndex--;
}
} else{
botChannel.send(`\`${args[0]}\` is an invalid index`, {reply: message});
}
} else{
dispatcher.end("clear");
currentSongIndex = 0;
songQueue = [];
//bot.user.setGame(currentSong.title);
//Workaround since above wouldn't work
bot.user.setPresence({ game: { name: "", type: 0 } });
message.member.voiceChannel.leave();
botChannel.send("The song queue has been cleared", {reply: message});
}
} else{
botChannel.send("You can't hear my music if you're not in a voice channel :cry:", {reply: message});
}
}
},
"shuffle": {
"usage": "",
"description": "Toggles shuffling of the song queue",
"process": function(message, args){
if(message.member.voiceChannel !== undefined){
if(shuffle){
shuffle = false;
botChannel.send("Shuffle is now disabled", {reply: message});
} else{
shuffle = true;
botChannel.send("Shuffle is now enabled", {reply: message});
}
} else{
botChannel.send("You can't hear my music if you're not in a voice channel :cry:", {reply: message});
}
}
},
"autoremove": {
"usage": "",
"description": "Toggles autoremoving songs of the song queue",
"process": function(message, args){
if(message.member.voiceChannel !== undefined){
if(autoremove){
autoremove = false;
botChannel.send("Song autoremoval is now disabled", {reply: message});
} else{
autoremove = true;
botChannel.send("Song autoremoval is now enabled", {reply: message});
}
} else{
botChannel.send("You can't hear my music if you're not in a voice channel :cry:", {reply: message});
}
}
},
"song": {
"usage": "",
"description": "Gives you information about the currently playing song",
"process": function(message, args){
if(songQueue.length > 0){
botChannel.send(`The current song is \`${songQueue[currentSongIndex].title}\` :musical_note:, added by ${songQueue[currentSongIndex].user}`, {reply: message});
} else{
botChannel.send("No song is in the queue", {reply: message});
}
}
},
"music": {
"usage": "",
"description": "Gives you a list of the songs currently in the queue",
"process": function(message, args){
if(songQueue.length > 0){
var songList = "";
for(var i = 0; i < songQueue.length; i++){
if(i === currentSongIndex){
songList += `__**\`${i + 1}. ${songQueue[i].title}\`**__\n`;
} else{
songList += `\`${i + 1}. ${songQueue[i].title}\`\n`;
}
}
botChannel.send("The song queue currently has:\n" + songList, {reply: message});
} else{
botChannel.send("No song is in the queue", {reply: message});
}
}
}
};
var addSong = function(message, url){
ytdl.getInfo(url).then(function(info){
var song = {};
song.title = info.title;
song.url = url;
song.user = message.author.username;
songQueue.push(song);
botChannel.send(`I have added \`${info.title}\` to the song queue! :headphones:`, {reply: message});
if(!bot.voiceConnections.exists("channel", message.member.voiceChannel)){
message.member.voiceChannel.join().then(function(connection){
playSong(message, connection);
}).catch(console.log);
}
}).catch(function(err){
botChannel.send("Sorry I couldn't get info for that song :cry:", {reply: message});
});
}
var playSong = function(message, connection){
if(shuffle){
do {
currentSongIndex = Math.floor(Math.random() * songQueue.length);
} while(currentSongIndex === previousSongIndex);
}
var currentSong = songQueue[currentSongIndex];
var stream = ytdl(currentSong.url, {"filter": "audioonly"});
dispatcher = connection.playStream(stream);
botChannel.send(`Now ${(shuffle) ? "randomly " : ""}playing \`${currentSong.title}\` :musical_note:, added by ${currentSong.user}`);
//bot.user.setGame(currentSong.title);
//Workaround since above wouldn't work
bot.user.setPresence({ game: { name: currentSong.title, type: 0 } });
dispatcher.player.on("warn", console.warn);
dispatcher.on("warn", console.warn);
dispatcher.on("error", console.error);
dispatcher.once("end", function(reason){
console.log("Song ended because: " + reason);
if(reason === "user" || reason === "Stream is not generating quickly enough."){
if(autoremove){
songQueue.splice(currentSongIndex, 1);
if(songQueue.length === 0){
//bot.user.setGame(currentSong.title);
//Workaround since above wouldn't work
bot.user.setPresence({ game: { name: "", type: 0 } });
message.member.voiceChannel.leave();
} else{
setTimeout(function(){
playSong(message, connection);
}, 500);
}
} else{
currentSongIndex++;
if(currentSongIndex >= songQueue.length && !shuffle){
//bot.user.setGame(currentSong.title);
//Workaround since above wouldn't work
bot.user.setPresence({ game: { name: "", type: 0 } });
message.member.voiceChannel.leave();
botChannel.send("Finished playing the song queue");
} else{
setTimeout(function(){
playSong(message, connection);
}, 500);
}
}
} else if(reason === "prev" || reason === "next" || reason === "goto" || reason === "random"){
setTimeout(function(){
playSong(message, connection);
}, 500);
}
});
}
var checkForCommand = function(message){
if(!botChannel){
botChannel = message.guild.channels.find("name", botChannelName);
}
if(!message.author.bot && message.content.startsWith(prefix)){
if(botChannel){
var args = message.content.substring(1).split(" ");
var command = args.splice(0, 1);
try{
commands[command].process(message, args);
} catch(e){
botChannel.send("Sorry, that isn't a command yet :sob:", {reply: message});
botChannel.send(`You can type \`${prefix}help\` to see a list of my commands`);
}
} else{
message.channel.send(`Please create a \`${botChannelName}\` channel`);
}
}
if(!message.author.bot){
var temp = "";
if(message.content.substring(0, 3).toLowerCase() === "im "){
temp = message.content.substring(3);
} else if(message.content.substring(0, 4).toLowerCase() === "i'm "){
temp = message.content.substring(4);
}
if(temp !== ""){
message.channel.send("Hi " + temp + ", I'm dad.", {reply: message});
}
}
}
bot.on("ready", function(){
console.log("Bot ready");
});
bot.on("disconnect", function(){
console.log("Bot disconnected");
process.exit(1);
});
bot.on("guildMemberAdd", function(member){
member.guild.defaultChannel.send(`Welcome to the server, ${member}! :smile:`);
member.guild.defaultChannel.send(`You can type \`${prefix}help\` at anytime to see my commands`);
});
bot.on("message", function(message){
checkForCommand(message);
});
bot.on("messageUpdate", function(oldMessage, newMessage){
checkForCommand(newMessage);
});
bot.login(process.env.BOTTOKEN).then(function(){
console.log("Bot logged in");
}).catch(console.log);
fs.readFile("save.json", function(err, data){
if(err){
if(err.code === "ENOENT"){
console.log("save.json does not exist");
fs.writeFile("save.json", "{}", "utf8", function(err){
if(err) throw err;
console.log("save.json created");
});
} else{
throw err;
}
}
});