-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathstart.js
4718 lines (4533 loc) · 231 KB
/
start.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
try {
// Get all the basic modules and files setup
const Discord = require("discord.js");
var configs = require("./data/config.json");
const AuthDetails = require("./auth.json");
var profileData = require("./data/profiles.json");
var stats = require("./data/stats.json");
var filter = require("./filter.json");
var reminders = require("./data/reminders.json");
// Hijack spawn for auto-update to work properly
(function() {
var childProcess = require("child_process");
childProcess.spawn = require("cross-spawn");
})();
// Misc. modules to make everything work
const writeFileAtomic = require("write-file-atomic");
const youtube_node = require("youtube-node");
const unirest = require("unirest");
const request = require("request");
const levenshtein = require("fast-levenshtein");
const htmlToText = require("html-to-text");
const qs = require("querystring");
const fs = require("fs");
const Wiki = require("wikijs");
const convert = require("convert-units");
const imgur = require("imgur-node-api");
var wolfram;
const urban = require("urban");
const weather = require("weather-js");
const fx = require("money");
const cheerio = require("cheerio");
const util = require("util");
const vm = require("vm");
const quotable = require("forbes-quote");
const readline = require("readline");
const searcher = require("google-search-scraper");
const urlInfo = require("url-info-scraper");
} catch(startError) {
console.log("Failed to start: ");
console.log(startError);
console.log("Exiting...");
process.exit(1);
}
// Bot setup
var version = "3.3.3";
var outOfDate = 0;
var readyToGo = false;
var logs = [];
var disconnects = 0;
// Set up message counter
var messages = {};
// Chatterbot setup, both Mitsuku and Cleverbot
var cleverOn = {};
const mitsuku = require("mitsuku-api")();
var bots = {};
const Cleverbot = require("cleverbot-node");
var cleverbot = new Cleverbot;
// Spam detection stuff
var spams = {};
// Stuff for ongoing polls, trivia games, reminders, and admin console sessions
var polls = {};
var trivia = {};
var adminconsole = {};
var admintime = {};
var updateconsole = false;
var maintainerconsole = false
var onlineconsole = {};
// Stuff for voting and lotteries
var novoting = {};
var pointsball = 20;
var lottery = {};
// List of bot commands along with usage and process for each
var commands = {
// Checks if bot is alive and shows version and uptime
"ping": {
extended: "A useful command to tell if the bot is alive. Also displays AwesomeBot version and status page URL if available.",
process: function(bot, msg) {
var info = "Pong! " + bot.user.username + " v" + version + " running for " + secondsToString(bot.uptime/1000);
if(configs.hosting!="") {
info = info.substring(0, info.length-1);
info += ". Status: " + configs.hosting;
}
bot.sendMessage(msg.channel, info);
}
},
// About AwesomeBot!
"about": {
extended: "Tells you all about the bot and where to get more info.",
process: function(bot, msg) {
bot.sendMessage(msg.channel, "Hello! I'm **" + bot.user.username + "**, here to help everyone on this server. A full list of commands and features is available with `@" + bot.user.username + " help`. You can PM me an invite link to add me to another server. To learn more, check out my GitHub page (https://git.io/vaa2F) or join the Discord server (https://discord.gg/0pRFCTcG2aIY53Jk)\n\n*v" + version + " by **@BitQuote**, made with NodeJS*");
}
},
// Shows top 5 games and active members
"stats": {
usage: "[clear]",
extended: "Shows the most active members, most played games, and most used commands on this server for this week. If you are an admin in this server, you can use the `clear` option to reset stats for this week.",
process: function(bot, msg, suffix) {
if(!stats[msg.channel.server.id]) {
logMsg(new Date().getTime(), "ERROR", msg.channel.server.name, msg.channel.name, "Failed to read stats");
bot.sendMessage(msg.channel, "Somehow, some way, I don't have any stats for this server :worried:");
return;
}
var data = getStats(msg.channel.server);
var info = "**" + msg.channel.server.name + " (this week)**"
for(var cat in data) {
info += "\n" + cat + ":" + (cat=="Data since" ? (" " + data[cat]) : "");
if(cat!="Data since") {
for(var i=0; i<data[cat].length; i++) {
info += "\n\t" + data[cat][i];
}
}
}
bot.sendMessage(msg.channel, info);
if(suffix.toLowerCase()=="clear" && configs.servers[msg.channel.server.id].admins.value.indexOf(msg.author.id)>-1) {
stats.timestamp = new Date().getTime();
clearServerStats(msg.channel.server.id);
logMsg(new Date().getTime(), "INFO", msg.channel.server.name, msg.channel.name, "Cleared stats for at admin's request");
}
}
},
// Gets Forbes Quote of the Day
"quote": {
extended: "Get the Forbes Quote of the Day as well as the author and URL.",
process: function(bot, msg) {
quotable().then(function (quote) {
bot.sendMessage(msg.channel, "`" + quote.quote + "`\n\t- " + quote.author + ": " + quote.url);
});
}
},
// Searches Google for a given query
"search": {
usage: "<query> [<count>]",
extended: "Searches Google for a given query. You can use the optional count parameter to specify the number of results to get (1-5).",
process: function(bot, msg, suffix) {
if(suffix) {
var query = suffix.substring(0, suffix.lastIndexOf(" "));
var count = parseInt(suffix.substring(suffix.lastIndexOf(" ")+1));
if(query=="" || !query || isNaN(count)) {
query = suffix;
count = 5;
}
if(count<1 || count>5) {
count = 5;
}
var options = {
query: query,
limit: count
};
var i = 0;
searcher.search(options, function(err, url) {
if(!err) {
urlInfo(url, function(error, linkInfo) {
if(i<count) {
i++;
if(!error) {
bot.sendMessage(msg.channel, "**" + linkInfo.title + "**\n" + url + "\n");
} else {
bot.sendMessage(msg.channel, url + "\n");
}
}
});
}
});
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "No search parameters");
bot.sendMessage(msg.channel, msg.author + " ???");
}
}
},
// Fetches Twitter user timelines
"twitter": {
usage: "<username> [<count>]",
extended: "Fetches the Twitter timeline for a given user. Do not include `@` in the username and use the optional count parameter to specify the number of tweets to get (1-5).",
process: function(bot, msg, suffix) {
if(suffix) {
var user = suffix.substring(0, suffix.indexOf(" "));
var count = parseInt(suffix.substring(suffix.indexOf(" ")+1));
if(user=="" || !user || isNaN(count)) {
user = suffix;
count = 0;
}
rssfeed(bot, msg, "http://twitrss.me/twitter_user_to_rss/?user=" + user, count, false);
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Twitter parameters not provided");
bot.sendMessage(msg.channel, msg.author + " You confuse me.");
}
}
},
// Gets YouTube link with given keywords
"youtube": {
usage: "<video tags>",
extended: "Gets a YouTube link with the given search terms. This includes channels, videos, and playlists.",
process: function(bot, msg, suffix) {
if(!suffix) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "User did not provide search term(s)");
bot.sendMessage(msg.channel, msg.author + " What should I search YouTube for?");
return;
}
ytSearch(suffix, function(link) {
bot.sendMessage(msg.channel, link);
});
}
},
// New Year Countdown
"year": {
extended: "The exact amount of time until next year!",
process: function(bot, msg) {
var a = new Date();
var e = new Date(a.getFullYear()+1, 0, 1, 0, 0, 0, 0);
var info = secondsToString((e-a)/1000) + "until " + (a.getFullYear()+1) + "!";
bot.sendMessage(msg.channel, info);
}
},
// Says something
"say": {
usage: "<something>",
process: function(bot, msg, suffix) {
if(!suffix) {
bot.sendMessage(msg.channel, "\t\n");
} else {
bot.sendMessage(msg.channel, msg.cleanContent.substring(bot.user.username.length+6));
}
}
},
// Allows approved users (essentially bot admins) to change chatterbot engine
"chatterbot": {
usage: "[switch]",
extended: "Displays the chatterbot currently in use, either Cleverbot or Mitsuku. Bot admins can use the `switch` option to toggle between these.",
process: function(bot, msg, suffix) {
if(configs.servers[msg.channel.server.id].admins.value.indexOf(msg.author.id)>-1) {
var isSwitch = suffix.toLowerCase() === "switch";
if (isSwitch) cleverOn[msg.channel.server.id] = !cleverOn[msg.channel.server.id];
var using = !cleverOn[msg.channel.server.id] ? "Mitsuku" : "Cleverbot";
if(isSwitch) {
logMsg(new Date().getTime(), "INFO", "Switched to " + using + " chatterbot");
bot.sendMessage(msg.channel, "Now using " + using + " for conversations.");
} else {
bot.sendMessage(msg.channel, "Currently using " + using + " for conversations.");
}
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "User is not a bot admin");
bot.sendMessage(msg.channel, msg.author + " Only my friends can do that.");
}
}
},
// Searches Google Images with keyword(s)
"image": {
usage: "<image tags> [random]",
extended: "Searches Google Images with a given query and returns the first result. Use the `random` option to get a random result instead.",
process: function(bot, msg, suffix) {
var numstr = "";
if(!suffix) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "User did not provide search term(s)");
bot.sendMessage(msg.channel, msg.author + " I don't know what image to get...");
return;
} else if(suffix.substring(suffix.lastIndexOf(" ")+1).toLowerCase()=="random") {
if(suffix.substring(0, suffix.lastIndexOf(" "))) {
suffix = suffix.substring(0, suffix.lastIndexOf(" "));
numstr = "&start=" + getRandomInt(0, 19);
}
}
giSearch(suffix, numstr, function(img) {
if(!img) {
bot.sendMessage(msg.channel, "Couldn't find anything, sorry");
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Image results not found for " + suffix)
} else {
bot.sendMessage(msg.channel, img);
}
});
}
},
// Get GIF from Giphy
"gif": {
usage: "<GIF tags>",
extended: "Gets a ~~meme~~ GIF fom Giphy with the given tags.",
process: function(bot, msg, suffix) {
if(!suffix) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "User did not provide GIF search term(s)");
bot.sendMessage(msg.channel, msg.author + " I don't know of a GIF for nothing.");
return;
}
var tags = suffix.split(" ");
var rating = "pg-13";
if(!configs.servers[msg.channel.server.id].nsfwfilter.value || !configs.servers[msg.channel.server.id].servermod.value) {
rating = "r";
}
getGIF(tags, function(id) {
if(typeof id!=="undefined") {
bot.sendMessage(msg.channel, "http://media.giphy.com/media/" + id + "/giphy.gif");
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "GIF not found for " + suffix);
bot.sendMessage(msg.channel, "The Internet has run out of memes :/");
}
}, rating);
}
},
// Defines word from Urban Dictionary
"urban": {
usage: "<term>",
extended: "Defines the given word. Source: Urban Dictionary",
process: function(bot, msg, suffix) {
var def = urban(suffix);
def.first(function(data) {
if(data) {
bot.sendMessage(msg.channel, "**" + suffix + "**: " + data.definition.replace("\r\n\r\n", "\n") + "\n*" + data.example.replace("\r\n\r\n", "\n") + "*\n`" + data.thumbs_up + " up, " + data.thumbs_down + " down`");
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Definition not found for " + suffix);
bot.sendMessage(msg.channel, "Wtf?! Urban Dictionary doesn't have an entry for " + suffix);
}
});
}
},
// Queries Wolfram Alpha
"wolfram" : {
usage: "<Wolfram|Alpha query>",
extended: "Displays an entire Wolfram|Alpha knowledge page about a given topic or person. This includes formulas, graphs, and more. May take some time to process.",
process(bot, msg, suffix) {
if(!suffix) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "User did not provide Wolfram|Alpha query");
bot.sendMessage(msg.channel, msg.author + " I'm confused...");
return;
}
wolfram.ask({query: suffix}, function(err, results) {
if(err) {
logMsg(new Date().getTime(), "ERROR", msg.channel.server.name, msg.channel.name, "Unable to connect to Wolfram|Alpha");
bot.sendMessage(msg.channel, "Unfortunately, I didn't get anything back from Wolfram|Alpha");
} else {
var info = ""
try {
for(var i=0; i<results.pod.length; i++) {
var fact = results.pod[i].subpod[0].plaintext[0] || results.pod[i].subpod[0].img[0].$.src;
info += "**" + results.pod[i].$.title + "**\n" + fact + "\n";
}
bot.sendMessage(msg.channel, info);
} catch(notFound) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Could not find Wolfram|Alpha data for " + suffix);
bot.sendMessage(msg.channel, "Wolfram|Alpha has nothing.");
}
}
});
}
},
// Gets Wikipedia article with given title
"wiki": {
usage: "<search terms>",
extended: "Shows the first three paragraphs of the Wikipedia article matching the given search terms. Make your query specific.",
process: function(bot, msg, suffix) {
if(!suffix) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "User did not provide Wikipedia search term(s)");
bot.sendMessage(msg.channel, msg.author + " You need to provide a search term.");
return;
}
new Wiki().search(suffix,1).then(function(data) {
if(data.results.length==0) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Wikipedia article not found for " + suffix);
bot.sendMessage(msg.channel, "I don't think Wikipedia has an article on that.");
return;
}
new Wiki().page(data.results[0]).then(function(page) {
page.summary().then(function(summary) {
if(summary.indexOf(" may refer to:") > -1 || summary.indexOf(" may stand for:") > -1) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Ambiguous search term '" + suffix + "' provided");
bot.sendMessage(msg.channel, "There are several matching Wikipedia articles; try making your query more specific.");
} else {
var sumText = summary.split("\n");
var count = 0;
var continuation = function() {
var paragraph = sumText.shift();
if(paragraph && count<3) {
count++;
bot.sendMessage(msg.channel, paragraph, continuation);
}
};
continuation();
}
});
});
}, function(err) {
logMsg(new Date().getTime(), "ERROR", msg.channel.server.name, msg.channel.name, "Unable to connect to Wikipedia");
bot.sendMessage(msg.channel, "Uhhh...Something went wrong :(");
});
}
},
// Converts between units
"convert": {
usage: "<no.> <unit> to <unit>",
extended: "Converts between units of measurement and currencies. Specify the quantity, starting unit, and end unit. The last two should be separated with ` to `",
process: function(bot, msg, suffix) {
var toi = suffix.lastIndexOf(" to ");
if(toi==-1) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "User used incorrect conversion syntax");
bot.sendMessage(msg.channel, msg.author + " Sorry, I didn't get that. Make sure you're using the right syntax: `@" + bot.user.username + " <no.> <unit> to <unit>`");
} else {
try {
var num = suffix.substring(0, suffix.indexOf(" "));
var unit = suffix.substring(suffix.indexOf(" ")+1, suffix.lastIndexOf(" to ")).toLowerCase();
var end = suffix.substring(suffix.lastIndexOf(" ")+1).toLowerCase();
if(isNaN(num)) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "User did not provide a numeric conversion quantity");
bot.sendMessage(msg.channel, msg.author + " That's not a number...");
return;
}
if(convert().possibilities().indexOf(unit)!=-1) {
if(convert().from(unit).possibilities().indexOf(end)!=-1) {
bot.sendMessage(msg.channel, (Math.round(convert(num).from(unit).to(end) * 1000) / 1000) + " " + end);
return;
}
}
try {
bot.sendMessage(msg.channel, (Math.round(fx.convert(num, {from: unit.toUpperCase(), to: end.toUpperCase()}) * 100) / 100) + " " + end.toUpperCase());
} catch(error) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Unsupported conversion unit(s)");
bot.sendMessage(msg.channel, msg.author + " I don't support that unit, try something else.");
}
} catch(err) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "User used incorrect convert syntax");
bot.sendMessage(msg.channel, msg.author + " Are you sure you're using the correct syntax?");
}
}
}
},
// Fetches stock symbol from Yahoo Finance
"stock": {
usage: "<stock symbol>",
extended: "Fetches basic information about a stock *symbol* from Yahoo! Finance.",
process: function(bot, msg, suffix) {
if(!suffix) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "User did not provide stock symbol");
bot.sendMessage(msg.channel, msg.author + " You never gave me a stock symbol! I'm not a magician, you know.");
return;
}
unirest.get("http://finance.yahoo.com/webservice/v1/symbols/" + suffix + "/quote?format=json&view=detail")
.header("Accept", "application/json")
.end(function(result) {
if(result.status==200 && JSON.parse(result.raw_body).list.resources[0]) {
var data = JSON.parse(result.raw_body).list.resources[0].resource.fields;
var info = data.issuer_name + " (" + data.symbol + ")\n\t$" + (Math.round((data.price)*100)/100) + "\n\t";
info += " " + (Math.round((data.change)*100)/100) + " (" + (Math.round((data.chg_percent)*100)/100) + "%)\n\t$" + (Math.round((data.day_low)*100)/100) + "-$" + (Math.round((data.day_high)*100)/100);
bot.sendMessage(msg.channel, info);
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Stock symbol " + suffix + " not found")
bot.sendMessage(msg.channel, "Sorry, I can't find that stock symbol.");
}
});
}
},
// Displays the weather for an area
"weather": {
usage: "<location> [<\"F\" or \"C\">]",
extended: "Gets the current weather and forecast for a given location from MSN Weather. Use the optional unit parameter (`F` or `C`) to change the unit (Fahrenheit is the default).",
process: function(bot, msg, suffix) {
var unit = "F";
var location = suffix;
if([" F", " C"].indexOf(suffix.substring(suffix.length-2))>-1) {
unit = suffix.charAt(suffix.length-1).toString();
location = suffix.substring(0, suffix.length-2);
}
weather.find({search: location, degreeType: unit}, function(err, data) {
if(err) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Could not find weather for location " + location);
bot.sendMessage(msg.channel, msg.author + " I can't find weather info for " + location);
} else {
data = data[0];
bot.sendMessage(msg.channel, "**" + data.location.name + " right now:**\n" + data.current.temperature + "°" + unit + " " + data.current.skytext + ", feels like " + data.current.feelslike + "°, " + data.current.winddisplay + " wind\n**Forecast for tomorrow:**\nHigh: " + data.forecast[1].high + "°, low: " + data.forecast[1].low + "° " + data.forecast[1].skytextday + " with " + data.forecast[1].precip + "% chance precip.");
}
});
}
},
// Silences the bot until the start statement is issued
"quiet": {
usage: "[all]",
extended: "Turns off the bot in this channel, or the whole server if you use the `all` option. You must be a bot admin in this server to use this command.",
process: function(bot, msg, suffix) {
if(configs.servers[msg.channel.server.id].admins.value.indexOf(msg.author.id)>-1 && suffix.toLowerCase()=="all") {
for(var i=0; i<msg.channel.server.channels; i++) {
stats[msg.channel.server.id].botOn[msg.channel.server.channels[i].id] = false;
}
} else if(configs.servers[msg.channel.server.id].admins.value.indexOf(msg.author.id)>-1) {
stats[msg.channel.server.id].botOn[msg.channel.id] = false;
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, msg.author.username + " is not a bot admin and cannot quiet bot");
bot.sendMessage(msg.channel,msg.author + " Sorry, I won't listen to you :P");
return;
}
bot.sendMessage(msg.channel, "Ok, I'll shut up.");
logMsg(new Date().getTime(), "INFO", msg.channel.server.name, msg.channel.name, "Bot has been quieted by an admin");
}
},
// Starts, ends, and answers live trivia game
"trivia": {
usage: "<start, end, next, or answer choice>",
extended: "AwesomeTrivia! *A fun group trivia game with really hard questions and a weird answer acceptance system.* Use `start` to begin playing, `next` to skip, and `end` to see your score.",
process: function(bot, msg, suffix) {
var triviaOn = trivia[msg.channel.id]!=null;
switch(suffix) {
case "start":
if(!triviaOn) {
logMsg(new Date().getTime(), "INFO", msg.channel.server.name, msg.channel.name, "Trivia game started");
trivia[msg.channel.id] = {answer: "", attempts: 0, score: 0, possible: 0};
bot.sendMessage(msg.channel, "Welcome to **AwesomeTrivia**! Here's your first question: " + triviaQ(msg.channel.id) + "\nAnswer by tagging me like this: `@" + bot.user.username + " trivia <answer>` or skip by doing this: `@" + bot.user.username + " trivia next`\nGood Luck!");
trivia[msg.channel.id].possible++;
if(!stats[msg.channel.server.id].commands.trivia) {
stats[msg.channel.server.id].commands.trivia = 0;
}
stats[msg.channel.server.id].commands.trivia++;
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Ongoing trivia game; new one cannot be started");
bot.sendMessage(msg.channel, "There's a trivia game already in progress on this server, in " + msg.channel.name);
}
break;
case "end":
if(triviaOn) {
var outof = trivia[msg.channel.id].possible-1;
if(trivia[msg.channel.id].possible==1) {
outof = 1;
}
logMsg(new Date().getTime(), "INFO", msg.channel.server.name, msg.channel.name, "Trivia game ended, score: " + trivia[msg.channel.id].score + " out of " + outof);
bot.sendMessage(msg.channel, "Thanks for playing! Y'all got " + trivia[msg.channel.id].score + " out of " + outof);
delete trivia[msg.channel.id];
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "No ongoing trivia game to end");
bot.sendMessage(msg.channel, "There isn't a trivia game going on right now. Start one by typing `@" + bot.user.username + " trivia start`");
}
break;
case "next":
if(triviaOn) {
logMsg(new Date().getTime(), "INFO", msg.channel.server.name, msg.channel.name, "Trivia question skipped by " + msg.author.username);
bot.sendMessage(msg.channel, "The answer was " + trivia[msg.channel.id].answer + "\n**Next Question:** " + triviaQ(msg.channel.id));
trivia[msg.channel.id].possible++;
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "No ongoing trivia game in which to skip question");
bot.sendMessage(msg.channel, "There isn't a trivia game going on right now. Start one by typing `@" + bot.user.username + " trivia start`");
}
break;
default:
if(triviaOn) {
if(levenshtein.get(suffix.toLowerCase(), trivia[msg.channel.id].answer.toLowerCase())<3 && triviaOn) {
logMsg(new Date().getTime(), "INFO", msg.channel.server.name, msg.channel.name, "Correct trivia game answer by " + msg.author.username);
bot.sendMessage(msg.channel, msg.author + " got it right! The answer is " + trivia[msg.channel.id].answer);
// Award AwesomePoints to author
if(!profileData[msg.author.id]) {
profileData[msg.author.id] = {
points: 0
};
}
profileData[msg.author.id].points += 5;
saveData("./data/profiles.json", function(err) {
if(err) {
logMsg(new Date().getTime(), "ERROR", "General", null, "Failed to save profile data for " + msg.author.username);
}
});
// Move on to next question
if(trivia[msg.channel.id].attempts<=2) {
trivia[msg.channel.id].score++;
}
trivia[msg.channel.id].attempts = 0;
bot.sendMessage(msg.channel, "**Next Question:** " + triviaQ(msg.channel.id));
trivia[msg.channel.id].possible++;
} else if(triviaOn) {
bot.sendMessage(msg.channel, msg.author + " Nope :(");
trivia[msg.channel.id].attempts++;
}
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "No ongoing trivia game to answer");
bot.sendMessage(msg.channel, "There isn't a trivia game going on right now. Start one by typing `@" + bot.user.username + " trivia start`");
}
}
}
},
// Sends reminders in given time for given note
"remindme": {
usage: "<no.> <\"d\", \"h\", \"m\", or \"s\"> <note>",
extended: "Set a reminder for yourself in a given number of days, hours, minutes, or seconds. You will be reminded via PM. You can also use a natural language command, for example: `remindme to do the dishes in 5 h`.",
process: function(bot, msg, suffix) {
parseReminder(suffix, msg.author, msg.channel);
}
},
// Gets top (max 5) posts in given subreddit, sorting hot
"reddit": {
usage: "<subreddit> [<count>]",
extended: "Gets the top 5 HOT posts in a given sub on Reddit. Use the optional `count` parameter to specify the number of posts to get (1-5).",
process: function(bot, msg, suffix) {
var path = "/.json"
var count = 5;
if(suffix) {
if(suffix.indexOf(" ")>-1) {
var sub = suffix.substring(0, suffix.indexOf(" "));
count = suffix.substring(suffix.indexOf(" ")+1);
if(count.indexOf(" ")>-1) {
count = count.substring(0, count.indexOf(" "));
}
path = "/r/" + sub + path;
} else {
path = "/r/" + suffix + path;
}
} else {
sub = "all";
count = 5;
}
if(!sub || !count || isNaN(count)) {
sub = suffix;
count = 5;
}
if(count<1 || count>5) {
count = 5;
}
unirest.get("https://www.reddit.com" + path)
.header("Accept", "application/json")
.end(function(result) {
if(result.body.data) {
var data = result.body.data.children;
var info = "";
var c = count;
for(var i=0; i<c; i++) {
if(!data[i] || !data[i].data || !data[i].data.score) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Subreddit not found or Reddit unavailable");
bot.sendMessage(msg.channel, "Surprisingly, I couldn't find anything in " + sub + " on reddit.");
return;
} else if(data[i].data.over_18 && configs.servers[msg.channel.server.id].admins.value.indexOf(msg.author.id)==-1 && configs.servers[msg.channel.server.id].nsfwfilter.value && configs.servers[msg.channel.server.id].servermod.value) {
logMsg(new Date().getTime(), "INFO", msg.channel.server.name, msg.channel.name, "Handling filtered query from " + msg.author.username);
kickUser(msg, "is abusing the bot", "attempting to fetch NSFW content");
if(configs.servers[msg.channel.server.id].points.value) {
if(!profileData[msg.author.id]) {
profileData[msg.author.id] = {
points: 0
}
}
profileData[msg.author.id].points -= 50;
saveData("./data/profiles.json", function(err) {
if(err) {
logMsg(new Date().getTime(), "ERROR", "General", null, "Failed to save profile data for " + msg.author.username);
}
});
}
return;
} else if(!data[i].data.stickied) {
info += "`" + data[i].data.score + "` " + data[i].data.title + " **" + data[i].data.author + "** *" + data[i].data.num_comments + " comments*";
info += ", https://redd.it/" + data[i].data.id + "\n";
} else {
c++;
}
}
bot.sendMessage(msg.channel, info);
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Subreddit not found or Reddit unavailable");
bot.sendMessage(msg.channel, "Surprisingly, I couldn't find anything in " + sub + " on reddit.");
}
});
}
},
// Gets top (max 5) posts in given RSS feed name
"rss": {
usage: "<site> [<count>]",
extended: "Gets entries in the given RSS feed. There are only certain feeds available; check the ones you can see in this server with the `help` command. Use the optional `count` parameter to specify the number of posts to get (1-5).",
process: function(bot, msg, suffix) {
if(configs.servers[msg.channel.server.id].rss.value[0]) {
var site = suffix.substring(0, suffix.indexOf(" "));
var count = parseInt(suffix.substring(suffix.indexOf(" ")+1));
if(site=="" || !site || isNaN(count)) {
site = suffix;
count = 0;
}
if(configs.servers[msg.channel.server.id].rss.value[2].indexOf(site.toLowerCase())>-1) {
rssfeed(bot,msg,configs.servers[msg.channel.server.id].rss.value[1][configs.servers[msg.channel.server.id].rss.value[2].indexOf(site.toLowerCase())], count, false);
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Feed " + site + " not found");
bot.sendMessage(msg.channel, msg.author + " Feed not found.");
}
}
}
},
// Generates a random number
"roll": {
usage: "[<min inclusive>] [<max inclusive>]",
extended: "Generate a random number. Without any parameters, this will roll a 6-sided die. You can also provide a minimum (inclusive) *and* maximum (inclusive).",
process: function(bot, msg, suffix) {
if(suffix.indexOf(" ")>-1) {
var min = suffix.substring(0, suffix.indexOf(" "));
var max = suffix.substring(suffix.indexOf(" ")+1);
} else if(!suffix) {
var min = 1;
var max = 6;
} else {
var min = 0;
var max = suffix;
}
var roll = getRandomInt(parseInt(min), parseInt(max));
if(isNaN(roll)) {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, msg.author.username + " provided nonsensical roll parameter");
bot.sendMessage(msg.channel, msg.author + " Wut.");
} else {
bot.sendMessage(msg.channel, msg.author + " rolled a " + parseInt(roll));
}
}
},
// Show list of games being played
"games": {
extended: "Lists the games being played on this server in descending order, along with the members playing them and the time played this week.",
process: function(bot, msg) {
var rawGames = {};
for(var i=0; i<msg.channel.server.members.length; i++) {
if(msg.channel.server.members[i].id!=bot.user.id && msg.channel.server.members[i].game && msg.channel.server.members[i].status!="offline") {
if(!rawGames[msg.channel.server.members[i].game.name]) {
rawGames[msg.channel.server.members[i].game.name] = [];
}
rawGames[msg.channel.server.members[i].game.name].push(msg.channel.server.members[i].username);
}
}
var games = [];
for(var game in rawGames) {
var playingFor;
if(stats[msg.channel.server.id].games[game]) {
playingFor = secondsToString(stats[msg.channel.server.id].games[game] * 3000) + "this week";
}
games.push([game, rawGames[game], playingFor]);
}
games.sort(function(a, b) {
return a[1].length - b[1].length;
});
var info = "";
for(var i=games.length-1; i>=0; i--) {
info += "**" + games[i][0] + "** (" + games[i][1].length + ")";
if(games[i][2]) {
info+="\n*" + games[i][2] + "*";
}
for(var j=0; j<games[i][1].length; j++) {
info += "\n\t@" + games[i][1][j];
}
info += "\n";
}
bot.sendMessage(msg.channel, info);
}
},
// Get a user's full profile
"profile": {
usage: "<username>",
extended: "The all-in-one command to view information about users. Providing no parameters will show your personal user profile on this server, including AwesomePoints, date joined, roles, and more. You can also provide a username as the parameter to view the profile of another member.",
process: function(bot, msg, suffix) {
var usr = msg.channel.server.members.get("username", suffix);
if(!suffix) {
usr = msg.author;
} else if(suffix.charAt(0)=="<") {
usr = msg.channel.server.members.get("id", suffix.substring(2, suffix.length-1));
}
if(usr) {
var data = getProfile(usr, msg.channel.server);
var info = "";
for(var sect in data) {
info += "**" + sect + ":**\n";
for(var key in data[sect]) {
info += "\t" + key + ": " + data[sect][key] + "\n";
}
}
bot.sendMessage(msg.channel, info);
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Requested member does not exist so profile cannot be shown");
bot.sendMessage(msg.channel, "That user doesn't exist :/");
}
}
},
// Quickly gets a user's points
"points": {
usage: "<username or \"lottery\">",
extended: "A quick way to get the number of points for a user. Use `me` if you want to see your own AwesomePoints, or use `lottery` to buy a PointsBall ticket!",
process: function(bot, msg, suffix) {
// Show points for user
var usr = msg.channel.server.members.get("username", suffix);
if(!suffix) {
var memberPoints = [];
for(var usrid in profileData) {
usr = msg.channel.server.members.get("id", usrid);
if(usr && profileData[usr.id].points>0) {
memberPoints.push([usr.username, profileData[usr.id].points]);
}
}
memberPoints.sort(function(a, b) {
return a[1] - b[1];
});
var info = "";
for(var i=memberPoints.length-1; i>=0; i--) {
info += "**@" + memberPoints[i][0] + "**: " + memberPoints[i][1] + " AwesomePoint" + (memberPoints[i][1]==1 ? "" : "s") + "\n";
}
bot.sendMessage(msg.channel, info);
return;
// PointsBall lottery game!
} else if(suffix=="lottery") {
// Start new lottery in server (winner in 60 minutes)
if(!lottery[msg.channel.server.id]) {
lottery[msg.channel.server.id] = {
members: [],
timestamp: new Date().getTime()
};
logMsg(new Date().getTime(), "INFO", msg.channel.server.name, msg.channel.name, "Lottery started, ends in 60 minutes");
setTimeout(function() {
var usrid = lottery[msg.channel.server.id].members[getRandomInt(0, lottery[msg.channel.server.id].members.length-1)];
var usr = msg.channel.server.members.get("id", usrid);
if(usr && !lottery[msg.channel.server.id].members.allValuesSame()) {
if(!profileData[usr.id]) {
profileData[usr.id] = {
points: 0,
}
}
if(pointsball>1000000) {
pointsball = 20;
}
profileData[usr.id].points += pointsball;
logMsg(new Date().getTime(), "INFO", msg.channel.server.name, msg.channel.name, usr.username + " won the lottery for " + pointsball);
saveData("./data/profiles.json", function(err) {
if(err) {
logMsg(new Date().getTime(), "ERROR", "General", null, "Failed to save profile data for " + usr.username);
}
});
bot.sendMessage(msg.channel.server.defaultChannel, "The PointsBall lottery amount is `" + pointsball + "` points, here's the winner..." + usr);
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "No winner of lottery for " + pointsball);
bot.sendMessage(msg.channel.server.defaultChannel, "The PointsBall lottery amount is `" + pointsball + "` points, here's the winner... NO ONE, rip");
}
delete lottery[msg.channel.server.id];
pointsball *= 2;
}, 3600000);
}
// Buy a lottery ticket
lottery[msg.channel.server.id].members.push(msg.author.id);
if(!profileData[msg.author.id]) {
profileData[msg.author.id] = {
points: 0,
}
}
profileData[msg.author.id].points -= 5;
logMsg(new Date().getTime(), "INFO", msg.channel.server.name, msg.channel.name, msg.author.username + " bought a lottery ticket");
bot.sendMessage(msg.channel, msg.author + " Thanks for buying a PointsBall ticket. That cost you 5 points. The lottery will end in " + secondsToString((lottery[msg.channel.server.id].timestamp + 3600000 - new Date().getTime())/1000));
saveData("./data/profiles.json", function(err) {
if(err) {
logMsg(new Date().getTime(), "ERROR", "General", null, "Failed to save profile data for " + msg.author.username);
}
});
return;
} else if(["me", "@me"].indexOf(suffix.toLowerCase())>-1) {
usr = msg.author;
} else if(suffix.charAt(0)=="<") {
usr = msg.channel.server.members.get("id", suffix.substring(2, suffix.length-1));
}
if(usr) {
if(!profileData[usr.id]) {
profileData[usr.id] = {
points: 0,
}
saveData("./data/profiles.json", function(err) {
if(err) {
logMsg(new Date().getTime(), "ERROR", "General", null, "Failed to save profile data for " + usr.username);
}
});
}
bot.sendMessage(msg.channel, "**@" + usr.username + "** has `" + profileData[usr.id].points + "` AwesomePoint" + (profileData[usr.id].points==1 ? "" : "s"));
} else {
logMsg(new Date().getTime(), "WARN", msg.channel.server.name, msg.channel.name, "Requested member does not exist so profile cannot be shown");
bot.sendMessage(msg.channel, "That user doesn't exist :confused:");
}
}
},
// Displays list of options and RSS feeds
"help": {
usage: "[<command name>]",
extended: "Shows the complete list of bot commands and features, specific to this server. You can include a command name as the parameter to get more information about it.",
process: function(bot, msg, suffix) {
if(!suffix) {
bot.sendMessage(msg.channel, "Tag me then state one of the following commands:" + getHelp(msg.channel.server));
} else {
bot.sendMessage(msg.channel, getCommandHelp(msg.channel.server, suffix.toLowerCase()));
}
}
}
};
var pmcommands = {
// Configuration options in wizard or online for maintainer and admins
"config": {
usage: "[<server>]",
extended: "Provides options to configure the bot overall or in a server. You must have privileges as the bot maintainer and/or admin in this server. Once you are authenticated, you will be " + (configs.hosting ? "given a link to the online interface for configuration" : "taken to a configuration wizard that allows you to enter configuration commands") + ".",
process: function(bot, msg, suffix) {
// Maintainer control panel for overall bot things
if(msg.author.id==configs.maintainer && !suffix && !maintainerconsole) {
logMsg(new Date().getTime(), "INFO", "General", null, "Maintainer console opened");
if(configs.hosting) {
if(!onlineconsole[msg.author.id] && !adminconsole[msg.author.id]) {
onlineconsole[msg.author.id] = {
token: genToken(30),
type: "maintainer",
timer: setTimeout(function() {
logMsg(new Date().getTime(), "INFO", "General", null, "Timeout on online maintainer console");
delete onlineconsole[msg.author.id];
}, 180000)
};
} else if(onlineconsole[msg.author.id]) {
bot.sendMessage(msg.channel, "You already have an online console session open. Logout of that first or wait 3 minutes...");
return;
} else if(adminconsole[msg.author.id]) {
bot.sendMessage(msg.channel, "One step at a time...Finish configuring this server, then come back later!");
return;
}
var url = (configs.hosting.charAt(configs.hosting.length-1)=='/' ? configs.hosting.substring(0, configs.hosting.length-1) : configs.hosting) + "?auth=" + onlineconsole[msg.author.id].token;
bot.sendMessage(msg.channel, url);
} else {
bot.sendMessage(msg.channel, "**Welcome to the " + bot.user.username + " maintainer console.** I am your owner. I will do what you say. Here are your options:\n\tquit\n\tgame <name of game or `.` to remove>\n\tusername <new name>\n\tstatus <online or idle>\n\tupdate\n\tkill\nUse the syntax `<option> <parameter>` as always! :)");
maintainerconsole = true;
}
return;
} else if(msg.author.id==configs.maintainer && maintainerconsole && !onlineconsole[msg.author.id]) {
var n = "";
var suffix = "";
if(msg.content.indexOf(" ")>-1) {
n = msg.content.substring(0, msg.content.indexOf(" ")).toLowerCase();
suffix = msg.content.substring(msg.content.indexOf(" ")+1);
} else {
n = msg.content.toLowerCase();
}
// Parse option and parameters
if(!n || ["quit", "game", "username", "status", "update", "kill"].indexOf(n)==-1) {
logMsg(new Date().getTime(), "WARN", "General", null, "Maintainer provided invalid option in console");
bot.sendMessage(msg.channel, "Invalid option, please see list above.");
return;
} else if((!suffix && ["game", "username", "status"].indexOf(msg.content)>-1) || (n=="status" && ["online", "idle"].indexOf(suffix)==-1)) {
logMsg(new Date().getTime(), "WARN", "General", null, "Maintainer provided invalid parameters in console");
bot.sendMessage(msg.channel, "Missing or incorrect parameter");
return;
}
switch(n) {
case "quit":
logMsg(new Date().getTime(), "INFO", "General", null, "Closed maintainer console");
bot.sendMessage(msg.channel, "Goodbye, master.");
maintainerconsole = false;
break;
case "game":
bot.setStatus("online", suffix);
if(suffix==".") {
suffix = "";
bot.setStatus("online", null);
}
logMsg(new Date().getTime(), "INFO", "General", null, "Set bot game to '" + suffix + "'");
configs.game = suffix;
saveData("./data/config.json", function(err) {
if(err) {
logMsg(new Date().getTime(), "ERROR", "General", null, "Could not save new config");
bot.sendMessage(msg.channel, "An unknown error occurred *saving* that change :crying_cat_face:");
} else {
bot.sendMessage(msg.channel, suffix=="" ? "Ok, removed game from status" : ("Ok, now I'm playing `" + suffix + "`"));
}
});
break;
case "username":
if(suffix==bot.user.username) {
logMsg(new Date().getTime(), "WARN", "General", null, "Maintainer provided existing username");
bot.sendMessage(msg.channel, "That's already my name! Haha");
return;
}
bot.setUsername(suffix, function(err) {
if(err) {
logMsg(new Date().getTime(), "ERROR", "General", null, "Failed to change username to " + suffix);
bot.sendMessage(msg.channel, "Uh-oh, something went wrong :o");
} else {
logMsg(new Date().getTime(), "INFO", "General", null, "Changed bot username to " + suffix);
bot.sendMessage(msg.channel, "Done!");
}
});
break;
case "status":
bot.setStatus(suffix, function(err) {
if(err) {
logMsg(new Date().getTime(), "ERROR", "General", null, "Failed to change status to " + suffix);
bot.sendMessage(msg.channel, "Discord is being weird, try again later");
} else {
logMsg(new Date().getTime(), "INFO", "General", null, "Changed bot status to " + suffix);
bot.sendMessage(msg.channel, "Ok, I am now `" + suffix + "`");
}
});
break;
case "update":
if(outOfDate>0) {