-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-commands.js
4031 lines (3554 loc) · 158 KB
/
chat-commands.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
/**
* System commands
* Pokemon Showdown - http://pokemonshowdown.com/
*
* These are system commands - commands required for Pokemon Showdown
* to run. A lot of these are sent by the client.
*
* System commands should not be modified, added, or removed. If you'd
* like to modify or add commands, add or edit files in chat-plugins/
*
* For the API, see chat-plugins/COMMANDS.md
*
* @license MIT license
*/
'use strict';
/* eslint no-else-return: "error" */
const crypto = require('crypto');
const FS = require('./lib/fs');
const MAX_REASON_LENGTH = 300;
const MUTE_LENGTH = 7 * 60 * 1000;
const HOURMUTE_LENGTH = 60 * 60 * 1000;
const MAX_CHATROOM_ID_LENGTH = 225;
let owners = ["mewth", "flufi"];
exports.commands = {
'!version': true,
version: function (target, room, user) {
if (!this.runBroadcast()) return;
this.sendReplyBox("Server version: <b>" + Chat.package.version + "</b>");
},
'!authority': true,
auth: 'authority',
stafflist: 'authority',
globalauth: 'authority',
authlist: 'authority',
authority: function (target, room, user, connection) {
if (target) {
let targetRoom = Rooms.search(target);
let availableRoom = targetRoom && targetRoom.checkModjoin(user);
if (targetRoom && availableRoom) return this.parse('/roomauth1 ' + target);
return this.parse('/userauth ' + target);
}
let rankLists = {};
let ranks = Object.keys(Config.groups);
for (let u in Users.usergroups) {
let rank = Users.usergroups[u].charAt(0);
if (rank === ' ' || rank === '+') continue;
// In case the usergroups.csv file is not proper, we check for the server ranks.
if (ranks.includes(rank)) {
let name = Users.usergroups[u].substr(1);
if (!rankLists[rank]) rankLists[rank] = [];
if (name) rankLists[rank].push(name);
}
}
let buffer = Object.keys(rankLists).sort((a, b) =>
(Config.groups[b] || {rank: 0}).rank - (Config.groups[a] || {rank: 0}).rank
).map(r =>
(Config.groups[r] ? "**" + Config.groups[r].name + "s** (" + r + ")" : r) + ":\n" + rankLists[r].sort((a, b) => toId(a).localeCompare(toId(b))).join(", ")
);
if (!buffer.length) return connection.popup("This server has no global authority.");
connection.popup(buffer.join("\n\n"));
},
authhelp: [
`/auth - Show global staff for the server.`,
`/auth [room] - Show what roomauth a room has.`,
`/auth [user] - Show what global and roomauth a user has.`,
],
userlist: function (target, room, user) {
let userList = [];
for (let i in room.users) {
let curUser = Users(room.users[i]);
if (!curUser || !curUser.named) continue;
userList.push(Chat.escapeHTML(curUser.getIdentity(room.id)));
}
let output = `There ${Chat.plural(userList.length, 'are', 'is')} <strong style="color:#24678d">${userList.length}</strong> user${Chat.plural(userList.length)} in this room:<br />`;
output += userList.join(`, `);
this.sendReplyBox(output);
},
userlisthelp: [`/userlist - Displays a list of users who are currently in the room.`],
'!me': true,
mee: 'me',
me: function (target, room, user) {
if (this.cmd === 'mee' && /[A-Z-a-z0-9/]/.test(target.charAt(0))) {
return this.errorReply(`/mee - must not start with a letter or number`);
}
target = this.canTalk(`/${this.cmd} ${target || ''}`);
if (!target) return;
if (this.message.startsWith(`/ME`)) {
const uppercaseIdentity = user.getIdentity(room).toUpperCase();
if (room) {
this.add(`|c|${uppercaseIdentity}|${target}`);
} else {
let msg = `|pm|${uppercaseIdentity}|${this.pmTarget.getIdentity()}|${target}`;
user.send(msg);
if (this.pmTarget !== user) this.pmTarget.send(msg);
}
return;
}
return target;
},
'!battle': true,
'battle!': 'battle',
battle: function (target, room, user, connection, cmd) {
if (cmd === 'battle') return this.sendReply("What?! How are you not more excited to battle?! Try /battle! to show me you're ready.");
if (!target) target = "randombattle";
return this.parse("/search " + target);
},
pi: function (target, room, user) {
return this.sendReplyBox(
'Did you mean: 1. 3.1415926535897932384626... (Decimal)<br />' +
'2. 3.184809493B91866... (Duodecimal)<br />' +
'3. 3.243F6A8885A308D... (Hexadecimal)<br /><br />' +
'How many digits of pi do YOU know? Test it out <a href="http://guangcongluo.com/mempi/">here</a>!');
},
code: function (target, room, user) {
if (!target) return this.parse('/help code');
if (!this.canTalk()) return;
if (target.startsWith('\n')) target = target.slice(1);
const separator = '\n';
if (target.includes(separator)) {
const params = target.split(separator);
let output = [];
for (const param of params) {
output.push(Chat.escapeHTML(param));
}
let code = `<div class="chat"><code style="white-space: pre-wrap; display: table">${output.join('<br />')}</code></div>`;
if (output.length > 3) code = `<details><summary>See code...</summary>${code}</details>`;
if (!this.canBroadcast('!code')) return;
if (this.broadcastMessage && !this.can('broadcast', null, room)) return false;
if (!this.runBroadcast('!code')) return;
this.sendReplyBox(code);
} else {
return this.errorReply("You can simply use ``[code]`` for code messages that are only one line.");
}
},
codehelp: [
`!code [code] - Broadcasts code to a room. Accepts multi-line arguments. Requires: + % @ & # ~`,
`/code [code] - Shows you code. Accepts multi-line arguments.`,
],
'!avatar': true,
avatar: function (target, room, user) {
if (!target) return this.parse('/avatars');
let parts = target.split(',');
let avatarid = toId(parts[0]);
let avatar = 0;
let avatarTable = {
lucas: 1,
dawn: 2,
youngster: 3,
lass: 4,
camper: 5,
picnicker: 6,
bugcatcher: 7,
aromalady: 8,
twins: 9,
hiker: 10,
battlegirl: 11,
fisherman: 12,
cyclist: 13,
cyclistf: 14,
blackbelt: 15,
artist: 16,
pokemonbreeder: 17,
pokemonbreederf: 18,
cowgirl: 19,
jogger: 20,
pokefan: 21,
pokefanf: 22,
pokekid: 23,
youngcouple: 24,
acetrainer: 25,
acetrainerf: 26,
waitress: 27,
veteran: 28,
ninjaboy: 29,
dragontamer: 30,
birdkeeper: 31,
doubleteam: 32,
richboy: 33,
lady: 34,
gentleman: 35,
socialite: 36,
madame: 36,
beauty: 37,
collector: 38,
policeman: 39,
pokemonranger: 40,
pokemonrangerf: 41,
scientist: 42,
swimmer: 43,
swimmerf: 44,
tuber: 45,
tuberf: 46,
sailor: 47,
sisandbro: 48,
ruinmaniac: 49,
psychic: 50,
psychicf: 51,
gambler: 52,
dppguitarist: 53,
acetrainersnow: 54,
acetrainersnowf: 55,
skier: 56,
skierf: 57,
roughneck: 58,
clown: 59,
worker: 60,
schoolkid: 61,
schoolkidf: 62,
roark: 63,
barry: 64,
byron: 65,
aaron: 66,
bertha: 67,
flint: 68,
lucian: 69,
dppcynthia: 70,
bellepa: 71,
rancher: 72,
mars: 73,
galacticgrunt: 74,
gardenia: 75,
crasherwake: 76,
maylene: 77,
fantina: 78,
candice: 79,
volkner: 80,
parasollady: 81,
waiter: 82,
interviewers: 83,
cameraman: 84,
oli: 84,
reporter: 85,
roxy: 85,
idol: 86,
grace: 86,
cyrus: 87,
jupiter: 88,
saturn: 89,
galacticgruntf: 90,
argenta: 91,
palmer: 92,
thorton: 93,
buck: 94,
darach: 95,
marley: 96,
mira: 97,
cheryl: 98,
riley: 99,
dahlia: 100,
ethan: 101,
lyra: 102,
archer: 132,
ariana: 133,
proton: 134,
petrel: 135,
mysteryman: 136,
eusine: 136,
ptlucas: 137,
ptdawn: 138,
falkner: 141,
bugsy: 142,
whitney: 143,
morty: 144,
chuck: 145,
jasmine: 146,
pryce: 147,
clair: 148,
will: 149,
koga: 150,
bruno: 151,
karen: 152,
lance: 153,
brock: 154,
misty: 155,
ltsurge: 156,
erika: 157,
janine: 158,
sabrina: 159,
blaine: 160,
blue: 161,
red2: 162,
red: 163,
silver: 164,
giovanni: 165,
unknownf: 166,
unknownm: 167,
unknown: 168,
hilbert: 169,
hilda: 170,
chili: 179,
cilan: 180,
cress: 181,
lenora: 188,
burgh: 189,
elesa: 190,
clay: 191,
skyla: 192,
cheren: 206,
bianca: 207,
n: 209,
brycen: 222,
iris: 223,
drayden: 224,
shauntal: 246,
marshal: 247,
grimsley: 248,
caitlin: 249,
ghetsis: 250,
ingo: 256,
alder: 257,
cynthia: 260,
emmet: 261,
dueldiskhilbert: 262,
dueldiskhilda: 263,
hugh: 264,
rosa: 265,
nate: 266,
colress: 267,
bw2beauty: 268,
bw2ghetsis: 269,
bw2plasmagrunt: 270,
bw2plasmagruntf: 271,
bw2iris: 272,
brycenman: 273,
shadowtriad: 274,
rood: 275,
zinzolin: 276,
bw2cheren: 277,
marlon: 278,
roxie: 279,
roxanne: 280,
brawly: 281,
wattson: 282,
flannery: 283,
norman: 284,
winona: 285,
tate: 286,
liza: 287,
juan: 288,
guitarist: 289,
steven: 290,
wallace: 291,
magicqueen: 292,
bellelba: 292,
benga: 293,
bw2elesa: '#bw2elesa',
teamrocket: '#teamrocket',
yellow: '#yellow',
zinnia: '#zinnia',
clemont: '#clemont',
wally: '#wally',
};
if (avatarTable.hasOwnProperty(avatarid)) {
avatar = avatarTable[avatarid];
} else {
avatar = parseInt(avatarid);
}
if (typeof avatar === 'number' && (!avatar || avatar > 294 || avatar < 1)) {
if (!parts[1]) {
this.errorReply("Invalid avatar.");
}
return false;
}
user.avatar = avatar;
if (!parts[1]) {
this.sendReply("Avatar changed to:\n" +
'|raw|<img src="//play.pokemonshowdown.com/sprites/trainers/' + (typeof avatar === 'string' ? avatar.substr(1) : avatar) + '.png" alt="" width="80" height="80" />');
}
if (typeof avatar === 'number' && (avatar === 500)) {
this.sendReply("Avatar changed to:\n" +
'|raw|<img src="https://vignette.wikia.nocookie.net/pokemon/images/a/a7/E_243_front.gif/revision/latest?cb=20140903211959" width="80" height="80"');
}
},
avatarhelp: [`/avatar [avatar number 1 to 293] - Change your trainer sprite.`],
'!logout': true,
signout: 'logout',
logout: function (target, room, user) {
user.resetName();
},
r: 'reply',
reply: function (target, room, user) {
if (!target) return this.parse('/help reply');
if (!user.lastPM) {
return this.errorReply("No one has PMed you yet.");
}
return this.parse('/msg ' + (user.lastPM || '') + ', ' + target);
},
replyhelp: [`/reply OR /r [message] - Send a private message to the last person you received a message from, or sent a message to.`],
'!msg': true,
pm: 'msg',
whisper: 'msg',
w: 'msg',
msg: function (target, room, user, connection) {
if (!target) return this.parse('/help msg');
target = this.splitTarget(target);
let targetUser = this.targetUser;
if (!target) {
this.errorReply("You forgot the comma.");
return this.parse('/help msg');
}
if (!targetUser) {
let error = `User ${this.targetUsername} not found. Did you misspell their name?`;
error = `|pm|${this.user.getIdentity()}| ${this.targetUsername}|/error ${error}`;
connection.send(error);
return;
}
this.pmTarget = targetUser;
this.room = undefined;
if (!targetUser.connected) {
return this.errorReply("User " + this.targetUsername + " is offline.");
}
this.parse(target);
},
msghelp: [`/msg OR /whisper OR /w [username], [message] - Send a private message.`],
'!invite': true,
inv: 'invite',
invite: function (target, room, user) {
if (!target) return this.parse('/help invite');
if (!this.canTalk()) return;
if (room) target = this.splitTarget(target) || room.id;
let targetRoom = Rooms.search(target);
if (targetRoom && !targetRoom.checkModjoin(user)) {
targetRoom = undefined;
}
if (room) {
if (!this.targetUser) return this.errorReply(`The user "${this.targetUsername}" was not found.`);
if (!targetRoom) return this.errorReply(`The room "${target}" was not found.`);
return this.parse(`/pm ${this.targetUsername}, /invite ${targetRoom.id}`);
}
let targetUser = this.pmTarget;
if (!targetRoom || targetRoom === Rooms.global) return this.errorReply(`The room "${target}" was not found.`);
if (targetRoom.staffRoom && !targetUser.isStaff) return this.errorReply(`User "${targetUser.name}" requires global auth to join room "${targetRoom.id}".`);
if (!targetUser) return this.errorReply(`The user "${targetUser.name}" was not found.`);
if (!targetRoom.checkModjoin(targetUser)) {
this.room = targetRoom;
this.parse(`/roomvoice ${targetUser.name}`);
if (!targetRoom.checkModjoin(targetUser)) {
return this.errorReply(`You do not have permission to invite people into this room.`);
}
}
if (targetUser in targetRoom.users) return this.errorReply(`This user is already in "${targetRoom.title}".`);
return '/invite ' + targetRoom.id;
},
invitehelp: [
`/invite [username] - Invites the player [username] to join the room you sent the command to.`,
`(in a PM) /invite [roomname] - Invites the player you're PMing to join the room [roomname].`,
],
pminfobox: function (target, room, user, connection) {
if (!this.canTalk()) return;
if (!this.can('addhtml', null, room)) return false;
if (!target) return this.parse("/help pminfobox");
target = this.canHTML(this.splitTarget(target));
if (!target) return;
let targetUser = this.targetUser;
if (!targetUser || !targetUser.connected) return this.errorReply(`User ${this.targetUsername} is not currently online.`);
if (!(targetUser in room.users) && !user.can('addhtml')) return this.errorReply("You do not have permission to use this command to users who are not in this room.");
if (targetUser.ignorePMs && targetUser.ignorePMs !== user.group && !user.can('lock')) return this.errorReply("This user is currently ignoring PMs.");
if (targetUser.locked && !user.can('lock')) return this.errorReply("This user is currently locked, so you cannot send them a pminfobox.");
// Apply the infobox to the message
target = `/raw <div class="infobox">${target}</div>`;
let message = `|pm|${user.getIdentity()}|${targetUser.getIdentity()}|${target}`;
user.send(message);
if (targetUser !== user) targetUser.send(message);
targetUser.lastPM = user.userid;
user.lastPM = targetUser.userid;
},
pminfoboxhelp: [`/pminfobox [user], [html]- PMs an [html] infobox to [user]. Requires * ~`],
'!ignorepms': true,
blockpm: 'ignorepms',
blockpms: 'ignorepms',
ignorepm: 'ignorepms',
ignorepms: function (target, room, user) {
if (user.ignorePMs === (target || true)) return this.errorReply("You are already blocking private messages! To unblock, use /unblockpms");
if (user.can('lock') && !user.can('bypassall')) return this.errorReply("You are not allowed to block private messages.");
user.ignorePMs = true;
if (target in Config.groups) {
user.ignorePMs = target;
return this.sendReply("You are now blocking private messages, except from staff and " + target + ".");
}
return this.sendReply("You are now blocking private messages, except from staff.");
},
ignorepmshelp: [`/blockpms - Blocks private messages. Unblock them with /unignorepms.`],
'!unignorepms': true,
unblockpm: 'unignorepms',
unblockpms: 'unignorepms',
unignorepm: 'unignorepms',
unignorepms: function (target, room, user) {
if (!user.ignorePMs) return this.errorReply("You are not blocking private messages! To block, use /blockpms");
user.ignorePMs = false;
return this.sendReply("You are no longer blocking private messages.");
},
unignorepmshelp: [`/unblockpms - Unblocks private messages. Block them with /blockpms.`],
'!away': true,
idle: 'away',
afk: 'away',
away: function (target, room, user) {
this.parse('/blockchallenges');
this.parse('/blockpms ' + target);
},
awayhelp: [`/away - Blocks challenges and private messages. Unblock them with /back.`],
'!back': true,
unaway: 'back',
unafk: 'back',
back: function () {
this.parse('/unblockpms');
this.parse('/unblockchallenges');
},
backhelp: [`/back - Unblocks challenges and/or private messages, if either are blocked.`],
'!rank': true,
rank: function (target, room, user) {
if (!target) target = user.name;
Ladders.visualizeAll(target).then(values => {
let buffer = '<div class="ladder"><table>';
buffer += '<tr><td colspan="8">User: <strong>' + Chat.escapeHTML(target) + '</strong></td></tr>';
let ratings = values.join('');
if (!ratings) {
buffer += '<tr><td colspan="8"><em>This user has not played any ladder games yet.</em></td></tr>';
} else {
buffer += '<tr><th>Format</th><th><abbr title="Elo rating">Elo</abbr></th><th>W</th><th>L</th><th>Total</th>';
buffer += ratings;
}
buffer += '</table></div>';
this.sendReply('|raw|' + buffer);
});
},
makeprivatechatroom: 'makechatroom',
makechatroom: function (target, room, user, connection, cmd) {
if (!this.can('makeroom')) return;
// `,` is a delimiter used by a lot of /commands
// `|` and `[` are delimiters used by the protocol
// `-` has special meaning in roomids
if (target.includes(',') || target.includes('|') || target.includes('[') || target.includes('-')) {
return this.errorReply("Room titles can't contain any of: ,|[-");
}
let id = toId(target);
if (!id) return this.parse('/help makechatroom');
if (id.length > MAX_CHATROOM_ID_LENGTH) return this.errorReply("The given room title is too long.");
// Check if the name already exists as a room or alias
if (Rooms.search(id)) return this.errorReply(`The room '${target}' already exists.`);
if (!Rooms.global.addChatRoom(target)) return this.errorReply(`An error occurred while trying to create the room '${target}'.`);
if (cmd === 'makeprivatechatroom') {
let targetRoom = Rooms.search(target);
targetRoom.isPrivate = true;
targetRoom.chatRoomData.isPrivate = true;
Rooms.global.writeChatRoomData();
if (Rooms.get('upperstaff')) {
Rooms.get('upperstaff').add(`|raw|<div class="broadcast-green">Private chat room created: <b>${Chat.escapeHTML(target)}</b></div>`).update();
}
this.sendReply(`The private chat room '${target}' was created.`);
} else {
if (Rooms.get('staff')) {
Rooms.get('staff').add(`|raw|<div class="broadcast-green">Public chat room created: <b>${Chat.escapeHTML(target)}</b></div>`).update();
}
if (Rooms.get('upperstaff')) {
Rooms.get('upperstaff').add(`|raw|<div class="broadcast-green">Public chat room created: <b>${Chat.escapeHTML(target)}</b></div>`).update();
}
this.sendReply(`The chat room '${target}' was created.`);
}
},
makechatroomhelp: [`/makechatroom [roomname] - Creates a new room named [roomname]. Requires: & ~`],
makegroupchat: function (target, room, user, connection, cmd) {
if (!user.autoconfirmed) {
return this.errorReply("You must be autoconfirmed to make a groupchat.");
}
if (!user.trusted) {
return this.errorReply("You must be global voice or roomdriver+ in some public room to make a groupchat.");
}
// if (!this.can('makegroupchat')) return false;
if (target.length > 64) return this.errorReply("Title must be under 32 characters long.");
let targets = target.split(',', 2);
// Title defaults to a random 8-digit number.
let title = targets[0].trim();
if (title.length >= 32) {
return this.errorReply("Title must be under 32 characters long.");
} else if (!title) {
title = ('' + Math.floor(Math.random() * 100000000));
} else if (Config.chatfilter) {
let filterResult = Config.chatfilter.call(this, title, user, null, connection);
if (!filterResult) return;
if (title !== filterResult) {
return this.errorReply("Invalid title.");
}
}
// `,` is a delimiter used by a lot of /commands
// `|` and `[` are delimiters used by the protocol
// `-` has special meaning in roomids
if (title.includes(',') || title.includes('|') || title.includes('[') || title.includes('-')) {
return this.errorReply("Room titles can't contain any of: ,|[-");
}
// Even though they're different namespaces, to cut down on confusion, you
// can't share names with registered chatrooms.
let existingRoom = Rooms.search(toId(title));
if (existingRoom && !existingRoom.modjoin) return this.errorReply("The room '" + title + "' already exists.");
// Room IDs for groupchats are groupchat-TITLEID
let titleid = toId(title);
if (!titleid) {
titleid = '' + Math.floor(Math.random() * 100000000);
}
let roomid = 'groupchat-' + user.userid + '-' + titleid;
// Titles must be unique.
if (Rooms.search(roomid)) return this.errorReply("A group chat named '" + title + "' already exists.");
// Tab title is prefixed with '[G]' to distinguish groupchats from
// registered chatrooms
if (Monitor.countGroupChat(connection.ip)) {
this.errorReply("Due to high load, you are limited to creating 4 group chats every hour.");
return;
}
// Privacy settings, default to hidden.
let privacy = (toId(targets[1]) === 'private') ? true : 'hidden';
let groupChatLink = '<code><<' + roomid + '>></code>';
let groupChatURL = '';
if (Config.serverid) {
groupChatURL = 'http://' + (Config.serverid === 'showdown' ? 'psim.us' : Config.serverid + '.psim.us') + '/' + roomid;
groupChatLink = '<a href="' + groupChatURL + '">' + groupChatLink + '</a>';
}
let titleHTML = '';
if (/^[0-9]+$/.test(title)) {
titleHTML = groupChatLink;
} else {
titleHTML = Chat.escapeHTML(title) + ' <small style="font-weight:normal;font-size:9pt">' + groupChatLink + '</small>';
}
let targetRoom = Rooms.createChatRoom(roomid, '[G] ' + title, {
isPersonal: true,
isPrivate: privacy,
auth: {},
introMessage: '<h2 style="margin-top:0">' + titleHTML + '</h2><p>There are several ways to invite people:<br />- in this chat: <code>/invite USERNAME</code><br />- anywhere in PS: link to <code><<' + roomid + '>></code>' + (groupChatURL ? '<br />- outside of PS: link to <a href="' + groupChatURL + '">' + groupChatURL + '</a>' : '') + '</p><p>This room will expire after 40 minutes of inactivity or when the server is restarted.</p><p style="margin-bottom:0"><button name="send" value="/roomhelp">Room management</button>',
staffMessage: `<p>As creator of this groupchat, <u>you are entirely responsible for what occurs in this chatroom</u>. Global rules apply at all times.</p><p>If you have created this room for someone else, <u>you are still responsible</u> whether or not you choose to actively supervise the room.</p><p style="font-style:italic">For this reason, we strongly recommend that you only create groupchats for users you know and trust.</p><p>If this room is used to break global rules or disrupt other areas of the server, this will be considered irresponsible use of auth privileges on the part of the creator, and <b>you will be globally demoted and barred from public auth.</b></p>`,
});
if (targetRoom) {
// The creator is RO.
targetRoom.auth[user.userid] = '#';
// Join after creating room. No other response is given.
user.joinRoom(targetRoom.id);
user.popup(`You've just made a groupchat; it is now your responsibility, regardless of whether or not you actively partake in the room. For more info, read your groupchat's staff intro.`);
return;
}
return this.errorReply("An unknown error occurred while trying to create the room '" + title + "'.");
},
makegroupchathelp: [`/makegroupchat [roomname], [hidden|private] - Creates a group chat named [roomname]. Leave off privacy to default to hidden. Requires global voice or roomdriver+ in a public room to make a groupchat.`],
deregisterchatroom: function (target, room, user) {
if (!this.can('makeroom')) return;
this.errorReply("NOTE: You probably want to use `/deleteroom` now that it exists.");
let id = toId(target);
if (!id) return this.parse('/help deregisterchatroom');
let targetRoom = Rooms.search(id);
if (!targetRoom) return this.errorReply("The room '" + target + "' doesn't exist.");
target = targetRoom.title || targetRoom.id;
const isPrivate = targetRoom.isPrivate;
const staffRoom = Rooms('staff');
const upperStaffRoom = Rooms('upperstaff');
if (Rooms.global.deregisterChatRoom(id)) {
this.sendReply("The room '" + target + "' was deregistered.");
this.sendReply("It will be deleted as of the next server restart.");
target = Chat.escapeHTML(target);
if (isPrivate) {
if (upperStaffRoom) upperStaffRoom.add(`|raw|<div class="broadcast-red">Private chat room deregistered by ${user.userid}: <b>${target}</b></div>`).update();
} else {
if (staffRoom) staffRoom.add(`|raw|<div class="broadcast-red">Public chat room deregistered: <b>${target}</b></div>`).update();
if (upperStaffRoom) upperStaffRoom.add(`|raw|<div class="broadcast-red">Public chat room deregistered by ${user.userid}: <b>${target}</b></div>`).update();
}
return;
}
return this.errorReply(`The room "${target}" isn't registered.`);
},
deregisterchatroomhelp: [`/deregisterchatroom [roomname] - Deletes room [roomname] after the next server restart. Requires: & ~`],
deletechatroom: 'deleteroom',
deletegroupchat: 'deleteroom',
deleteroom: function (target, room, user, connection, cmd) {
let roomid = target.trim();
if (!roomid) {
// allow deleting personal rooms without typing out the room name
if (room.isPersonal && cmd === "deletegroupchat") {
roomid = room.id;
} else {
return this.parse(`/help deleteroom`);
}
} else {
let targetRoom = Rooms.search(roomid);
if (targetRoom !== room) {
return this.parse(`/help deleteroom`);
}
}
if (room.isPersonal) {
if (!this.can('editroom', null, room)) return;
} else {
if (!this.can('makeroom')) return;
}
const title = room.title || room.id;
if (room.id === 'global') {
return this.errorReply(`This room can't be deleted.`);
}
if (room.chatRoomData) {
if (room.isPrivate) {
if (Rooms.get('upperstaff')) {
Rooms.get('upperstaff').add(Chat.html`|raw|<div class="broadcast-red">Private chat room deleted by ${Server.nameColor(user.userid)}: <b>${title}</b></div>`).update();
}
} else {
if (Rooms.get('staff')) {
Rooms.get('staff').add(Chat.html`|raw|<div class="broadcast-red">Public chat room deleted: <b>${title}</b></div>`).update();
}
if (Rooms.get('upperstaff')) {
Rooms.get('upperstaff').add(Chat.html`|raw|<div class="broadcast-red">Public chat room deleted by ${Server.nameColor(user.userid)}: <b>${title}</b></div>`).update();
}
}
}
if (room.subRooms) {
for (const subRoom of room.subRooms) subRoom.parent = null;
}
const parent = room.parent;
if (parent && parent.subRooms) {
parent.subRooms.delete(room.id);
if (!parent.subRooms.size) parent.subRooms = null;
}
room.add(`|raw|<div class="broadcast-red"><b>This room has been deleted.</b></div>`);
room.update(); // |expire| needs to be its own message
room.add(`|expire|This room has been deleted.`);
this.sendReply(`The room "${title}" was deleted.`);
room.update();
room.destroy();
},
deleteroomhelp: [
`/deleteroom [roomname] - Deletes room [roomname]. Must be typed in the room to delete. Requires: & ~`,
`/deletegroupchat - Deletes the current room, if it's a groupchat. Requires: & ~ #`,
],
hideroom: 'privateroom',
hiddenroom: 'privateroom',
secretroom: 'privateroom',
publicroom: 'privateroom',
privateroom: function (target, room, user, connection, cmd) {
if (room.battle || room.isPersonal) {
if (!this.can('editroom', null, room)) return;
} else {
// registered chatrooms show up on the room list and so require
// higher permissions to modify privacy settings
if (!this.can('makeroom')) return;
}
let setting;
switch (cmd) {
case 'privateroom':
return this.parse('/help privateroom');
case 'publicroom':
setting = false;
break;
case 'secretroom':
setting = true;
break;
default:
if (room.isPrivate === true && target !== 'force') {
return this.sendReply(`This room is a secret room. Use "/publicroom" to make it public, or "/hiddenroom force" to force it hidden.`);
}
setting = 'hidden';
break;
}
if ((setting === true || room.isPrivate === true) && !room.isPersonal) {
if (!this.can('makeroom')) return;
}
if (this.meansNo(target) || !setting) {
if (!room.isPrivate) {
return this.errorReply(`This room is already public.`);
}
if (room.isPersonal) return this.errorReply(`This room can't be made public.`);
if (room.privacySetter && user.can('nooverride', null, room) && !user.can('makeroom')) {
if (!room.privacySetter.has(user.userid)) {
const privacySetters = Array.from(room.privacySetter).join(', ');
return this.errorReply(`You can't make the room public since you didn't make it private - only ${privacySetters} can.`);
}
room.privacySetter.delete(user.userid);
if (room.privacySetter.size) {
const privacySetters = Array.from(room.privacySetter).join(', ');
return this.sendReply(`You are no longer forcing the room to stay private, but ${privacySetters} also need${Chat.plural(room.privacySetter, '', 's')} to use /publicroom to make the room public.`);
}
}
delete room.isPrivate;
room.privacySetter = null;
this.addModAction(`${user.name} made this room public.`);
this.modlog('PUBLICROOM');
if (room.chatRoomData) {
delete room.chatRoomData.isPrivate;
Rooms.global.writeChatRoomData();
}
} else {
const settingName = (setting === true ? 'secret' : setting);
if (room.subRooms) return this.errorReply("Private rooms cannot have subrooms.");
if (room.isPrivate === setting) {
if (room.privacySetter && !room.privacySetter.has(user.userid)) {
room.privacySetter.add(user.userid);
return this.sendReply(`This room is already ${settingName}, but is now forced to stay that way until you use /publicroom.`);
}
return this.errorReply(`This room is already ${settingName}.`);
}
room.isPrivate = setting;
this.addModAction(`${user.name} made this room ${settingName}.`);
this.modlog(`${settingName.toUpperCase()}ROOM`);
if (room.chatRoomData) {
room.chatRoomData.isPrivate = setting;
Rooms.global.writeChatRoomData();
}
room.privacySetter = new Set([user.userid]);
}
},
privateroomhelp: [
`/secretroom - Makes a room secret. Secret rooms are visible to & and up. Requires: & ~`,
`/hiddenroom [on/off] - Makes a room hidden. Hidden rooms are visible to % and up, and inherit global ranks. Requires: \u2606 & ~`,
`/publicroom - Makes a room public. Requires: \u2606 & ~`,
],
officialchatroom: 'officialroom',
officialroom: function (target, room, user) {
if (!this.can('makeroom')) return;
if (!room.chatRoomData) {
return this.errorReply(`/officialroom - This room can't be made official`);
}
if (this.meansNo(target)) {
if (!room.isOfficial) return this.errorReply(`This chat room is already unofficial.`);
delete room.isOfficial;
this.addModAction(`${user.name} made this chat room unofficial.`);
this.modlog('UNOFFICIALROOM');
delete room.chatRoomData.isOfficial;
Rooms.global.writeChatRoomData();
} else {
if (room.isOfficial) return this.errorReply(`This chat room is already official.`);
room.isOfficial = true;
this.addModAction(`${user.name} made this chat room official.`);
this.modlog('OFFICIALROOM');
room.chatRoomData.isOfficial = true;
Rooms.global.writeChatRoomData();
}
},
psplwinnerroom: function (target, room, user) {
if (!this.can('makeroom')) return;
if (!room.chatRoomData) {
return this.errorReply(`/psplwinnerroom - This room can't be marked as a PSPL Winner room`);
}
if (this.meansNo(target)) {
if (!room.pspl) return this.errorReply(`This chat room is already not a PSPL Winner room.`);
delete room.pspl;
this.addModAction(`${user.name} made this chat room no longer a PSPL Winner room.`);
this.modlog('PSPLROOM');
delete room.chatRoomData.pspl;
Rooms.global.writeChatRoomData();
} else {
if (room.pspl) return this.errorReply("This chat room is already a PSPL Winner room.");
room.pspl = true;
this.addModAction(`${user.name} made this chat room a PSPL Winner room.`);
this.modlog('UNPSPLROOM');
room.chatRoomData.pspl = true;
Rooms.global.writeChatRoomData();
}
},
setsubroom: 'subroom',
subroom: function (target, room, user) {
if (!user.can('makeroom')) return this.errorReply(`/subroom - Access denied. Did you mean /subrooms?`);
if (!target) return this.parse('/help subroom');
if (!room.chatRoomData) return this.errorReply(`Temporary rooms cannot be subrooms.`);
if (room.parent) return this.errorReply(`This room is already a subroom. To change which room this subroom belongs to, remove the subroom first.`);
if (room.subRooms) return this.errorReply(`This room is already a parent room, and a parent room cannot be made as a subroom.`);
const main = Rooms.search(target);
if (!main) return this.errorReply(`The room '${target}' does not exist.`);
if (main.isPrivate || !main.chatRoomData) return this.errorReply(`Only public rooms can have subrooms.`);
if (room === main) return this.errorReply(`You cannot set a room to be a subroom of itself.`);
room.parent = main;
if (!main.subRooms) main.subRooms = new Map();
main.subRooms.set(room.id, room);
const mainIdx = Rooms.global.chatRoomDataList.findIndex(r => r.title === main.title);
const subIdx = Rooms.global.chatRoomDataList.findIndex(r => r.title === room.title);
// This is needed to ensure that the main room gets loaded before the subroom.
if (mainIdx > subIdx) {
const tmp = Rooms.global.chatRoomDataList[mainIdx];
Rooms.global.chatRoomDataList[mainIdx] = Rooms.global.chatRoomDataList[subIdx];
Rooms.global.chatRoomDataList[subIdx] = tmp;
}
room.chatRoomData.parentid = main.id;
Rooms.global.writeChatRoomData();
for (let userid in room.users) {
room.users[userid].updateIdentity(room.id);
}
this.modlog('SUBROOM', null, `of ${main.title}`);
return this.addModAction(`This room was set as a subroom of ${main.title} by ${user.name}.`);
},
removesubroom: 'unsubroom',
desubroom: 'unsubroom',
unsubroom: function (target, room, user) {
if (!this.can('makeroom')) return;
if (!room.parent || !room.chatRoomData) return this.errorReply(`This room is not currently a subroom of a public room.`);
const parent = room.parent;
if (parent && parent.subRooms) {
parent.subRooms.delete(room.id);
if (!parent.subRooms.size) parent.subRooms = null;
}
room.parent = null;
delete room.chatRoomData.parentid;
Rooms.global.writeChatRoomData();
for (let userid in room.users) {
room.users[userid].updateIdentity(room.id);
}
this.modlog('UNSUBROOM');
return this.addModAction(`This room was unset as a subroom by ${user.name}.`);
},