forked from Dougley/DougleyBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord_bot.js
1901 lines (1729 loc) · 72.3 KB
/
discord_bot.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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
========================
This is a "ping-pong bot"
Everytime a message matches a command, the bot will respond.
========================
*/
var VersionChecker = require("./runtime/versioncheck");
var Cleverbot = require('cleverbot-node');
var cleverbot = new Cleverbot();
var getJSON = require('get-json')
var dateFromNum = require('date-from-num')
var ms = require('ms')
var ChatLog = require("./runtime/logger.js").ChatLog;
var Logger = require("./runtime/logger.js").Logger;
var maintenance;
var version = require("./package.json").version;
var Discord = require("discord.js");
var yt = require("./runtime/youtube_plugin");
var youtube_plugin = new yt();
var min = 1;
var max = 671;
var cmdPrefix = require("./config.json").command_prefix;
var aliases;
//Allowed send file types for !iff
var ext = [".jpg", ".jpeg", ".gif", ".png"];
var imgDirectory = require("./config.json").image_folder;
// Get the email and password
var ConfigFile = require("./config.json");
var qs = require("querystring");
var htmlToText = require('html-to-text');
var config = {
"api_key": "dc6zaTOxFJmzC",
"rating": "r",
"url": "http://api.giphy.com/v1/gifs/search",
"permission": ["NORMAL"]
};
var meme = require("./runtime/memes.json");
try {
var wa = require("./runtime/wolfram_plugin");
var wolfram_plugin = new wa();
} catch(e){
console.log("couldn't load wolfram plugin!\n"+e.stack);
}
var game_abbreviations = require("./runtime/abbreviations.json");
var cmdLastExecutedTime = {};
var admin_ids = require("./config.json").admin_ids;
/*
========================
Commands.
These are the commands, as described in the wiki, they can be adjusted to your needs.
None of the commands given here are required for the bot to run.
========================
*/
var commands = {
"gif": {
name: "gif",
description: "Returns a random gif matching the tags passed.",
extendedhelp: "I will search Giphy for a gif matching your tags.",
usage: "<image tags>",
process: function(bot, msg, suffix) {
var tags = suffix.split(" ");
get_gif(tags, function(id) {
if (typeof id !== "undefined") {
bot.sendMessage(msg.channel, "http://media.giphy.com/media/" + id + "/giphy.gif [Tags: " + (tags ? tags : "Random GIF") + "]");
} else {
bot.sendMessage(msg.channel, "Invalid tags, try something different. For example, something that exists [Tags: " + (tags ? tags : "Random GIF") + "]");
}
});
}
},
"maintenance-mode": {
name: "maintenance-mode",
description: "Enables maintenance mode.",
extendedhelp: "This will disable my command interpeter for a given amount of seconds, making me inable to execute commands.",
usage: "<time-in-seconds>",
adminOnly: true,
process: function(bot, msg, suffix) {
Logger.log("warn", "Maintenance mode activated for " + suffix + " seconds.");
bot.sendMessage(msg.channel, "The bot is now in maintenance mode, commands **will NOT** work!");
bot.setPlayingGame(525);
bot.setStatusIdle();
maintenance = "true";
setTimeout(continueExecution, Math.round(suffix * 1000));
function continueExecution() {
Logger.log("info", "Maintenance ended.");
bot.sendMessage(msg.channel, "Maintenance period ended, returning to normal.");
bot.setPlayingGame(308);
bot.setStatusOnline();
maintenance = null;
}
}
},
"info": {
name: "info",
description: "Tells a bit of info about the bot.",
extendedhelp: "I'll tell you some information about myself.",
process: function(bot, msg) {
var msgArray = [];
msgArray.push("Hello, I'm " + bot.user.username + ", a Discord bot.");
msgArray.push("I'm currently running on DougleyBot version " + version + ", which utilizes the latest version of Discord.js, an *unofficial* Discord libary by hydrabolt.");
msgArray.push("The developers of my source code are Dougley and Perpetucake, you can contact them via the test server.");
msgArray.push("https://discord.gg/0cFoiR5QVh4agupi");
msgArray.push("To see what I can do, use `" + ConfigFile.command_prefix + "help`");
msgArray.push("My invocation method is using prefixes, currently, I only respond to messages beginning with `" + ConfigFile.command_prefix + "`");
bot.sendMessage(msg.author, msgArray);
}
},
"ping": {
name: "ping",
description: "Responds pong, useful for checking if bot is alive.",
extendedhelp: "I'll reply to you with ping, this way you can see if I'm still able to take commands.",
process: function(bot, msg, suffix) {
bot.sendMessage(msg.channel, " " + msg.sender + " pong!");
if (suffix) {
bot.sendMessage(msg.channel, "note that !ping takes no arguments!");
}
}
},
"cleverbot": {
name: "cleverbot",
description: "Talk to Cleverbot!",
extendedhelp: "I'll act as Cleverbot when you execute this command, remember to enter a message as suffix.",
usage: "<message>",
process: function(bot, msg, suffix) {
Cleverbot.prepare(function() {
bot.startTyping(msg.channel);
cleverbot.write(suffix, function(response) {
bot.sendMessage(msg.channel, response.message);
bot.stopTyping(msg.channel);
});
});
}
},
"devs": {
name: "devs",
description: "Prints the devs of DougleyBot to the channel.",
extendedhelp: "This will print the Discord ID's from the developers of DougleyBot to the channel.",
process: function(bot, msg, suffix) {
bot.sendMessage(msg.channel, "Made with love by <@107904023901777920> and <@108125505714139136>. <3 <@110147170740494336> did stuff too.");
}
},
"status": {
name: "status",
description: "Prints the stats from the instance into the chat.",
extendedhelp: "This will print some information about the instance of the bot to the channel, like uptime and currently connected users.",
process: function(bot, msg, suffix) {
var msgArray = [];
msgArray.push("My uptime is " + (Math.round(bot.uptime / (1000 * 60 * 60))) + " hours, " + (Math.round(bot.uptime / (1000 * 60)) % 60) + " minutes, and " + (Math.round(bot.uptime / 1000) % 60) + " seconds.");
msgArray.push("Currently, I'm in " + bot.servers.length + " servers, and in " + bot.channels.length + " channels.");
msgArray.push("Currently, I'm serving " + bot.users.length + " users.");
msgArray.push("To Discord, I'm known as " + bot.user + ", and I'm running DougleyBot version " + version);
Logger.log("debug", msg.sender.username + " requested the bot status.");
bot.sendMessage(msg, msgArray);
}
},
"hello": {
name: "hello",
description: "Gives a friendly greeting, including github link.",
extendedhelp: "I'll respond to you with hello along with a GitHub link, handy!",
process: function(bot, msg) {
bot.sendMessage(msg.channel, "Hello " + msg.sender + "! I'm " + bot.user.username + ", help me grow by contributing to my GitHub: https://github.com/SteamingMutt/DougleyBot");
}
},
"server-info": {
name: "server-info",
description: "Prints the information of the current server.",
extendedhelp: "I'll tell you some information about the server and the channel you're currently in.",
process: function(bot, msg, suffix) {
// if we're not in a PM, return some info about the channel
if (msg.channel.server) {
var msgArray = [];
msgArray.push("You are currently in " + msg.channel + " (id: " + msg.channel.id + ")");
msgArray.push("on server **" + msg.channel.server.name + "** (id: " + msg.channel.server.id + ") (region: " + msg.channel.server.region + ")");
msgArray.push("owned by " + msg.channel.server.owner + " (id: " + msg.channel.server.owner.id + ")");
if (msg.channel.topic) {
msgArray.push("The current topic is: " + msg.channel.topic);
}
bot.sendMessage(msg, msgArray);
} else {
bot.sendMessage(msg, "You can't do that in a DM, dummy!.");
}
}
},
"birds": {
name: "birds",
description: "What are birds?",
extendedhelp: "The best stale meme evahr, IDST.",
process: function(bot, msg) {
var msgArray = [];
msgArray.push("https://www.youtube.com/watch?v=Kh0Y2hVe_bw");
msgArray.push("We just don't know");
bot.sendMessage(msg, msgArray);
}
},
"game": {
name: "game",
description: "Pings channel asking if anyone wants to play.",
extendedhelp: "I'll ask the channel you're currently in if they want to play the game you provide me, try some abbreviations, some might work!",
usage: "<name of game>",
process: function(bot, msg, suffix) {
var game = game_abbreviations[suffix];
if (!game) {
game = suffix;
}
if (!msg.channel.permissionsOf(bot.user).hasPermission("mentionEveryone")) {
bot.sendMessage(msg.channel, msg.sender + " would like to know if anyone is up for " + game);
Logger.log("debug", "Sent game invites without @everyone for " + game);
} else {
bot.sendMessage(msg.channel, "@everyone, " + msg.sender + " would like to know if anyone is up for " + game);
Logger.log("debug", "Sent game invites with @everyone for " + game);
}
}},
"servers": {
name: "servers",
description: "Lists servers bot is connected to.",
extendedhelp: "This will list all the servers I'm currently connected to, but if I'm in a lot of servers, don't expect a response.",
adminOnly: true,
process: function(bot, msg) {
bot.sendMessage(msg.channel, bot.servers);
}
},
"channels": {
name: "channels",
description: "Lists channels bot is connected to.",
extendedhelp: "This will list all the channels I'm currently connected to, but if I'm in a lot of channels, don't expect a response.",
adminOnly: true,
process: function(bot, msg) {
bot.sendMessage(msg.channel, bot.channels);
}
},
"myid": {
name: "myid",
description: "Returns the user id of the sender.",
extendedhelp: "This will print your Discord ID to the channel, useful if you want to define admins in your own instance.",
process: function(bot, msg) {
bot.sendMessage(msg.channel, msg.author.id);
}
},
"idle": {
name: "idle",
description: "Sets bot status to idle.",
extendedhelp: "This will change my status to idle.",
adminOnly: true,
process: function(bot, msg) {
bot.setStatusIdle();
Logger.log("debug", "My status has been changed to idle.");
}
},
"killswitch": {
name: "killswitch",
description: "Kills all running instances of DougleyBot.",
extendedhelp: "This will instantly terminate all of the running instances of the bot without restarting.",
adminOnly: true,
process: function(bot, msg) {
bot.sendMessage(msg.channel, "An admin has requested to kill all instances of DougleyBot, exiting...");
bot.logout();
Logger.log("warn", "Disconnected via killswitch!");
process.exit(0);
} //exit node.js without an error
},
"addmeme": {
name: "addmeme",
description: "Adds a meme.",
adminOnly: true,
extendedhelp: "Type !addmeme followed by text to add that text to the memelist.", //Just uses memes.txt in root folder.
process: function(bot, msg, suffix) {
var fs = require ("fs");
fs.appendFile('memes.txt', suffix + " ~END\n", function(err) {
});
bot.sendMessage(msg.channel, "Added '" + suffix + "' as a meme.");
}
},
"saymeme":{
name: "saymeme",
description: "Say a meme",
extendedhelp: "Makes the bot say a random meme from the meme list",
process: function(bot, msg) {
var fs = require ("fs");
fs.readFile('memes.txt', "utf8", function(err, fileContents) {
var lines = fileContents.split(" ~END\n");
bot.sendMessage(msg.channel, lines[Math.floor(Math.random()*lines.length) -1]);
});
}
},
"purge": {
name: "purge",
usage: "<number-of-messages-to-delete> [force]",
extendedhelp: "I'll delete a certain ammount of messages.",
process: function(bot, msg, suffix) {
if (!msg.channel.server) {
bot.sendMessage(msg.channel, "You can't do that in a DM, dummy!");
return;
}
if (!suffix){
bot.sendMessage(msg.channel, "Please define an ammount of messages for me to delete!");
return;
}
if (!msg.channel.permissionsOf(msg.sender).hasPermission("manageMessages")) {
bot.sendMessage(msg.channel, "Sorry, your permissions doesn't allow that.");
return;
}
if (!msg.channel.permissionsOf(bot.user).hasPermission("manageMessages")) {
bot.sendMessage(msg.channel, "I don't have permission to do that!");
return;
}
if (suffix.split(" ")[0] > 20 && msg.content != "force"){
bot.sendMessage(msg.channel, "I can't delete that much messages in safe-mode, add `force` to your message to force me to delete.");
return;
}
if (suffix.split(" ")[0] > 100){
bot.sendMessage(msg.channel, "The maximum is 100, 20 without `force`.");
return;
}
if (suffix.split(" ")[0] == "force"){
bot.sendMessage(msg.channel, "Please put `force` at the end of your message.");
return;
}
bot.getChannelLogs(msg.channel, suffix.split(" ")[0], function(error, messages){
if (error){
bot.sendMessage(msg.channel, "Something went wrong while fetching logs.");
return;
} else {
Logger.info("Beginning purge...");
var todo = messages.length,
delcount = 0;
for (msg of messages){
bot.deleteMessage(msg);
todo--;
delcount++;
if (todo === 0){
bot.sendMessage(msg.channel, "Done! Deleted " + delcount + " messages.");
Logger.info("Ending purge, deleted " + delcount + " messages.");
return;
}}
}
}
);}},
"kappa": {
name: "kappa",
description: "Kappa all day long!",
extendedhelp: "KappaKappaKappaKappaKappaKappaKappaKappaKappaKappa",
process: function(bot, msg, suffix) {
bot.sendFile(msg.channel, "./images/kappa.png");
if (msg.channel.server){
var bot_permissions = msg.channel.permissionsOf(bot.user);
if (bot_permissions.hasPermission("manageMessages")) {
bot.deleteMessage(msg);
return;
} else {
bot.sendMessage(msg.channel, "*This works best when I have the permission to delete messages!*");
}}
}
},
"iff": {
name: "iff",
description: "Send an image from the ./images/ directory!",
extendedhelp: "I'll send an image from the image directory to the chat.",
usage: "[image name] -ext",
process: function(bot, msg, suffix) {
var fs = require("fs");
var path = require("path");
var imgArray = [];
fs.readdir(imgDirectory, function(err, dirContents) {
for (var i = 0; i < dirContents.length; i++) {
for (var o = 0; o < ext.length; o++) {
if (path.extname(dirContents[i]) === ext[o]) {
imgArray.push(dirContents[i]);
}
}
}
if (imgArray.indexOf(suffix) !== -1) {
bot.sendFile(msg.channel, "./images/" + suffix);
if (msg.channel.server){
var bot_permissions = msg.channel.permissionsOf(bot.user);
if (bot_permissions.hasPermission("manageMessages")) {
bot.deleteMessage(msg);
return;
} else {
bot.sendMessage(msg.channel, "*This works best when I have the permission to delete messages!*");
}}
} else {
bot.sendMessage(msg.channel, "*Invalid input!*");
}
});
}
},
"imglist": {
name: "imglist",
description: "List's ./images/ dir!",
extendedhelp: "I'll list the images in the images directory for you, use them with the " + ConfigFile.command_prefix + "iff command!",
process: function(bot, msg, suffix) {
var fs = require("fs");
var path = require("path");
var imgArray = [];
fs.readdir(imgDirectory, function(err, dirContents) {
for (var i = 0; i < dirContents.length; i++) {
for (var o = 0; o < ext.length; o++) {
if (path.extname(dirContents[i]) === ext[o]) {
imgArray.push(dirContents[i]);
}
}
}
bot.sendMessage(msg.channel, imgArray);
});
}
},
"leave": {
name: "leave",
description: "Asks the bot to leave the current server.",
extendedhelp: "I'll leave the server in which the command is executed, you'll need the *Manage server* permission in your role to use this command.",
process: function(bot, msg, suffix) {
if (msg.channel.server) {
if (msg.channel.permissionsOf(msg.sender).hasPermission("manageServer")) {
bot.sendMessage(msg.channel, "Alright, see ya!");
bot.leaveServer(msg.channel.server);
Logger.log("info", "I've left a server on request of " + msg.sender.username + ", I'm only in " + bot.servers.length + " servers now.");
return;
} else {
bot.sendMessage(msg.channel, "Can't tell me what to do. (Your role in this server needs the permission to manage the server to use this command.)");
Logger.log("warn", "A non-privileged user (" + msg.sender.username + ") tried to make me leave a server.");
return;
}
} else {
bot.sendMessage(msg.channel, "I can't leave a DM, dummy!");
return;
}
}
},
"online": {
name: "online",
description: "Sets bot status to online.",
extendedhelp: "I'll change my status to online.",
adminOnly: true,
process: function(bot, msg) {
bot.setStatusOnline();
Logger.log("debug", "My status has been changed to online.");
}
},
"youtube": {
name: "youtube",
description: "Gets a Youtube video matching given tags.",
extendedhelp: "I'll search YouTube for a video matching your given tags.",
usage: "<video tags>",
process: function(bot, msg, suffix) {
youtube_plugin.respond(suffix, msg.channel, bot);
}
},
"say": {
name: "say",
description: "Copies text, and repeats it as the bot.",
extendedhelp: "I'll echo the suffix of the command to the channel and, if I have sufficient permissions, deletes the command.",
usage: "<text>",
process: function(bot, msg, suffix) {
if (suffix.search("!say") === -1) {
// bot.sendMessage(msg.channel, suffix, true + "-" + msg.author);
// This line makes no sense... it appears there is an attempt to add "-"+msg.author to the suffix, and true is supposed to enable the boolean /tts function. This command is useless if it adds the msg.author, so I'll just fix tts for now now lol
bot.sendMessage(msg.channel, suffix);
if (msg.channel.server){
var bot_permissions = msg.channel.permissionsOf(bot.user);
if (bot_permissions.hasPermission("manageMessages")) {
bot.deleteMessage(msg);
return;
} else {
bot.sendMessage(msg.channel, "*This works best when I have the permission to delete messages!*");
}}
} else {
bot.sendMessage(msg.channel, "HEY " + msg.sender + " STOP THAT!", {tts:"true"});
}
}
},
"tts": {
name: "tts",
description: "Same as say, tts",
extendedhelp: "SAAAAMMMEEE ASSSS SAAAAYYYYY, TTS",
usage: "<text>",
process: function(bot, msg, suffix) {
if (suffix.search("!say") === -1) {
bot.sendMessage(msg.channel, suffix, {tts:"true"});
if (msg.channel.server){
var bot_permissions = msg.channel.permissionsOf(bot.user);
if (bot_permissions.hasPermission("manageMessages")) {
bot.deleteMessage(msg);
return;
} else {
bot.sendMessage(msg.channel, "*This works best when I have the permission to delete messages!*");
}}
} else {
bot.sendMessage(msg.channel, "HEY " + msg.sender + " STOP THAT!", {tts:"true"});
}
}
},
"dankquote": {
name: "dankquote",
description: "Makes a dank quote and says it as the bot.",
extendedhelp: "Makes a dank quote and says it as the bot. This is the extended version, it is longer.",
usage: "<text>",
process: function(bot, msg, suffix) {
if (suffix.search("!say") === -1) {
var d = new Date();
bot.sendMessage(msg.channel,'"' + suffix + '"' + ' -' + msg.author + ' ' + d.getFullYear(), {tts:"true"});
if (msg.channel.server){
var bot_permissions = msg.channel.permissionsOf(bot.user);
if (bot_permissions.hasPermission("manageMessages")) {
bot.deleteMessage(msg);
return;
} else {
bot.sendMessage(msg.channel, "*This works best when I have the permission to delete messages!*");
}}
} else {
bot.sendMessage(msg.channel, "HEY " + msg.sender + " STOP THAT!", {tts:"true"});
}
}
},
"image": {
name: "image",
description: "Gets image matching tags from Google.",
extendedhelp: "I'll search teh interwebz for a picture matching your tags.",
usage: "<image tags>",
process: function(bot, msg, suffix) {
if(!ConfigFile || !ConfigFile.youtube_api_key || !ConfigFile.google_custom_search){
bot.sendMessage(msg.channel, "Image search requires both a YouTube API key and a Google Custom Search key!");
return;
}
//gets us a random result in first 5 pages
var page = 1 + Math.floor(Math.random() * 5) * 10; //we request 10 items
var request = require("request");
request("https://www.googleapis.com/customsearch/v1?key=" + ConfigFile.youtube_api_key + "&cx=" + ConfigFile.google_custom_search + "&q=" + (suffix.replace(/\s/g, '+')) + "&searchType=image&alt=json&num=10&start="+page, function(err, res, body) {
var data, error;
try {
data = JSON.parse(body);
} catch (error) {
Logger.error(error);
return;
}
if(!data){
Logger.debug(data);
bot.sendMessage(msg.channel, "Error:\n" + JSON.stringify(data));
return;
}
else if (!data.items || data.items.length === 0){
Logger.debug(data);
bot.sendMessage(msg.channel, "No result for '" + suffix + "'");
return;
}
var randResult = data.items[Math.floor(Math.random() * data.items.length)];
bot.sendMessage(msg.channel, randResult.title + '\n' + randResult.link);
});
Logger.log("debug", "I've looked for images of " + suffix + " for " + msg.sender.username);
}},
"pullanddeploy": {
name: "pullanddeploy",
description: "Bot will perform a git pull master and restart with the new code.",
extendedhelp: "I'll check if my code is up-to-date with the code from <@107904023901777920>, and restart. **Please note that this does NOT work on Windows!**",
adminOnly: true,
process: function(bot, msg, suffix) {
bot.sendMessage(msg.channel, "Fetching updates...", function(error, sentMsg) {
Logger.log("info", "Updating...");
var spawn = require('child_process').spawn;
var log = function(err, stdout, stderr) {
if (stdout) {
Logger.log("debug", stdout);
}
if (stderr) {
Logger.log("debug", stderr);
}
};
var fetch = spawn('git', ['fetch']);
fetch.stdout.on('data', function(data) {
Logger("debug", data.toString());
});
fetch.on("close", function(code) {
var reset = spawn('git', ['reset', '--hard', 'origin/master']);
reset.stdout.on('data', function(data) {
Logger.log("debug", data.toString());
});
reset.on("close", function(code) {
var npm = spawn('npm', ['install']);
npm.stdout.on('data', function(data) {
Logger.log("debug", data.toString());
});
npm.on("close", function(code) {
Logger.log("info", "Goodbye");
bot.sendMessage(msg.channel, "brb!", function() {
bot.logout(function() {
process.exit();
});
});
});
});
});
});
}
},
"meme": {
name: "meme",
extendedhelp: "I'll create a meme with your suffixes!",
usage: 'memetype "top text" "bottom text"',
process: function(bot, msg, suffix) {
var tags = msg.content.split('"');
var memetype = tags[0].split(" ")[1];
//bot.sendMessage(msg.channel,tags);
var Imgflipper = require("imgflipper");
var imgflipper = new Imgflipper(ConfigFile.imgflip_username, ConfigFile.imgflip_password);
imgflipper.generateMeme(meme[memetype], tags[1] ? tags[1] : "", tags[3] ? tags[3] : "", function(err, image) {
//Logger.log("debug", arguments);
bot.sendMessage(msg.channel, image);
if (msg.channel.server){
var bot_permissions = msg.channel.permissionsOf(bot.user);
if (bot_permissions.hasPermission("manageMessages")) {
bot.deleteMessage(msg);
return;
} else {
bot.sendMessage(msg.channel, "*This works best when I have the permission to delete messages!*");
}}
});
}
},
"log": {
name: "log",
description: 'Logs a message to the console.',
extendedhelp: "I'll log your message to the console.",
usage: '<log message>',
adminOnly: true,
process: function(bot, msg, suffix) {
Logger.log("debug", msg.content);
}
},
"whois": {
name: "whois",
description: "Gets info of a user.",
extendedhelp: "I'll fetch some info about the user you've mentioned.",
usage: '<user-mention>',
process: function(bot, msg, suffix) {
if (!msg.channel.server) {
bot.sendMessage(msg.author, "I can't do that in a DM, sorry.");
return;
}
if (msg.mentions.length === 0) {
bot.sendMessage(msg.channel, "Please mention the user that you want to get information of.");
return;
}
msg.mentions.map(function(user) {
var msgArray = [];
if (user.avatarURL === null) {
msgArray.push("Requested user: `" + user.username + "`");
msgArray.push("ID: `" + user.id + "`");
msgArray.push("Status: `" + user.status + "`");
bot.sendMessage(msg.channel, msgArray);
return;
} else {
msgArray.push("Requested user: `" + user.username + "`");
msgArray.push("ID: `" + user.id + "`");
msgArray.push("Status: `" + user.status + "`");
msgArray.push("Avatar: " + user.avatarURL);
bot.sendMessage(msg.channel, msgArray);
}
});
}
},
"wiki": {
name: "wiki",
description: "Returns the summary of the first matching search result from Wikipedia.",
extendedhelp: "I'll search Wikipedia for your requested subject, and return my finds.",
usage: "<search terms>",
timeout: 10, // In seconds
process: function(bot, msg, suffix) {
var query = suffix;
if (!query) {
bot.sendMessage(msg.channel, "usage: !wiki search terms");
return;
}
var Wiki = require('wikijs');
new Wiki().search(query, 1).then(function(data) {
new Wiki().page(data.results[0]).then(function(page) {
page.summary().then(function(summary) {
var sumText = summary.toString().split('\n');
var continuation = function() {
var paragraph = sumText.shift();
if (paragraph) {
bot.sendMessage(msg.channel, paragraph);
}
};
continuation();
});
});
}, function(err) {
bot.sendMessage(msg.channel, err);
});
}
},
"join-server": {
name: "join-server",
description: "Joins the server it's invited to.",
extendedhelp: "I'll join the server you've requested me to join, as long as the invite is valid and I'm not banned of already in the requested server.",
usage: "<bot-username> <instant-invite>",
process: function(bot, msg, suffix) {
suffix = suffix.split(" ");
if (suffix[0] === bot.user.username) {
Logger.log("debug", bot.joinServer(suffix[1], function(error, server) {
Logger.log("debug", "callback: " + arguments);
if (error || !server) {
Logger.warn("Failed to join a server: " + error);
bot.sendMessage(msg.channel, "Something went wrong, try again.");
} else {
var msgArray = [];
msgArray.push("Yo! I'm **" + bot.user.username + "**, " + msg.author + " invited me to this server.");
msgArray.push("If I'm intended to be in this server, you may use **" + ConfigFile.command_prefix + "help** to see what I can do!");
msgArray.push("If you don't want me here, you may use **" + ConfigFile.command_prefix + "leave** to ask me to leave.");
bot.sendMessage(server.defaultChannel, msgArray);
msgArray = [];
msgArray.push("Hey " + server.owner.username + ", I've joined a server in which you're the founder.");
msgArray.push("I'm " + bot.user.username + " by the way, a Discord bot, meaning that all of the things I do are mostly automated.");
msgArray.push("If you are not keen on having me in your server, you may use `" + ConfigFile.command_prefix + "leave` in the server I'm not welcome in.");
msgArray.push("If you do want me, use `" + ConfigFile.command_prefix + "help` to see what I can do.");
bot.sendMessage(server.owner, msgArray);
bot.sendMessage(msg.channel, "I've successfully joined **" + server.name + "**");
}
}));
} else {
Logger.log("debug", "Ignoring join command meant for another bot.");
}
}
},
"stock": {
name: "stock",
extendedhelp: "I'll search Yahoo! Finance for the price of the stock you've given me.",
usage: "<stockticker>",
process: function(bot, msg, suffix) {
var yahooFinance = require('yahoo-finance');
yahooFinance.snapshot({
symbol: suffix,
fields: ['s', 'n', 'd1', 'l1', 'y', 'r'],
}, function(error, snapshot) {
if (error) {
bot.sendMessage(msg.channel, "Couldn't get stock, it's behind lock, also, it's boorriiinngg: " + error);
} else {
//bot.sendMessage(msg.channel,JSON.stringify(snapshot));
bot.sendMessage(msg.channel, snapshot.name + "\nprice: $" + snapshot.lastTradePriceOnly);
}
});
}
},
"rss": {
name: "rss",
extendedhelp: "I'll list all of the avalible RSS feeds, just for you.",
description: "Lists available rss feeds",
process: function(bot, msg, suffix) {
/*var args = suffix.split(" ");
var count = args.shift();
var url = args.join(" ");
rssfeed(bot,msg,url,count,full);*/
bot.sendMessage(msg.channel, "Available feeds:", function() {
for (var c in rssFeeds) {
bot.sendMessage(msg.channel, c + ": " + rssFeeds[c].url);
}
});
}
},
"reddit": {
name: "reddit",
description: "Returns the top post on reddit. Can optionally pass a subreddit to get the top post there instead",
extendedhelp: "I'll fetch the top post from the top page of Reddit, and return the link, you can enter a specific subreddit as suffix and I'll post the first post from that subreddit.",
usage: "[subreddit]",
process: function(bot, msg, suffix) {
var path = "/.rss";
if (suffix) {
path = "/r/" + suffix + path;
}
rssfeed(bot, msg, "https://www.reddit.com" + path, 1, false);
}
},
"stroke": {
name: "stroke",
description: "Stroke someone's ego, best to use first and last name or split the name!",
extendedhelp: "I'll stroke someones ego, how nice of me.",
usage: "[First name][, [Last name]]",
process: function(bot, msg, suffix) {
var name;
if (suffix) {
name = suffix.split(" ");
if (name.length === 1) {
name = ["", name];
}
} else {
name = ["Perpetu", "Cake"];
}
var request = require('request');
request('http://api.icndb.com/jokes/random?escape=javascript&firstName=' + name[0] + '&lastName=' + name[1], function(error, response, body) {
if (!error && response.statusCode == 200) {
var joke = JSON.parse(body);
bot.sendMessage(msg.channel, joke.value.joke);
} else {
Logger.log("warn", "Got an error: ", error, ", status code: ", response.statusCode);
}
});
}
},
"yomomma": {
name: "yomomma",
description: "Returns a random Yo momma joke.",
extendedhelp: "I'll get a random yo momma joke for you.",
process: function(bot, msg, suffix) {
var request = require('request');
request('http://api.yomomma.info/', function(error, response, body) {
if (!error && response.statusCode == 200) {
var yomomma = JSON.parse(body);
bot.sendMessage(msg.channel, yomomma.joke);
} else {
Logger.log("warn", "Got an error: ", error, ", status code: ", response.statusCode);
}
});
}
},
"advice": {
name: "advice",
description: "Gives you good advice!",
extendedhelp: "I'll give you some great advice, I'm just too kind.",
process: function(bot, msg, suffix) {
var request = require('request');
request('http://api.adviceslip.com/advice', function(error, response, body) {
if (!error && response.statusCode == 200) {
var advice = JSON.parse(body);
bot.sendMessage(msg.channel, advice.slip.advice);
} else {
Logger.log("warn", "Got an error: ", error, ", status code: ", response.statusCode);
}
});
}
},
"yesno": {
name: "yesno",
description: "Answer yes or no with a gif (or randomly choose one!)",
extendedhelp: "Ever wanted a gif displaying your (dis)agreement? Then look no further!",
usage: "optional: [force yes/no/maybe]",
process: function(bot, msg, suffix) {
var request = require('request');
request('http://yesno.wtf/api/?force=' + suffix, function(error, response, body) {
if (!error && response.statusCode == 200) {
var yesNo = JSON.parse(body);
bot.sendMessage(msg.channel, msg.sender + " " + yesNo.image);
} else {
Logger.log("warn", "Got an error: ", error, ", status code: ", response.statusCode);
}
});
}
},
"urbandictionary": {
name: "urbandictionary",
description: "Search Urban Dictionary, one of the original AIDS of the internet!",
extendedhelp: "Every wanted to know what idiots on the internet thinks something means? Here ya go!",
usage: "[string]",
process: function(bot, msg, suffix) {
var request = require('request');
request('http://api.urbandictionary.com/v0/define?term=' + suffix, function(error, response, body) {
if (!error && response.statusCode == 200) {
var uD = JSON.parse(body);
if (uD.result_type !== "no_results") {
bot.sendMessage(msg.channel, suffix + ": " + uD.list[0].definition + ' "' + uD.list[0].example + '"');
} else {
bot.sendMessage(msg.channel, suffix + ": This is so screwed up, even Urban Dictionary doesn't have it in it's database");
}
} else {
Logger.log("warn", "Got an error: ", error, ", status code: ", response.statusCode);
}
});
}
},
"xkcd": {
name: "xkcd",
description: "Returns a random (or chosen) xkcd comic",
extendedhelp: "I'll get a XKCD comic for you, you can define a comic number and I'll fetch that one.",
usage: "[current, or comic number]",
process: function(bot, msg, suffix) {
var request = require('request');
request('http://xkcd.com/info.0.json', function(error, response, body) {
if (!error && response.statusCode == 200) {
var xkcdInfo = JSON.parse(body);
if (suffix) {
var isnum = /^\d+$/.test(suffix);
if (isnum) {
if ([suffix] < xkcdInfo.num) {
request('http://xkcd.com/' + suffix + '/info.0.json', function(error, response, body) {
if (!error && response.statusCode == 200) {
xkcdInfo = JSON.parse(body);
bot.sendMessage(msg.channel, xkcdInfo.img);
} else {
Logger.log("warn", "Got an error: ", error, ", status code: ", response.statusCode);
}
});
} else {
bot.sendMessage(msg.channel, "There are only " + xkcdInfo.num + " xkcd comics!");
}
} else {
bot.sendMessage(msg.channel, xkcdInfo.img);
}
} else {
var xkcdRandom = Math.floor(Math.random() * (xkcdInfo.num - 1)) + 1;
request('http://xkcd.com/' + xkcdRandom + '/info.0.json', function(error, response, body) {
if (!error && response.statusCode == 200) {
xkcdInfo = JSON.parse(body);
bot.sendMessage(msg.channel, xkcdInfo.img);
} else {
Logger.log("warn", "Got an error: ", error, ", status code: ", response.statusCode);
}
});
}
} else {
Logger.log("warn", "Got an error: ", error, ", status code: ", response.statusCode);
}
});
}
},
"8ball": {
name: "8ball",
description: "Makes executive decisions super easy!",
extendedhelp: "I'll function as an magic 8 ball for a bit and anwser all of your questions! (So long as you enter the questions as suffixes.)",
process: function(bot, msg, suffix) {
var request = require('request');
request('https://8ball.delegator.com/magic/JSON/0', function(error, response, body) {
if (!error && response.statusCode == 200) {
var eightBall = JSON.parse(body);
bot.sendMessage(msg.channel, eightBall.magic.answer + ", " + msg.sender);
} else {
Logger.log("warn", "Got an error: ", error, ", status code: ", response.statusCode);
}
});
}
},
"catfacts": {
name: "catfacts",
description: "Returns cool facts about cats!",
extendedhelp: "I'll give you some interresting facts about cats!",
process: function(bot, msg, suffix) {
var request = require('request');
request('http://catfacts-api.appspot.com/api/facts', function(error, response, body) {
if (!error && response.statusCode == 200) {
var catFact = JSON.parse(body);
bot.sendMessage(msg.channel, catFact.facts[0]);
} else {
Logger.log("warn", "Got an error: ", error, ", status code: ", response.statusCode);
}
});
}
},
"fact": {
name: "fact",
description: "Returns a random fact!",
extendedhelp: "I'll give you some interresting facts!",
process: function(bot, msg, suffix) {
var request = require('request');
var xml2js = require('xml2js');
request("http://www.fayd.org/api/fact.xml", function(error, response, body) {
if (!error && response.statusCode == 200) {
//Logger.log("debug", body)
xml2js.parseString(body, function(err, result) {
bot.sendMessage(msg.channel, result.facts.fact[0]);
});
} else {
Logger.log("warn", "Got an error: ", error, ", status code: ", response.statusCode);
}
});
}
},
"csgoprice": {
name: "csgoprice",
description: "Gives the price of a CSGO skin. Very picky regarding capitalization and punctuation.",
extendedhelp: "I'll give you the price of a CS:GO skin.",
usage: '[weapon "AK-47"] [skin "Vulcan"] [[wear "Factory New"] [stattrak "(boolean)"]] Quotes are important!',
process: function(bot, msg, suffix) {
skinInfo = suffix.split('"');
var csgomarket = require('csgo-market');
csgomarket.getSinglePrice(skinInfo[1], skinInfo[3], skinInfo[5], skinInfo[7], function(err, skinData) {
if (err) {
Logger.log('error', err);
bot.sendMessage(msg.channel, "That skin is so super secret rare, it doesn't even exist!");