-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.js
More file actions
2074 lines (1695 loc) · 87.2 KB
/
commands.js
File metadata and controls
2074 lines (1695 loc) · 87.2 KB
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
/**
* @typedef {typeof Discord|typeof Tmi} Service
* @typedef {import("./user")} User
*/
const tz = require("timezone-js"),
tzData = require("tzdata"),
Db = require("./database"),
Exception = require("./exception"),
pjson = require("./package.json"),
Warning = require("./warning"),
forceHomeParse = /^<@!?([0-9]+)> (.+)$/,
forceReplaceHomeParse = /^<@!?([0-9]+)> (.+) with (.+)$/i,
idMessageParse = /^<@!?([0-9]+)> ([^ ]+)(?: (.+))?$/,
mergeParse = /^<@!?([0-9]+)> into <@!?([0-9]+)>$/,
addEventParse = /^([1-9][0-9]*) (.*) (\d{1,2}\/\d{1,2}\/(?:\d{2}|\d{4}) (?:1[012]|[1-9]):[0-5][0-9] [AP]M)$/i,
replaceHomeParse = /^(.+) with (.+)$/i,
reportAnarchyParse = /^ ?(?:<@!?(\d+)> (-?\d+))((?: <@!?\d+> -?\d+)*)$/,
reportGameParse = /^<@!?(\d+)> (-?\d+) <@!?(\d+)> (-?\d+)$/,
reportParse = /^(-?[0-9]+) (-?[0-9]+)$/,
twoIdParse = /^<@!?([0-9]+)> <@!?([0-9]+)>$/,
wrongReportParse = /^(-?[0-9]+)-(-?[0-9]+)$/;
/**
* @type {typeof import("./discord.js")}
*/
let Discord;
/**
* @type {typeof import("./event")}
*/
let Event;
/**
* @type {typeof import("./tmi")}
*/
let Tmi;
// ### #
// # # #
// # ### ## # ## # ### # ## ## # ###
// # # # # # # # # # # ## # # ## #
// # # # # # # # # # #### # # # # ###
// # # # # # # # # # # # # # # # ## #
// ### ### # # # # #### # # ## # ####
/**
* A class that handles commands given by chat.
*/
class Commands {
// # #
// # #
// ## ## ### ### ### ### # # ## ### ## ###
// # # # # # ## # # # # # # # # # # #
// # # # # # ## # # # # # # # # #
// ## ## # # ### ## # ### ## ## ## #
/**
* Initializes the class with the service to use.
* @param {Service} service The service to use with the commands.
*/
constructor(service) {
this.service = service;
if (!Discord) {
Discord = require("./discord");
}
if (!Tmi) {
Tmi = require("./tmi");
}
if (!Event) {
Event = require("./event");
}
Event.onLoad();
}
// # # # # ### ## # #
// # # # # # # # #
// ## ### ## ## # # # # ### ## ### # ### # # ### # # ## ###
// # # # # ## # ## # # ## # ## # # # ## #### # # #### # # #
// # # # ## # # # # # ## ## # # ## # # # # # # # # #
// ## # # ## ## # # ## ### ## # ### ### # # ### # # ### # #
/**
* Checks that the user is an admin.
* @param {User} user The user to check.
* @returns {void}
*/
static checkUserIsAdmin(user) {
if (!Discord.isOwner(user)) {
throw new Warning("Admin permission required to perform this command.");
}
}
// # # # # ### #### ### # #
// # # #### # # # # #
// ## ### ## ## # # #### ## ### ### ### ### ## # ### ### ### ## # # # # ## ### ## ## ### ###
// # # # # ## # ## # # # ## ## ## # # # # # ## # ## # # # # # #### # # # ## # # # # # # #
// # # # ## # # # # # ## ## ## # ## ## ## # ## # # # # # # # # # ## # # # # # #
// ## # # ## ## # # # # ## ### ### # # # ## ### ### # # ## # # ### ### ### ## ## # ###
// ###
/**
* Checks that the message is from Discord.
* @param {Service} service The service.
* @returns {void}
*/
static checkMessageIsFromDiscord(service) {
if (service.name !== Discord.name) {
throw new Warning("This command is for Discord only.");
}
}
// # # # # ### #### ### #
// # # #### # # #
// ## ### ## ## # # #### ## ### ### ### ### ## # ### ### ### ## # # # # # ##
// # # # # ## # ## # # # ## ## ## # # # # # ## # ## # # # # # #### # #### #
// # # # ## # # # # # ## ## ## # ## ## ## # ## # # # # # # # # # #
// ## # # ## ## # # # # ## ### ### # # # ## ### ### # # ## # # # # # ###
// ###
/**
* A promise that only proceeds if the user is on tmi.
* @param {Service} service The service.
* @returns {void}
*/
static checkMessageIsFromTmi(service) {
if (service.name !== Tmi.name) {
throw new Warning("This command is for Twitch chat only.");
}
}
// # ## #
// # #
// ### ## # # # # # ### ### ##
// ## # #### # # # # # # # ##
// ## # # # # # # # ## # ##
// ### ### # # ### ### # # ## ##
/**
* Simulates other users making a command.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise} A promise that resolves when the command completes.
*/
async simulate(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
Commands.checkUserIsAdmin(user);
if (!idMessageParse.test(message)) {
return false;
}
const matches = idMessageParse.exec(message),
{1: userId, 3: newMessage} = matches,
command = matches[2].toLocaleLowerCase();
if (Object.getOwnPropertyNames(Commands.prototype).filter((p) => typeof Commands.prototype[p] === "function" && p !== "constructor").indexOf(command) === -1) {
return false;
}
const newUser = Discord.getGuildUser(userId);
if (!newUser) {
throw new Warning("User does not exist.");
}
return await this[command](newUser, newMessage, channel) || void 0;
}
// #
//
// # # ## ### ### ## ## ###
// # # # ## # # ## # # # # #
// # # ## # ## # # # # #
// # ## # ### ### ## # #
/**
* Replies with the current version of the bot.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async version(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (message) {
return false;
}
await Discord.queue(`FusionBot, DescentBot, whatever, I have an identity crisis. Written by roncli, Version ${pjson.version}`, channel);
return true;
}
// # #
//
// # ## ## ###
// # # # # # #
// # # # # # #
// # # ## ### # #
// #
/**
* Joins the user to the event.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async join(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (message) {
return false;
}
if (!Event.isRunning) {
await Discord.queue(`Sorry, ${user}, but there is no event currently running.`, channel);
throw new Warning("No event currently running.");
}
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can join.`, channel);
throw new Warning("Not a joinable event.");
}
const guildUser = Discord.getGuildUser(user),
ratedPlayer = await Event.getRatedPlayerByName(guildUser.displayName);
if (ratedPlayer && user.discord.id !== ratedPlayer.DiscordID) {
await Discord.queue(`Sorry, ${user}, but I already have a record of you previously participating under another account. Please either log into the account you previously played under, or contact roncli to have your accounts merged.`);
throw new Warning("User made another account.");
}
const player = Event.getPlayer(user.discord.id);
if (player && !player.withdrawn) {
await Discord.queue(`Sorry, ${user}, but you have already joined this event. You can use \`!withdraw\` to leave it.`, channel);
throw new Warning("Already joined.");
}
let homes;
try {
homes = await Db.getHomesForDiscordId(user.discord.id);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was a database error getting a pilot's home maps.", err);
}
if (homes.length < 3) {
await Discord.queue(`Sorry, ${user}, but you have not yet set all 3 home maps. Please use the \`!home\` command to select 3 home maps, one at a time, for example, \`!home Logic x2\`.`, channel);
throw new Warning("Pilot has not yet set 3 home maps.");
}
if (player) {
Event.rejoinPlayer(player, homes);
} else {
Event.addPlayer(user.discord.id, homes);
}
await Discord.addEventRole(user);
await Discord.queue("You have been successfully added to the event. I assume you can host games, but if you cannot please issue the `!host` command to toggle this option.", channel);
await Discord.queue(`${guildUser.displayName} has joined the tournament!`);
try {
if (!await Event.getRatedPlayerById(user.discord.id)) {
await Event.addRatedPlayer(user.discord);
await Discord.queue(`${user} has joined the tournament, but there is no record of them participating previously. Ensure this is not an existing player using a new Discord account.`, Discord.alertsChannel);
}
} catch (err) {
throw new Exception("There was a database error while determining if this player exists.", err);
}
return true;
}
// # # # #
// # # #
// # # ## ### ### ### ### ### # #
// # # # # # # # # # # # # # #
// #### # # # # # # # # ## ####
// #### ### ## # # ### # # # ####
/**
* Withdraws the user from the event.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async withdraw(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (message) {
return false;
}
if (!Event.isRunning) {
await Discord.queue(`Sorry, ${user}, but there is no event currently running.`, channel);
throw new Warning("No event currently running.");
}
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can withdraw from.`, channel);
throw new Warning("Not a withdrawable event.");
}
const player = Event.getPlayer(user.discord.id);
if (!player) {
await Discord.queue(`Sorry, ${user}, but you have not yet joined this event. You can use \`!join\` to enter it.`, channel);
throw new Warning("Player has not entered.");
}
if (player.withdrawn) {
await Discord.queue(`Sorry, ${user}, but you have have already withdrawn from this event. You can use \`!join\` to re-enter it.`, channel);
throw new Warning("Player has already withdrew.");
}
try {
await Event.removePlayer(user.discord.id);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was a Discord error removing a pilot from the tournament.", err);
}
await Discord.queue("You have been successfully withdrawn from the event. If you wish to return before the end of the event, you may use the `!join` command once again.", user);
await Discord.queue(`${Discord.getGuildUser(user).displayName} has withdrawn from the tournament.`);
return true;
}
// #
// #
// ### ## # # ##
// # # # # #### # ##
// # # # # # # ##
// # # ## # # ##
/**
* Sets home maps.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async home(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (!message) {
return false;
}
let homeCount;
try {
homeCount = await Db.getHomeCountForDiscordId(user.discord.id);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was a database error getting the count of a pilot's home maps.", err);
}
if (homeCount >= 3) {
await Discord.queue(`Sorry, ${user}, but you have already set 3 home maps. If you haven't played a match yet, you can use \`!resethome\` to reset your home map selections.`, channel);
throw new Warning("Player already has 3 homes.");
}
let bannedHomes;
try {
bannedHomes = await Db.getBannedHomes(user.discord.id, Event.season);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was a database error getting a pilot's banned homes.", err);
}
for (const home of bannedHomes) {
const bannedHome = home.split(" ", 1)[0].toLowerCase(),
homeToCheck = message.split(" ", 1)[0].toLowerCase();
if (bannedHome === homeToCheck) {
await Discord.queue(`Sorry, ${user}, but \`${message}\` appears to be banned for this season. If you believe this is in error, please address this with @roncli.`, channel);
throw new Warning("Home map is banned.");
}
}
try {
await Db.addHome(user.discord.id, message);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was a database error setting a pilot's home map.", err);
}
homeCount++;
if (homeCount < 3) {
await Discord.queue(`You have successfully set one of your home maps to \`${message}\`. You may set ${3 - homeCount} more home map${3 - homeCount === 1 ? "" : "s"}. You can use \`!resethome\` at any point prior to playing a match to reset your home maps.`, user);
return true;
}
if (!Event.isRunning || Event.isFinals) {
await Discord.queue(`You have successfully set one of your home maps to \`${message}\`. Your maps for the season are now setup. You can use \`!resethome\` at any point prior to playing a match to reset your home maps.`, user);
await Discord.queue(`${user} has set their home maps, please check them against the ban list.`, Discord.alertsChannel);
return true;
}
const player = Event.getPlayer(user.discord.id);
if (!player) {
await Discord.queue(`You have successfully set one of your home maps to \`${message}\`. Your maps for the season are now setup. You can use \`!resethome\` at any point prior to playing a match to reset your home maps. You may now \`!join\` the current event.`, user);
await Discord.queue(`${user} has set their home maps, please check them against the ban list.`, Discord.alertsChannel);
return true;
}
let homes;
try {
homes = await Db.getHomesForDiscordId(user.discord.id);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was a database error getting a pilot's home maps.", err);
}
Event.setHomes(user.discord.id, homes);
await Discord.queue(`You have successfully set one of your home maps to \`${message}\`. Your maps for the season are now setup. You can use \`!resethome\` at any point prior to playing a match to reset your home maps.`, user);
return true;
}
// # #
// # #
// ### ## ### ## ### ### ## # # ##
// # # # ## ## # ## # # # # # #### # ##
// # ## ## ## # # # # # # # ##
// # ## ### ## ## # # ## # # ##
/**
* Resets home maps.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async resethome(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (message) {
return false;
}
let status;
try {
status = await Db.getResetStatusForDiscordId(user.discord.id);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was a database error getting whether a pilot's home maps are locked.", err);
}
if (!status.hasHomes) {
await Discord.queue(`Sorry, ${user}, but you haven't set any home maps yet. Please use the \`!home\` command to select 3 home maps, one at a time, for example, \`!home Logic x2\`.`, channel);
throw new Warning("Player has no home maps.");
}
if (status.locked) {
await Discord.queue(`Sorry, ${user}, but your home maps are set for the season.`, channel);
throw new Warning("Player's home maps are locked.");
}
try {
await Db.deleteHomesForDiscordId(user.discord.id);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was a database error resetting a pilot's home maps.", err);
}
await Discord.queue("You have successfully cleared your home maps. Please use the `!home` command to select 3 home maps, one at a time, for example, `!home Logic x2`.", user);
return true;
}
// # ## # #
// # # #
// ### ## # # ## # ## ### ###
// # # # # #### # ## # # ## #
// # # # # # # ## # # ## #
// # # ## # # ## ### ### ### ##
/**
* Sends the list of home maps to a user.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async homelist(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (message) {
return false;
}
let homeList;
try {
homeList = await Db.getHomeList();
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was a database error getting the home map list.", err);
}
if (!homeList || homeList.length === 0) {
await Discord.queue(`Sorry, ${user}, but no one has set their home map yet.`, channel);
throw new Warning("No home maps set yet.");
}
const homes = {};
homeList.forEach((row) => {
const name = Discord.getGuildUser(row.DiscordID) || `<@${row.DiscordID}>`;
if (!homes[name]) {
homes[name] = [];
}
homes[name].push(row.Home);
});
let str = "Home maps for the season:";
Object.keys(homes).sort().forEach((name) => {
str += `\n${name}: \`${homes[name].join("`, `")}\``;
});
await Discord.queue(str, user);
return true;
}
// # # #
// # #
// ### ### ### ### ### ## ### ### ###
// ## # # # # # # # # # # # # ##
// ## # # ## # # # # # # # ## ##
// ### ## # # # # ### ### # # # ###
// ###
/**
* Gets the current tournament's standings.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async standings(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (message) {
return false;
}
if (!Event.isRunning) {
await Discord.queue(`Sorry, ${user}, but there is no event currently running.`, channel);
throw new Warning("No event currently running.");
}
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is a Finals Tournament event, and does not have any standings to display. See the #match-results page for results from the tournament.`, channel);
throw new Warning("Event is a Finals Tournament.");
}
let standings;
try {
standings = Event.getStandingsText();
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was an error getting the standings.", err);
}
await Discord.queue(standings, user);
return true;
}
// # #
// # #
// ### ## ### ###
// # # # # ## #
// # # # # ## #
// # # ## ### ##
/**
* Toggles the player's ability to host.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async host(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (message) {
return false;
}
if (!Event.isRunning) {
await Discord.queue(`Sorry, ${user}, but there is no event currently running.`, channel);
throw new Warning("No event currently running.");
}
const player = Event.getPlayer(user.discord.id);
if (!player) {
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but you are not entered into this tournament.`, channel);
} else {
await Discord.queue(`Sorry, ${user}, but you first need to \`!join\` the tournament before toggling your ability to host games.`, channel);
}
throw new Warning("Player hasn't joined tournament.");
}
if (player.withdrawn) {
await Discord.queue(`Sorry, ${user}, but you have withdrawn from the tournament.`, channel);
throw new Warning("Player withdrew from the tournament.");
}
player.canHost = !player.canHost;
await Discord.queue(`You have successfully toggled ${player.canHost ? "on" : "off"} your ability to host games.`, user);
await Discord.queue(`${Discord.getGuildUser(user).displayName} has toggled ${player.canHost ? "on" : "off"} their ability to host games.`);
return true;
}
// #
// #
// ## ### ## ## ### ##
// # # # # # # # ## # ##
// # # # # # # # ## ##
// ## # # ## ## ### ##
/**
* Chooses a home map.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async choose(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (!message || ["a", "b", "c"].indexOf(message.toLowerCase()) === -1) {
return false;
}
if (!Event.isRunning) {
await Discord.queue(`Sorry, ${user}, but there is no event currently running.`, channel);
throw new Warning("No event currently running.");
}
const match = Event.getCurrentMatch(user.discord.id);
if (!match) {
await Discord.queue(`Sorry, ${user}, but I cannot find a match available for you.`, channel);
throw new Warning("Player has no current match.");
}
if (match.home === user.discord.id) {
await Discord.queue(`Sorry, ${user}, but your opponent must pick one of your home maps.`, channel);
throw new Warning("Home player tried to select home map.");
}
await Event.setMatchHome(match, message.toLowerCase().charCodeAt(0) - 97);
return true;
}
// #
// #
// ### ## ### ## ### ###
// # # # ## # # # # # # #
// # ## # # # # # #
// # ## ### ## # ##
// #
/**
* Reports a match result.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async report(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (!message) {
return false;
}
if (!Event.isRunning) {
await Discord.queue(`Sorry, ${user}, but there is no event currently running.`, channel);
throw new Warning("No event currently running.");
}
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can report games in.`, channel);
throw new Warning("Event does not allow reporting.");
}
if (!reportParse.test(message)) {
if (wrongReportParse.test(message)) {
await Discord.queue(`Sorry, ${user}, but did you mean to report a negative score, or are you using the dash as a separator? You must separate your score and your opponent's score with a space, not a dash: \`!report 20 12\``, channel);
throw new Warning("Invalid syntax.");
} else {
await Discord.queue(`Sorry, ${user}, but you must report the score in the following format: \`!report 20 12\``, channel);
throw new Warning("Invalid syntax.");
}
}
const match = Event.getCurrentMatch(user.discord.id);
if (!match) {
await Discord.queue(`Sorry, ${user}, but I cannot find a match available for you.`, channel);
throw new Warning("Player has no current match.");
}
if (!match.homeSelected) {
await Discord.queue(`Sorry, ${user}, but no home map has been set for your match. See the instructions in ${match.channel} to get a home map selected for this match.`, channel);
throw new Warning("Current match has no home map set.");
}
let score1, score2;
({1: score1, 2: score2} = reportParse.exec(message));
score1 = +score1;
score2 = +score2;
if (score1 < score2) {
[score1, score2] = [score2, score1];
}
if (score1 < 20 || score1 === 20 && score1 - score2 < 2 || score1 > 20 && score1 - score2 !== 2) {
await Discord.queue(`Sorry, ${user}, but that is an invalid score. Games must be played to 20, and you must win by 2 points.`, channel);
throw new Warning("Invalid score.");
}
const player2 = Discord.getGuildUser(match.players.filter((p) => p !== user.discord.id)[0]);
match.reported = {
winner: player2.id,
score: [score1, score2]
};
await Discord.queue(`Game reported: ${player2.displayName} ${score1}, ${Discord.getGuildUser(user).displayName} ${score2}. ${player2}, please type \`!confirm\` to confirm the match. If there is an error, such as the wrong person reported the game, it can be reported again to correct it.`, match.channel);
return true;
}
// # #
// # #
// # ## # # ### ## ## ### ##
// ### # ## ## # # # # # # ##
// # # ## ## # # # # ##
// # ### # # ### ## ## # ##
/**
* Fixes the score of a match already played.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async fixscore(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
Commands.checkUserIsAdmin(user);
if (!message) {
return false;
}
if (!Event.isRunning) {
await Discord.queue(`Sorry, ${user}, but an event is not currently running.`, channel);
throw new Warning("Event is currently running.");
}
if (!Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but the event currently running is not a Finals Tournament.`, channel);
throw new Warning("Event is currently running.");
}
if (!reportGameParse.test(message)) {
await Discord.queue(`Sorry, ${user}, but you must correct the score using the following format: \`!fixscore <player1> <score1> <player2> <score2>\`.`, channel);
throw new Warning("Incorrect syntax.");
}
const {1: player1Id, 2: player1Score, 3: player2Id, 4: player2Score} = reportGameParse.exec(message);
const match = Event.getMatchBetweenPlayers(player1Id, player2Id);
if (!match) {
await Discord.queue(`Sorry, ${user}, but I cannot find a match between those two players.`, channel);
throw new Warning("No match between players.");
}
if (!match.winner) {
await Discord.queue(`Sorry, ${user}, but you cannot correct game scores of an unreported match.`, channel);
throw new Warning("Match not yet confirmed.");
}
await Event.fixScore(match, player1Id, +player1Score, player2Id, +player2Score);
return true;
}
// # #
// # #
// ## ## ### # ## ### # #
// # # # # # ### # # # ####
// # # # # # # # # # #
// ## ## # # # ### # # #
/**
* Confirms a match result.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async confirm(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (message) {
return false;
}
if (!Event.isRunning) {
await Discord.queue(`Sorry, ${user}, but there is no event currently running.`, channel);
throw new Warning("No event currently running.");
}
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can report games in.`, channel);
throw new Warning("Event does not allow reporting.");
}
const match = Event.getCurrentMatch(user.discord.id);
if (!match) {
await Discord.queue(`Sorry, ${user}, but I cannot find a match available for you.`, channel);
throw new Warning("Player has no current match.");
}
if (!match.reported) {
await Discord.queue(`Sorry, ${user}, but this match hasn't been reported yet. Make sure the loser reports the result of the game in the following format: \`!report 20 12\``, channel);
throw new Warning("Match is not yet reported.");
}
if (match.reported.winner !== user.discord.id) {
await Discord.queue(`Sorry, ${user}, but you can't confirm your own reports!`, channel);
throw new Warning("Player tried to confirm their own report.");
}
await Event.confirmResult(match, match.reported.winner, match.reported.score);
return true;
}
// #
// #
// ## ## # # # # ## ### ###
// # # # #### #### # ## # # #
// # # # # # # # ## # # #
// ## ## # # # # ## # # ##
/**
* Comments a match.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async comment(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
if (!message) {
return false;
}
if (!Event.isRunning) {
await Discord.queue(`Sorry, ${user}, but there is no event currently running.`, channel);
throw new Warning("No event currently running.");
}
const matches = Event.getCompletedMatches(user.discord.id);
if (matches.length === 0) {
await Discord.queue(`Sorry, ${user}, but you have not played in any matches that can be commented on.`, channel);
throw new Warning("User has no completed matches.");
}
const match = matches[matches.length - 1];
if (!match.comments) {
match.comments = {};
}
match.comments[user.discord.id] = message;
await Event.updateResult(match);
await Discord.queue(`${user}, your match comment has been successfully updated.`, channel);
return true;
}
// # # #
// # # #
// ### ### ### ## # # ## ### ###
// # # # # # # # ## # # # ## # # #
// # ## # # # # ## # # ## # # #
// # # ### ### ## # ## # # ##
/**
* Adds a new event.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async addevent(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
Commands.checkUserIsAdmin(user);
if (!message) {
return false;
}
if (!addEventParse.test(message)) {
await Discord.queue(`Sorry, ${user}, but for you to add the event, you must include the name of the event followed by the time that it is to start.`, channel);
return false;
}
const {1: season, 2: event, 3: date} = addEventParse.exec(message);
let eventDate;
try {
eventDate = new Date(new tz.Date(date, "America/Los_Angeles"));
} catch (err) {
await Discord.queue(`Sorry, ${user}, but that is an invalid date and time.`, channel);
throw new Warning("Invalid date and time.");
}
if (eventDate < new Date()) {
await Discord.queue(`Sorry, ${user}, but that date occurs in the past.`, channel);
throw new Warning("Date is in the past.");
}
try {
await Event.addEvent(+season, event, eventDate);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a problem adding a new event.`, channel);
throw err;
}
return true;
}
// # #
// # #
// ### ## ### ## ### ### ### ## ### ## # # ### ###
// # # # ## # # # ## # # # # # # ## # # # # # # # # # #
// ## ## # # ## # # ## # ## # # # # # # # # #
// # ## # # ## # # # ## ## # ## ### # # ###
// ###
/**
* Generates the next round of the tournament.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<boolean>} A promise that resolves with whether the command completed successfully.
*/
async generateround(user, message, channel) {
Commands.checkMessageIsFromDiscord(this.service);
Commands.checkUserIsAdmin(user);
if (message) {
return false;
}
if (!Event.isRunning) {
await Discord.queue(`Sorry, ${user}, but there is no event currently running.`, channel);
throw new Warning("Event is not currently running.");
}
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can generate rounds for.`, channel);
throw new Warning("Event is not of the right type.");
}
try {
await Event.loadRatedPlayers();
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error.`, channel);
throw new Exception("There was a database error getting the list of rated players.", err);
}
let matches;
try {
matches = await Event.generateRound();
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a problem matching players up for the next round.`, channel);
throw err;
}
await Discord.queue(`Round ${Event.round} starts now!`);
try {
let str = "Matches:";