-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspaghettatron.js
1088 lines (824 loc) · 26.1 KB
/
spaghettatron.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
/* Dependencies */
const Discord = require('discord.js')
const fs = require('fs')
const timers = require('timers')
const uuid = require('uuid/v1')
const download = require('download-file')
/* Imports */
const util = require('./util')
/* Configs */
const auth = require('./config/auth.json')
const config = require ('./config/config.json')
const events = require('./config/events.json')
/* Create Spaghettatron */
let Spaghettatron = new Discord.Client();
Spaghettatron.login(auth.token);
/* Custom Messages */
var messages, responses, command_redirects, searches, subreddits, requests, activities, commands, edits;
/* When Spaghettatron is ready */
Spaghettatron.on('ready', function() {
init();
console.log('Ready!');
Spaghettatron.user.setActivity(util.select(activities.activities));
});
/* When a message has been sent */
Spaghettatron.on('message', function(message) {
/* Spaghettatron will ignore his own messages */
if(message.author === Spaghettatron.user) {
return;
}
/* Log the message before it's manipulated */
log(message);
/* Content variable to use and manipulate */
var content = message.content;
/* Date */
var date = message.createdAt;
/*
* Replies:
* Respond to messages that don't have the prefix but start with something Spaghettatron recognizes
*/
for(var key in responses) {
if(content.toLowerCase().startsWith(key)) {
message.channel.send(util.select(responses[key]).format(message.author));
break;
}
}
/*
* Replacers:
* Replaces words (filter)
*/
for(var key in edits.replace) {
if(content.toLowerCase().includes(key)) {
var replace = '"' + content.replace(key, util.select(edits.replace[key])) + '"\n' + '-' + message.author.username;
message.delete()
.then(message.channel.send(replace))
.catch(console.error);
break;
}
}
/*
* Commands:
* Check to see if the message starts with the prefix defined in config.json
*/
if(content.startsWith(config.prefix)) {
/* Remove the prefix from "content" */
content = content.substring(config.prefix.length);
/* Get arguments with or without quotes from a string as an array */
var args = content.match(/[^\s"]+|"([^"]*)"/g);
/* Remove quotes from args with spaces */
args.forEach(function(item, index, array) {
if(item.startsWith('"') && item.includes(' ')) {
args[index] = item.slice(1,-1);
}
});
/*
* Make "command" the first argument, remove it from the real arguments
* This is used for general organization
*/
var command = args[0];
args.shift();
/* Create a simpler command object to parse */
var cmd = {
command: command,
args: args,
channel: message.channel,
sender: message.author
}
/* Execute the command and get the reply */
var reply = execute(cmd);
/* If there even was a reply */
if(reply) {
/* If the bot should @ the sender */
if(reply.at) {
message.reply(reply.message);
} else {
if(reply.message) {
message.channel.send(reply.message);
}
}
/* If the sender epic failed at trying to perform a command */
if(reply.fail) {
message.reply(util.select(messages.fail));
}
/* If the sender's message should be deleted */
if(reply.removeSent) {
message.delete();
}
/* Delete the message after how long */
if(reply.deleteAfter >= 0) {
message.delete(reply.deleteAfter);
}
if(reply.reactions) {
for(var i = 0; i < reply.reactions.length; i++) {
var addReaction = function() {message.react(reply.reactions[i])};
setTimeout(addReaction, 500);
}
}
}
}
});
/* When a message has been deleted */
Spaghettatron.on('messageDelete', function(message) {
if(!message.content.startsWith(config.prefix)) {
unlog(message);
}
});
/*
* Handle Commands
* Making it a seperate function to be used recursively or something
*/
function execute(cmd)
{
/* Setup essential variables */
var command = cmd.command;
var args = cmd.args;
var sender = cmd.sender;
/* Default Response */
var response = {
at: false,
message: util.select(messages.what),
fail: false,
removeSent: false,
deleteAfter: -1,
reactions: []
}
/* Redirects */
for(var i = 0; i < command_redirects.length; i++) {
var c = command_redirects[i];
if(command.equalsIgnoreCase(c.get)) {
command = c.set;
if(c.args) {
args = c.args.concat(args);
}
break;
}
}
/* Roll */
if(command.equalsIgnoreCase('roll') || command.equalsIgnoreCase('pick')) {
/* Default values */
var min = 1;
var max = 10;
/* If there is an argument */
if(args.length >= 1) {
/* If the first argument is a number */
if(!isNaN(args[0])) {
/* If the second argument is a number */
if(args[1] && !isNaN(args[1])) {
min = args[0];
max = args[1];
} else {
max = args[0];
}
} else {
/* If the first argument is "who" */
if (args[0].equalsIgnoreCase('who')) {
/* If the channel has enough members */
if(cmd.channel && cmd.channel.members && cmd.channel.members.size > 1) {
if(args.includes('-notme') || Math.random() >= 1.0 / cmd.channel.members.size) {
response.message = util.select(commands.roll.responses.person.other).format(cmd.channel.members.random().displayName);
} else {
response.message = util.select(commands.roll.responses.person.self);
}
} else {
response.message = util.select(commands.roll.responses.person.self);
}
}
/* Handle yes/no questions */
else if(commands.roll.yes_no.includes(args[0].toLowerCase())) {
var options = [commands.roll.responses.yes, commands.roll.responses.no];
if(!args.includes('-maybe')) options.push(commands.roll.responses.maybe)
response.message = util.select(util.select(options));
}
else {
if(args.length === 1) {
response.message = util.select(commands.roll.responses.one).format(args[0]);
} else {
response.message = util.select(commands.roll.responses.misc).format(util.select(args));
}
}
return response;
}
}
response.message = util.select(commands.roll.responses.numeric).format(util.random(min, max));
}
/* Reload JSON */
else if(command.equalsIgnoreCase('reload')) {
response.message = util.select(commands.reload.responses);
response.removeSent = true;
response.deleteAfter = 3000;
loadJSON();
}
/* Requests Info */
else if(command.equalsIgnoreCase('requests')) {
/* No arguments */
if(args.length === 0) {
var list = '';
for(var i = 0; i < requests.length; i++) {
list += '`!' + requests[i].name + '` *' + requests[i].messages.length + ' messages*\n';
}
response.message = list;
}
/* 1 argument */
else if(args.length === 1) {
var list= '';
key:
for(var i = 0; i < requests.length; i++) {
if(args[0].equalsIgnoreCase(requests[i].name)) {
item:
for(var j = 0; j < requests[i].messages.length; j++) {
var max = 15;
if(j <= max) {
list += '`' + requests[i].messages[j] + '`\n\n';
} else {
list += '*and ' + (requests[i].messages.length - max) + ' more...*';
break item;
}
}
response.message = list;
}
}
}
/* More than 1 argument */
else {
response.message = util.select(messages.fail);
}
}
/* Weather */
else if(command.equalsIgnoreCase('weather')) {
/* Default is 1-day weather data */
var days = 1;
/* Load weather.json for all of the users */
var file = util.resolvePath(config.weather_dir + '/weather.json');
var contents = fs.readFileSync(file);
var data = JSON.parse(contents);
var userData = {};
/* Get the object for the user data if applicable */
for(var obj in data) {
if(data[obj].id === sender.id) {
userData = data[obj];
}
}
var metric = userData.metric;
/* Create a new object for the user */
if(!data.includes(userData)) {
data.push(userData);
}
/* Add to the new object */
if(!userData.id) {
userData.id = sender.id;
}
/* Handle "now" weather conditions if the location is set */
if(args.length === 0) {
if(userData.location) {
response.message = undefined;
sendWeatherNow(userData.location, metric, cmd.channel);
} else {
response.message = util.select(commands.weather.responses.no_location);
}
}
/* Send weather either for a certain number of days or for a specified location (1 day) */
else if(args.length === 1) {
/* If location is set and "now" weather is requested */
if(args[0].equalsIgnoreCase('now')) {
if(userData.location) {
response.message = undefined;
sendWeatherNow(userData.location, metric, cmd.channel);
} else {
response.message = util.select(commands.weather.responses.no_location);
}
}
/* If location is set, and the argument is a number, display weather for that many days */
else if(!isNaN(args[0])) {
if(userData.location) {
response.message = undefined;
sendWeather(args[0], userData.location, metric, cmd.channel);
} else {
response.message = util.select(commands.weather.responses.no_location);
}
}
/* Send a 1 day weather report for specified location */
else {
response.message = undefined;
sendWeatherSlow(days, args[0], metric, cmd.channel);
}
}
/* Handle 2 arguments */
else if(args.length === 2) {
/* Save location */
if(args[0].equalsIgnoreCase('location')) {
var url = 'http://dataservice.accuweather.com/locations/v1/cities/search?apikey={0}&q={1}'.format(auth.accu_weather_api_key, args[1]);
response.message = undefined;
response.removeSent = true;
util.requestJSON(url, 'Spaghettatron')
.then(function(obj) {
/* Save to json */
userData.location = obj[0].Key;
util.write(JSON.stringify(data), file);
/* Send message */
cmd.channel.send(util.select(commands.weather.responses.location_set));
})
.catch(console.error);
}
/* Save metic preference */
else if(args[0].equalsIgnoreCase('unit')) {
if(args[1].charAt(0).equalsIgnoreCase('c') || args[1].charAt(0).equalsIgnoreCase('f')) {
userData.metric = args[1].charAt(0).equalsIgnoreCase('c');
util.write(JSON.stringify(data), file);
response.message = util.select(commands.weather.responses.unit_set).format(args[1].toUpperCase());
} else {
response.message = util.select(messages.fail);
}
}
/* Show "now" weather for a location */
else if(args[0].equalsIgnoreCase('now')) {
response.message = undefined;
sendWeatherNowSlow(args[0], metric, cmd.channel);
}
/* Show specific location weather */
else if(isNaN(args[0]) && !isNaN(args[1])) {
location = args[0];
days = args[1];
response.message = undefined;
sendWeatherSlow(days, location, metric, cmd.channel);
}
else {
response.message = util.select(messages.fail);
}
}
}
/* Help command */
else if(command.equalsIgnoreCase('help')) {
/* Display all help */
if(args.length === 0) {
response.message = undefined;
for(var key in commands) {
cmd.channel.send(getHelp(key));
}
}
/* Display help for specified command */
else if(args.length === 1) {
if(commands[args[0].toLowerCase()]) {
response.message = undefined;
cmd.channel.send(getHelp(args[0].toLowerCase()));
} else {
response.message = util.select(commands.help.responses.non_command).format(args[0]);
}
}
}
/* Subreddit commands */
else if(command.equalsIgnoreCase('subreddits') || command.equalsIgnoreCase('subreddit')) {
if(args.length === 0) {
var list = '';
for(var i = 0; i < subreddits.length; i++) {
var subreddit = subreddits[i];
list += '**' + config.prefix + subreddit.name + '** `r/' + (subreddit.url ? subreddit.url : subreddit.name) + '`' + (subreddit.nsfw ? ' *NSFW*' : '') + '\n';
}
response.message = list;
} else {
if(args[0].equalsIgnoreCase('add')) {
var subreddit = {};
if(args.length === 2) {
subreddit.name = args[1];
}
else if(args.length >= 3) {
subreddit.name = args[1];
subreddit.url = args[2];
if(args.includes('--nsfw')) {
subreddit.nsfw = true;
}
if(args.includes('--troll')) {
subreddit.troll = true;
}
} else {
response.message = util.select(messages.fail);
return response;
}
subreddits.push(subreddit);
response.message = util.select(commands.subreddits.responses.add_subreddit).format(subreddit.url ? subreddit.url : subreddit.name);
}
else if(args[0].equalsIgnoreCase('remove')) {
if(args.length === 2) {
var found = false;
for(var i = 0; i < subreddits.length; i++) {
if(subreddits[i].name === args[1]) {
found = true;
subreddits.splice(i, 1);
response.message = util.select(commands.subreddits.responses.delete_subreddit).format(args[1]);
break;
}
}
if(!found) response.message = util.select(commands.subreddits.responses.not_found).format(args[1]);
} else {
response.message = util.select(messages.fail);
return response;
}
}
for(var i = 0; i < subreddits.length; i++) {
subreddits[i].shown = undefined;
}
util.write(JSON.stringify(subreddits), './config/strings/subreddits.json');
}
}
/* Embed Searches */
else if(command.equalsIgnoreCase('searches')) {
if(args.length === 0) {
var list = '';
for(var i = 0; i < searches.length; i++) {
var searchEngine = searches[i];
list += '**' + config.prefix + searchEngine.name + '** `r/' + (searchEngine.url ? searchEngine.url : searchEngine.name) + '`' + '\n';
}
response.message = list;
} else {
if(args.length > 1) {
if(args.length === 2) {
if(args[0].equalsIgnoreCase('remove')) {
for(var i = 0; i < searches.length; i++) {
if(searches[i].name === args[1]) {
searches.splice(i, 1);
break;
}
}
}
}
else if(args.length === 3) {
if(args[0].equalsIgnoreCase('add')) {
searches.push({name: args[1], url: args[2]});
}
}
util.write(JSON.stringify(searches), './config/strings/searches.json');
}
}
}
/* Test command */
else if(command.equalsIgnoreCase('test')) {
response.message = 'test';
}
/* Handle other */
else {
/* Searches */
for(var i = 0; i < searches.length; i++) {
if(command.equalsIgnoreCase(searches[i].name)) {
response.message = searches[i].url + args.join('%20');
return response;
}
}
/* Subreddits */
for(var i = 0; i < subreddits.length; i++) {
if(command.equalsIgnoreCase(subreddits[i].name)) {
var subreddit = subreddits[i];
if(!subreddit.shown) {
subreddit.shown = [];
}
if(subreddit.troll) {
subreddit.url = commands.subreddits.troll_url;
}
util.requestJSON('http://reddit.com/r/' + (subreddit.url ? subreddit.url : subreddit.name) + '.json', 'Spaghettatron')
.then(function(obj) {
var posts = obj.data.children;
var imagePosts = [];
var nsfw = subreddit.nsfw || obj.data.whitelist_status === "promo_adult_nsfw";
for(var j = 0; j < posts.length; j++) {
if(posts[j].data.url && (util.isImage(posts[j].data.url) || util.isVideo(posts[j].data.url))) {
imagePosts.push(posts[j]);
}
}
if(subreddit.shown.length >= imagePosts.length) {
subreddit.shown = [];
}
var k = 0;
do {
k = Math.floor(Math.random() * imagePosts.length);
} while(subreddit.shown.includes(k))
subreddit.shown.push[k];
if(!nsfw) {
cmd.channel.send(imagePosts[k].data.url);
} else {
cmd.channel.send(util.select(commands.subreddits.responses.nsfw));
}
})
.catch(console.error);
response.message = null;
}
}
/* Requests */
for(var i = 0; i < requests.length; i++) {
if(command.equalsIgnoreCase(requests[i].name)) {
response.message = util.select(requests[i].messages);
response.removeSent = true;
if(args.length === 1) {
var index = args[0];
if(isNaN(index)) {
var items = [];
for(var i = 0; i < requests[i].messages.length; i++) {
var item = requests[i].messages[i];
if(item.toUpperCase().includes(index.toUpperCase())) {
items.push(item);
}
}
if(items.length > 0) {
response.message = (util.select(items));
}
} else {
if(!isNaN(index)) {
response.message = requests[i].messages[index];
}
}
}
return response;
}
}
}
return response;
}
/* Logging a chat message */
function log(message) {
/* Create a logged message */
var logged = toLoggedMessage(message);
/* Save to the text log */
util.addLine(logged.line + '\n', logged.dir + '/' + logged.file);
}
/* Un-log a chat message (delete from text log, but add to deleted log) */
function unlog(message) {
/* Do not unlog commands */
if(!message.content.startsWith(config.prefix)) {
/* Get the logged message */
var logged = toLoggedMessage(message);
/* Remove line from text log */
util.removeLine(logged.line, logged.dir + '/' + logged.file);
var dir = logged.dir + '/deleted/';
/* Save to the deleted log */
util.addLine(logged.line + '\n', dir + logged.file);
}
}
/* Create logged message object */
function toLoggedMessage(message) {
var date = message.createdAt;
var attachments = [];
var content = message.content;
/* If the message contained attachments, download them, store them, and log their names */
if(message.attachments && message.attachments.size > 0) {
Array.from(message.attachments.values()).forEach(function(item) {
attachments.push(item.filename);
var file = item.filename;
var extension = item.filename.substring(item.filename.indexOf('.'));
var dir = util.resolvePath(config.logs_dir + '/attachments/' + util.getMonthName(date));
if(extension.match(/.(jp(e)?g|png|gif)/g)) {
dir += '/images';
} else if(extension.match(/.(mp4|avi)/g)) {
dir += '/videos';
} else {
dir += '/misc';
}
var options = {
directory: dir,
filename: item.filename + '-' + uuid() + extension
};
download(item.url, options, function(err){
if (err) {
console.log(err);
}
});
});
content += '[' + attachments + ']';
}
/* Format the line */
var line = config.log_format
.replace('hh', date.getHours())
.replace('mm', date.getMinutes())
.replace('ss', date.getSeconds())
.replace('username', message.author.username)
.replace('message', content);
/* Format the directory */
dir = config.logs_dir + '/' + config.logs_structure
.replace('MM', util.getMonthName(date).toString())
.replace('mm', date.getMonth() + 1)
.replace('dd', date.getDate())
.replace('yyyy', date.getFullYear());
var obj = {
line: line,
attachments: attachments,
dir: dir,
file: message.channel.name + (content.startsWith(config.prefix) ? '-commands' : '') + '.log'
}
return obj;
}
/* Load all JSON files */
function loadJSON() {
var contents;
contents = fs.readFileSync('./config/strings/messages.json');
messages = JSON.parse(contents);
contents = fs.readFileSync('./config/strings/responses.json');
responses = JSON.parse(contents);
contents = fs.readFileSync('./config/strings/searches.json');
searches = JSON.parse(contents);
contents = fs.readFileSync('./config/strings/subreddits.json');
subreddits = JSON.parse(contents);
contents = fs.readFileSync('./config/strings/requests.json');
requests = JSON.parse(contents);
contents = fs.readFileSync('./config/strings/activities.json');
activities = JSON.parse(contents);
contents = fs.readFileSync('./config/strings/commands.json');
commands = JSON.parse(contents);
contents = fs.readFileSync('./config/strings/edits.json');
edits = JSON.parse(contents);
contents = fs.readFileSync('./config/strings/command_redirects.json');
command_redirects = JSON.parse(contents);
}
/* Init functions */
function init() {
/* Load the json into their object counterparts */
loadJSON();
/* Create files if they don't exist */
var weather = util.resolvePath(config.weather_dir + '/weather.json');
if (!fs.existsSync(weather)) {
util.write('[]', weather);
}
/* Special Days */
var date = new Date(Date.now());
var channel = Spaghettatron.channels.get(config.channel);
if(channel) {
for(var i in events.dates) {
var obj = events.dates[i];
var event = function() {
sendMessage(util.select(requests[select(obj.requests)]));
};
if(date.getMonth() + 1 === obj.month && date.getDate() === obj.day) {
if(obj.repeat === true) {
timers.setInterval(event, obj.delay);
} else {
timers.setTimeout(event, obj.delay);
}
}
}
}
}
/* Send a message to the preferred channel defined in config.json */
function sendMessage(message) {
Spaghettatron.channels.get(config.channel).send(message);
}
/* Send weather data to specified channel */
function sendWeather(days, id, metric, channel) {
var getDays = 1;
if(days > 1 && days <= 5) getDays = 5;
else if(days > 5 && days <= 10) getDays = 10;
else if(days > 10) getDays = 15;
var url = 'http://dataservice.accuweather.com/forecasts/v1/daily/{0}day/{1}?apikey={2}'.format(getDays, id, auth.accu_weather_api_key);
if(metric) {
url += '&metric=true';
}
util.requestJSON(url, 'Spaghettatron')
.then(function(obj) {
var embed = new Discord.RichEmbed()
.setThumbnail('https://docs.typo3.org/typo3cms/extensions/arx_accuweather/_images/Icon_AccuWeather.png')
.setColor(0xEF5414)
.setFooter(obj.Headline.Text)
for(var i = 0; i < days; i++) {
var forecast = obj.DailyForecasts[i];
var date = new Date(forecast.Date);
var content = '';
for(var index in commands.weather.display) {
if(!isNaN(index)) {
var line = commands.weather.display[index];
content += line
.replace('\{min_value\}', forecast.Temperature.Minimum.Value)
.replace('\{max_value\}', forecast.Temperature.Maximum.Value)
.replace('\{day\}', forecast.Day.IconPhrase)
.replace('\{night\}', forecast.Night.IconPhrase)
.replace('\{unit\}', forecast.Temperature.Maximum.Unit)
+ '\n';
}
}
embed.addField(days === 1 ? 'Today' : util.getDayName(date), content);
}
channel.send(embed);
})
.catch(console.error);
}
/* Send weather data from a city string */
function sendWeatherSlow(days, location, metric, channel) {
/* Set up URL */
var url = 'http://dataservice.accuweather.com/locations/v1/cities/search?apikey={0}&q={1}'.format(auth.accu_weather_api_key, location);
/* Get the ID */
util.requestJSON(url, 'Spaghettatron')
.then(function(obj) {
/* Send weather info on gotten ID */
sendWeather(days, obj[0].Key, metric, channel);
})
.catch(console.error);
}
/* Send current weather data */
function sendWeatherNow(id, metric, channel) {
/* Set up URL */
var url = 'http://dataservice.accuweather.com/currentconditions/v1/{0}?apikey={1}'.format(id, auth.accu_weather_api_key);
util.requestJSON(url, 'Spaghettatron')
.then(function(obj) {
var object = obj[0];
var icon = object.WeatherIcon < 10 ? '0' + object.WeatherIcon : object.WeatherIcon;
var embed = new Discord.RichEmbed()
.setThumbnail('https://developer.accuweather.com/sites/default/files/{0}-s.png'.format(icon))
.setColor(object.IsDayTime ? 0xFFD033 : 0x2C4555)
.setFooter(object.WeatherText)
var temperature = object.Temperature;
var date = new Date(object.LocalObservationDateTime);
var content = '';
for(var index in commands.weather.display_now) {
if(!isNaN(index)) {
var line = commands.weather.display_now[index];
content += line
.replace('\{value\}', metric ? temperature.Metric.Value : temperature.Imperial.Value)
.replace('\{unit\}', metric ? temperature.Metric.Unit : temperature.Imperial.Unit)
;
}
}
embed.addField('Current Weather Conditions', content);
channel.send(embed);
});
}
/* Send now weather data from a city string */
function sendWeatherNowSlow(location, metric, channel) {
/* Set up URL */
var url = 'http://dataservice.accuweather.com/locations/v1/cities/search?apikey={0}&q={1}'.format(auth.accu_weather_api_key, location);
/* Get the ID */
util.requestJSON(url, 'Spaghettatron')
.then(function(obj) {
/* Send weather info on gotten ID */
sendWeatherNow(obj[0].Key, metric, channel);
})
.catch(console.error);
}
/* Get a help embed */
function getHelp(command) {
var cmd = config.prefix + command;
var help = commands[command].help;
var embed = new Discord.RichEmbed()
.setColor(help.color)
.setFooter(util.select(commands.help.footer));
/* Add the description if applicable */
if(help.description) {
embed.setDescription(help.description);
}
for(var key in help.commands) {