This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MrClockwork.js
1748 lines (1427 loc) · 46.9 KB
/
MrClockwork.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 mail = require('mail.js');
const config = require("../MrClockwork/config.json");
const event = require("../MrClockwork/emitter.js");
const rw = require("../MrClockwork/reader_writer.js");
const dom4game = require("../MrClockwork/dom4game.js");
const coe4game = require("../MrClockwork/coe4game.js");
const timer = require("../MrClockwork/timer.js");
const trans = require("../MrClockwork/translator.js");
const dice = require("../MrClockwork/dice.js");
const tracker = require("../MrClockwork/tracker.js").init();
const dom5game = require("../MrClockwork/dom5game.js"); //to be initialized after loading the games data.
const dom4Host = require("../MrClockwork/dom4_hosting.js"); //to be initialized after loading the games data.
const dom5Host = require("../MrClockwork/dom5_hosting.js"); //to be initialized after loading the games data.
const coe4Host = require("../MrClockwork/coe4_hosting.js"); //to be initialized after loading the games data.
//The Bot
const bot = new Discord.Client();
Number.isFloat = function(n)
{
return Number(n) === n && n % 1 !== 0;
}
String.prototype.capitalize = function()
{
return this.charAt(0).toUpperCase() + this.slice(1);
}
String.prototype.width = function (space, spaceFirst = false, spacingChar = " ")
{
var arrL = space - this.length + 1;
if (arrL < 1) arrL = 1;
if (spaceFirst) return Array(arrL).join(spacingChar) + this;
else return this + Array(arrL).join(spacingChar);
}
String.prototype.toBox = function()
{
if (this !== "" && this != null && this.length && /\S+/.test(this))
{
return "```" + this + "```";
}
else return this;
}
Number.prototype.cap = function(limit)
{
if (this > limit)
{
return limit;
}
else return this;
}
Number.prototype.lowerCap = function(limit)
{
if (this < limit)
{
return limit;
}
else return this;
}
var owner;
var myGuild;
var modRole;
var houndsRole;
var blitzerRole;
var games = {};
var maxGames = 27;
//Will switch to true when the on.ready event first triggers so that certain bits of code don't get re-executed when the bot reconnects and goes through on.ready again
var wasInitialized = false;
var didNotReconnect = false;
var monitoredMembers = [];
var monitoredMessages = [];
var inviteUses = {};
//Stuff starts to happen after the 'ready' event is sent, so code is put here. Kinda like a constructor or main function.
bot.on("ready", () =>
{
rw.log("I am ready!");
didNotReconnect = false;
owner = bot.users.get(config.ownerID);
myGuild = bot.guilds.get(config.myGuildID);
if (owner == null)
{
rw.log("Something went wrong; cannot find the owner of the guild.");
return;
}
if (myGuild == null)
{
rw.log("Something went wrong; cannot find myGuild.");
return;
}
houndsRole = myGuild.roles.get(config.houndsID);
if (houndsRole == null)
{
rw.log("Something went wrong; cannot find the Hounds role object in this guild.");
}
blitzerRole = myGuild.roles.get(config.blitzerID);
if (blitzerRole == null)
{
rw.log("Something went wrong; cannot find the Blitzer role object in this guild.");
}
modRole = myGuild.roles.get(config.modID);
if (modRole == null)
{
rw.log("Something went wrong; cannot find the Moderator role object in this guild.");
}
myGuild.fetchInvites().then(invites =>
{
for (var [key, value] of invites)
{
inviteUses[value.code] = value.uses;
}
}).catch(console.error);
owner.send("I am ready!").catch((err) => {rw.log(err);});
if (wasInitialized == false)
{
initialize();
}
});
bot.on("guildMemberAdd", member =>
{
myGuild.fetchInvites().then(invites =>
{
for (var [key, value] of invites)
{
if (inviteUses[value.code] == null)
{
inviteUses[value.code] = 0;
}
if (inviteUses[value.code] < value.uses)
{
inviteUses[value.code] = value.uses;
rw.logMemberJoin(member.user.username, value.code, value.inviter.username);
}
}
}).catch(console.error);
});
//On messages sent to channels
bot.on('message', message =>
{
myGuild.fetchMember(message.author).then(function(result)
{
processMessage(message, result);
});
});
event.e.on("minute", () =>
{
if (wasInitialized === false)
{
return;
}
for (inst in games)
{
if (games[inst].game === "Dom4" || games[inst].game === "Dom5")
{
games[inst].statusCheck();
}
}
monitoredMessages.forEach(function(msg, index)
{
if (Date.now() - msg.stamp >= 60000)
{
monitoredMessages.splice(index, 1);
}
});
});
event.e.on("5 seconds", () =>
{
if (wasInitialized === false)
{
return;
}
for (inst in games)
{
if (games[inst].game === "CoE4")
{
games[inst].statusCheck();
}
}
});
event.e.on("hour", () =>
{
if (wasInitialized === false)
{
return;
}
for (var key in games)
{
if (games[key].game === "CoE4")
{
continue;
}
if (games[key].wasStarted === false && games[key].channel == null && games[key].role == null && games[key].currenttimer != null && games[key].currenttimer.turn === 0 && Date.now() - games[key].firstHosted > 3600000)
{
rw.log("More than an hour has passed and " + games[key].name + " has not started, neither does it have a channel. Cleaning it up.");
games[key].deleteDomSave();
games[key].deleteSave();
delete games[key];
}
}
});
//only triggers on messages sent after the bot was started
bot.on("messageDelete", message =>
{
myGuild.fetchMember(message.author).then(function(result)
{
processDeletedMessage(message, result);
});
});
function processDeletedMessage(message, authorMember)
{
var monitored = monitoredMessages.find(function(msg)
{
return msg.id === message.id;
});
if (monitored == null)
{
return;
}
myGuild.fetchAuditLogs({user: monitored.author, type: "MESSAGE_DELETE"}).then(function(audit)
{
var lastEntry = audit.entries.last();
//self-deleted messages do not show up in the audit log
if (lastEntry == null || lastEntry.executor.id === monitored.author.id)
{
if (authorMember.highestRole.id === myGuild.id)
{
owner.send("`" + message.author.username + "` is being banned for sending the following mention in the channel `" + message.channel.name + "` and deleted it within a minute:\n\n" + message.content);
myGuild.ban(monitored.author, {reason: "Auto-banned for pinging a user and then deleting the message within a minute. The banned member did not have a role."});
}
else owner.send("`" + message.author.username + "` sent the following mention in the channel `" + message.channel.name + "` and deleted it within a minute:\n\n" + message.content);
}
});
}
function processMessage(message, member)
{
var input = message.content;
var username = message.author.username;
if (message.author.bot === true)
{
return;
}
if (member == null)
{
rw.log("Could not find the GuildMember object of user " + username + ". His input was '" + input + "' in channel " + message.channel.name + ".");
}
if (message.mentions.users.size > 0 || message.mentions.roles.size > 0 || message.mentions.everyone === true)
{
monitoredMessages.push({id: message.id, stamp: message.createdTimestamp, author: message.author});
}
if (monitoredMembers.includes(member.id) === true)
{
owner.send(username + " sent the following message in the channel " + message.channel.name + ":\n\n" + message.content);
}
if (/^%FOLLOW\s*\d+$/ig.test(input) === true)
{
var id;
if (message.author.id !== owner.id)
{
return;
}
id = input.replace(/^FOLLOW\s*(\d+)$/i, "$1");
bot.fetchUser(id).then(function(fetchedUser)
{
if (fetchedUser == null)
{
owner.send("The id " + id + " is not a registered user.");
return;
}
monitoredMembers.push(id);
});
}
else if (/^%UNFOLLOW\s*\d+$/ig.test(input) === true)
{
var id;
if (message.author.id !== owner.id)
{
return;
}
id = input.replace(/^FOLLOW\s*(\d+)$/i, "$1");
if (monitoredMembers.includes(id) === false)
{
owner.send("The id " + id + " was not being followed.");
return;
}
monitoredMembers.splice(monitoredMembers.indexOf(id), 1);
owner.send("The id " + id + " has been removed from the list.");
}
else if (/^%JOINEDAT$/ig.test(input) === true)
{
var arr = [];
var response = "";
if (message.author.id !== owner.id || member.highestRole.position < modRole.position)
{
rw.log(`${username} tried to use the command %joinedAt but does not have enough permissions.`);
return;
}
myGuild.fetchMembers().then(function(result)
{
for (var [key, value] of result.members)
{
arr.push(value);
}
arr.sort(function (memberA, memberB)
{
return memberB.joinedAt.getTime() - memberA.joinedAt.getTime();
});
arr.forEach(function(orderedMember, index)
{
response += (orderedMember.user.username + " ").width(30) + " " + orderedMember.joinedAt + "\n";
if (index % 15 === 0 && index > 0)
{
message.author.send(response, {code: true});
response = "";
}
});
message.author.send(response, {code: true});
});
}
//dice roller
if (/^(\%|\?)[d0-9\+]+$/ig.test(input) === true)
{
var rollStr;
try
{
rollStr = dice.processRolls(input);
message.channel.send(rollStr);
}
catch (err)
{
rw.log("An error occurred when trying to roll " + input + ":\n\n\n" + err);
message.channel.send("Ooops, something went wrong in the code here.").catch((err) => {rw.log(err);});
}
}
else if (/\%GAMES/i.test(input))
{
var onlineGames = "";
var offlineGames = "";
if (message.channel.type != "dm")
{
message.reply("This command must be used through a Direct Message (DM) to me.");
return;
}
for (var key in games)
{
if (games[key].instance == null)
{
offlineGames += games[key].printInfo();
}
else
{
onlineGames += games[key].printInfo();
}
}
if (offlineGames === "" && onlineGames === "")
{
message.channel.send("There are no games hosted.").catch((err) => {rw.log(err);});
return;
}
if (offlineGames === "")
{
message.channel.send("All currently saved games are **online**:\n" + onlineGames.toBox(), {split: {prepend: "```", append: "```"}});
return;
}
if (onlineGames === "")
{
message.channel.send("All currently saved games are **offline**:\n" + offlineGames.toBox(), {split: {prepend: "```", append: "```"}});
return;
}
//When both online and offline games contain stuff, it *has* to be done like this instead of
//using the usual split message option, because those are async and it messes up the order in
//which the game list appears
if (onlineGames.length > 1900)
{
var l = Math.ceil(onlineGames.length / 1500);
message.channel.send("The following games are **online**:\n").catch((err) => {rw.log(err);});
for (var i = 0; i < l; i++)
{
var nextBreak = onlineGames.indexOf("\n", (i+1) * 1500);
if (i == l - 1)
{
message.channel.send(onlineGames.slice(onlineGames.indexOf("\n", i * 1500)).toBox());
}
else message.channel.send(onlineGames.slice(i * 1500, nextBreak).toBox());
}
}
else message.channel.send("The following games are **online**:\n" + onlineGames.toBox());
if (offlineGames.length > 1900)
{
var l = Math.ceil(offlineGames.length / 1500);
message.channel.send("\n\nThe following games are saved, but **currently offline**:\n").catch((err) => {rw.log(err);});
for (var i = 0; i < l; i++)
{
var nextBreak = offlineGames.indexOf("\n", (i+1) * 1500);
if (i == l - 1)
{
message.channel.send(offlineGames.slice(offlineGames.indexOf("\n", i * 1500)).toBox());
}
else message.channel.send(offlineGames.slice(i * 1500, nextBreak).toBox());
}
}
else message.channel.send("\n\nThe following games are saved, but **currently offline**:\n" + offlineGames.toBox());
}
else if (/\%MODS DOM4/i.test(input))
{
var msg = "";
var path = config.dom4DataPath + "mods";
if (fs.existsSync(path) === false)
{
message.author.send("An error occurred. No mods directory was found on the server.").catch((err) => {rw.log(err);});
return;
}
fs.readdir(path, "utf8", (err, files) =>
{
if (err)
{
rw.log("An error occurred while reading the mods directory:\n\n" + err);
message.author.send("An error occurred while reading the mods directory.").catch((err) => {rw.log(err);});
return;
}
message.author.send("Here are the mods available on the server:\n\n").catch((err) => {rw.log(err);});
for (var i = 0; i < files.length; i++)
{
//Only check the files with the .2h extension
if (files[i].slice(files[i].lastIndexOf(".")) == ".dm")
{
msg += (files[i]).width(24) + "\n";
}
}
message.author.send(msg.toBox(), {split: {prepend: "```", append: "```"}});
});
}
else if (/\%MODS DOM5/i.test(input))
{
var msg = "";
var path = config.dom5DataPath + "mods";
if (fs.existsSync(path) === false)
{
message.author.send("An error occurred. No mods directory was found on the server.").catch((err) => {rw.log(err);});
return;
}
fs.readdir(path, "utf8", (err, files) =>
{
if (err)
{
rw.log("An error occurred while reading the mods directory:\n\n" + err);
message.author.send("An error occurred while reading the mods directory.").catch((err) => {rw.log(err);});
return;
}
message.author.send("Here are the mods available on the server:\n\n").catch((err) => {rw.log(err);});
for (var i = 0; i < files.length; i++)
{
//Only check the files with the .2h extension
if (files[i].slice(files[i].lastIndexOf(".")) == ".dm")
{
msg += (files[i]).width(24) + "\n";
}
}
message.author.send(msg.toBox(), {split: {prepend: "```", append: "```"}});
});
}
else if (/\%MAPS DOM4/i.test(input))
{
var msg = "";
var provCount;
var path = config.dom4DataPath + "maps";
if (fs.existsSync(path) === false)
{
message.author.send("An error occurred. No maps directory was found on the server.").catch((err) => {rw.log(err);});
return;
}
fs.readdir(path, "utf8", (err, files) =>
{
if (err)
{
rw.log("An error occurred while reading the maps directory:\n\n" + err);
message.author.send("An error occurred while reading the maps directory.").catch((err) => {rw.log(err);});
return;
}
message.author.send("Here are the maps available on the server:\n\n").catch((err) => {rw.log(err);});
for (var i = 0; i < files.length; i++)
{
//Only check the files with the .2h extension
if (files[i].slice(files[i].lastIndexOf(".")) == ".map")
{
provCount = trans.getProvinceCount(fs.readFileSync(config.dom4DataPath + "maps/" + files[i], "utf8"));
msg += (files[i]).width(48) + "(" + provCount.land.toString().width(4) + " land + " + provCount.sea.toString().width(3) + " sea).\n";
}
}
message.author.send(msg.toBox(), {split: {prepend: "```", append: "```"}});
});
}
else if (/\%MAPS DOM5/i.test(input))
{
var msg = "";
var provCount;
var path = config.dom5DataPath + "maps";
if (fs.existsSync(path) === false)
{
message.author.send("An error occurred. No maps directory was found on the server.").catch((err) => {rw.log(err);});
return;
}
fs.readdir(path, "utf8", (err, files) =>
{
if (err)
{
rw.log("An error occurred while reading the maps directory:\n\n" + err);
message.author.send("An error occurred while reading the maps directory.").catch((err) => {rw.log(err);});
return;
}
message.author.send("Here are the maps available on the server:\n\n").catch((err) => {rw.log(err);});
for (var i = 0; i < files.length; i++)
{
//Only check the files with the .2h extension
if (files[i].slice(files[i].lastIndexOf(".")) == ".map")
{
provCount = trans.getProvinceCount(fs.readFileSync(config.dom5DataPath + "maps/" + files[i], "utf8"));
msg += (files[i]).width(48) + "(" + provCount.land.toString().width(4) + " land + " + provCount.sea.toString().width(3) + " sea).\n";
}
}
message.author.send(msg.toBox(), {split: {prepend: "```", append: "```"}});
});
}
else if (/\%NATIONS DOM4/i.test(input))
{
var str = "";
for (id in trans.dom4NationNumbersToNames)
{
str += (id + ": ").width(10) + trans.dom4NationNumbersToNames[id] + "\n";
}
message.author.send(str.toBox(), {split: {prepend: "```", append: "```"}});
}
else if (/\%NATIONS DOM5/i.test(input))
{
var str = "";
for (id in trans.dom5NationNumbersToNames)
{
str += (id + ": ").width(10) + trans.dom5NationNumbersToNames[id] + "\n";
}
message.author.send(str.toBox(), {split: {prepend: "```", append: "```"}});
}
else if (/^\%ADD ROLE/i.test(input) && message.channel.type != "dm")
{
var gameKey = message.channel.name.replace("_game", "").toLowerCase();
var membersMap = message.mentions.members;
var rolesMap = myGuild.roles;
if (message.channel.name.includes("_game") === false)
{
var roles = input.replace(/%ADD ROLE/i, "").trim().toLowerCase();
if (rolesMap.size <= 0)
{
message.reply("No role is available in the guild.");
return;
}
for (var [k, v] of rolesMap)
{
if (roles.includes(v.name.toLowerCase()) === false)
{
continue;
}
//only works if the member's highest role is higher than the other roles he tries to give himself.
if (v.name.toLowerCase().includes(" player") === true)
{
message.reply("Only the game's organizer or an Admin, Pretender or GM can give the role " + v.name + " to you.");
}
else if (member.highestRole.comparePositionTo(v) > 0)
{
member.addRole(v);
message.reply("The role " + v.name + " was added.");
}
else
{
message.reply("You do not have permissions to add the role " + v.name + " to yourself. You can only add roles lower than yours.");
}
}
return;
}
if (games[gameKey] == null)
{
message.reply("The game is not in my list of saved games. Are you sure this channel name is correct? (it should be the game name + '_game', like 'midnight_game' or 'total_expression_game').");
return;
}
if (checkPermissions(message.author.id, member, games[gameKey]) === false)
{
message.reply("Sorry, you do not have the permissions to do this. Only this game's organizer (" + games[gameKey].organizer + ") or Admins, Pretenders or GMs can do this.");
return;
}
if (games[gameKey].role == null)
{
message.reply("There doesn't seem to be a role created for this game yet. You can use the `%record mygamename` command PMed to me to create one automatically.");
return;
}
if (membersMap.size <= 0)
{
message.reply("You did not mention any user. Use @ to mention a user to whom you want to give the role.");
return;
}
for (var [k, v] of membersMap)
{
v.addRole(games[gameKey].role);
}
message.reply("The roles were added.");
}
else if (/^\%REMOVE ROLE/i.test(input) && message.channel.type != "dm")
{
var gameKey = message.channel.name.replace("_game", "").toLowerCase();
var membersMap = message.mentions.members;
var rolesMap = member.roles;
if (message.channel.name.includes("_game") === false)
{
var roles = input.replace(/%REMOVE ROLE/i, "").trim().toLowerCase();
if (rolesMap.size <= 0)
{
message.reply("You do not have any role.");
return;
}
for (var [k, v] of rolesMap)
{
if (roles.includes(v.name.toLowerCase()) === false)
{
continue;
}
member.removeRole(v);
message.reply("The role " + v.name + " was removed.");
}
return;
}
if (games[gameKey] == null)
{
message.reply("The game is not in my list of saved games. Are you sure this channel name is correct? (it should be the game name + '_game', like 'midnight_game' or 'total_expression_game').");
return;
}
if (checkPermissions(message.author.id, member, games[gameKey]) === false)
{
message.reply("Sorry, you do not have the permissions to do this. Only this game's organizer (" + games[gameKey].organizer + ") or Admins, Pretenders or GMs can do this.");
return;
}
if (games[gameKey].role == null)
{
message.reply("There doesn't seem to be a role created for this game yet. You can use the `%record mygamename` command PMed to me to create one automatically.");
return;
}
if (membersMap.size <= 0)
{
message.reply("You did not mention any user. Use @ to mention a user from whom you want to remove the role.");
return;
}
for (var [k, v] of membersMap)
{
v.removeRole(games[gameKey].role);
}
message.reply("The roles were removed.");
}
else if (/^\%REMOVE\s+(\w+\.2h)?\s*(\w+)?/i.test(input) && message.channel.type != "dm")
{
var nation = input.replace(/%REMOVE\s+(\w+\.2h)?\s*(\w+)?/i, "$1");
var gameKey;
if (message.channel.name.includes("_game") === false)
{
gameKey = input.replace(/%REMOVE\s+(\w+\.2h)?\s*(\w+)?/i, "$2").toLowerCase();
}
else gameKey = message.channel.name.replace("_game", "").toLowerCase();
if (games[gameKey] == null)
{
message.reply("The game is not in my list of saved games. If it's a game with a channel, the channel name might not be matching. Otherwise, make sure you check your spelling. The game name should come at the end of the command, separated by a space.");
return;
}
if (games[gameKey].game != "Dom5")
{
message.reply("Only Dominions 5 games support this function.");
return;
}
if (checkPermissions(message.author.id, member, games[gameKey]) === false)
{
message.reply("Sorry, you do not have the permissions to do this. Only this game's organizer (" + games[gameKey].organizer + ") or Admins, Pretenders or GMs can do this.");
return;
}
if (games[gameKey].wasStarted === true)
{
message.reply("You cannot remove a nation from a game when it has already started. Use this only for when players change nations in the lobby.");
return;
}
message.reply(games[gameKey].removeNation(nation));
}
else if (/^\%TIMER\s*(\w+)\s+(\w+)/i.test(input) === true && message.channel.name.includes("_game") === false && message.channel.type != "dm")
{
var gameKey = input.replace(/^\%TIMER\s*(\w+)\s+(\w+)/i, "$2").trim().toLowerCase();
var newTimer = timer.createFromInput(input.replace(/^\%TIMER\s*(\w+)\s+(\w+)/i, "$1"));
try
{
timerInputCheck(message, member, gameKey);
}
catch (err)
{
message.reply(err);
return;
}
games[gameKey].changeCurrentTimer(newTimer);
if (newTimer.isPaused === true)
{
rw.log(message.author.username + " paused the timer: " + input);
message.reply(mentionRole(games[gameKey].role) + " The timer has been paused. This might take a few seconds to update ingame.");
}
else
{
rw.log(username + " requested a timer change: " + input);
message.reply(mentionRole(games[gameKey].role) + " The timer has been changed. Now " + newTimer.print() + " remain for the new turn to arrive. This might take a few seconds to update ingame.");
}
}
else if (/^\%TIMER\s*(\w+)/i.test(input) === true && message.channel.name.includes("_game") === true && message.channel.type != "dm")
{
var gameKey = message.channel.name.replace("_game", "").toLowerCase();
var newTimer = timer.createFromInput(input.replace(/^\%TIMER\s*(\w+)/i, "$1"));
try
{
timerInputCheck(message, member, gameKey);
}
catch (err)
{
message.reply(err);
return;
}
games[gameKey].changeCurrentTimer(newTimer);
if (newTimer.isPaused == true)
{
rw.log(message.author.username + " paused the timer: " + input);
message.reply(mentionRole(games[gameKey].role) + " The timer has been paused. This might take a few seconds to update ingame.");
}
else
{
rw.log(username + " requested a timer change: " + input);
message.reply(mentionRole(games[gameKey].role) + " The timer has been changed. Now " + newTimer.print() + " remain for the new turn to arrive. This might take a few seconds to update ingame.");
}
}
else if (/^\%DTIMER\s*(\w+)\s+(\w+)/i.test(input) === true && message.channel.name.includes("_game") === false && message.channel.type != "dm")
{
var gameKey = input.replace(/^\%DTIMER\s*(\w+)\s+(\w+)/i, "$2").trim().toLowerCase();
var newTimer = timer.createFromInput(input.replace(/^\%DTIMER\s*(\w+)\s+(\w+)/i, "$1"));
try
{
timerInputCheck(message, member, gameKey);
}
catch (err)
{
message.reply(err);
return;
}
games[gameKey].changeDefaultTimer(newTimer);
if (newTimer.isPaused == true)
{
rw.log(message.author.username + " set the default timer to zero (unlimited turn times): " + input);
message.reply(mentionRole(games[gameKey].role) + " The default timer has been paused.");
}
else
{
rw.log(username + " requested a default timer change: " + input);
message.reply(mentionRole(games[gameKey].role) + " The default timer has been set to " + newTimer.print() + ".");
}
}
else if (/^\%DTIMER\s*(\w+)/i.test(input) === true && message.channel.name.includes("_game") === true && message.channel.type != "dm")
{
var gameKey = message.channel.name.replace("_game", "").toLowerCase();
var newTimer = timer.createFromInput(input.replace(/^\%DTIMER\s*(\w+)/i, "$1"));
try
{
timerInputCheck(message, member, gameKey);
}
catch (err)
{
message.reply(err);
return;
}
games[gameKey].changeDefaultTimer(newTimer);
if (newTimer.isPaused == true)
{
rw.log(message.author.username + " set the default timer to zero (unlimited turn times): " + input);
message.reply(mentionRole(games[gameKey].role) + " The default timer has been paused.");
}
else
{
rw.log(username + " requested a default timer change: " + input);
message.reply(mentionRole(games[gameKey].role) + " The default timer has been set to " + newTimer.print() + ".");
}
}
else if (/^\%TIMER\s?(\w+)?/i.test(input) && message.channel.type != "dm")
{
var gameKey = message.channel.name.replace("_game", "").toLowerCase();
if (message.channel.name.includes("_game") === false)
{
gameKey = input.replace(/^\%TIMER\s?(\w+)/i, "$1").trim().toLowerCase();
}
if (games[gameKey] == null)
{
message.reply("The game is not in my list of saved games. If it's a game with a channel, the channel name might not be matching. Otherwise, make sure you check your spelling.");
return;
}
if (games[gameKey].game != "Dom4" && games[gameKey].game != "Dom5")
{
message.reply("Unfortunately, only Dominions 4 or 5 games have a timer available.");
return;
}
rw.log(username + " requested to know the timer.");
games[gameKey].getCurrentTimer(function(currTimer)
{
if (currTimer.turn === 0 || games[gameKey].wasStarted === false)
{
message.reply("The game has not started yet!");
}
else if (games[gameKey].instance == null)
{
message.reply("The game's instance isn't online, so the timer isn't moving. " + "It is turn " + currTimer.turn + ", and there are " + currTimer.print() + " left for it to roll.");
}
else if (currTimer.isPaused === true)
{
message.reply("It is turn " + currTimer.turn + ", and the timer is paused.");
}