-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommands.js
559 lines (511 loc) · 23.7 KB
/
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
global.Banlist = JSON.parse(FS.readFileSync("data/banlist.json"));
global.PokeDex = require("./data/pokedex.js");
global.fdata = require("./data/formats-data.js");
global.Items = require("./data/items.js");
global.Gen8fdata = require("./data/gen8/formats-data.js");
global.Gen8PokeDex = require("./data/gen8/pokedex.js");
global.Gen8Items = require("./data/gen8/items.js");
let commands = {
// Utilities
stat: function (room, user, args) {
let target = user.can(room, "+") ? room : user;
args = args[0].split(" ");
let pokemon = args[0];
let stat = toId(args[1]);
let invest = args[2];
let boost = args[3];
let stats = ["hp", "atk", "def", "spa", "spd", "spe"];
if (!pokemon) {
target.send("Usage: .stat [pokemon], [hp/atk/def/spa/spd/spe], [ivs:evs], [boost]");
return [null];
}
if (!stat || stats.indexOf(stat) === -1) {
target.send("No valid stat given. (hp/atk/def/spa/spd/spe)");
return [null];
}
if (!invest) invest = "0";
if (!boost) boost = "0";
let invests = invest.split(":");
let ev,
iv,
nature = 1;
if (invests[1]) {
if (invests[1].indexOf("+") != -1) {
nature = "1.1";
invests[1] = invests[1].substring(0, invests[1].length - 1);
}
if (invests[1].indexOf("-") != -1) {
nature = "0.9";
invests[1] = invests[1].substring(0, invests[1].length - 1);
}
ev = parseInt(invests[1]);
iv = parseInt(invests[0]);
} else {
if (invests[0].indexOf("+") != -1) {
nature = "1.1";
invests[0] = invests[0].substring(0, invests[0].length - 1);
}
if (invests[0].indexOf("-") != -1) {
nature = "0.9";
invests[0] = invests[0].substring(0, invests[0].length - 1);
}
ev = parseInt(invests[0]);
iv = 31;
}
let mon = PokeDex[toId(pokemon)];
if (!mon) {
target.send(`${pokemon} is not a valid pokemon`);
return [null];
}
if (stat === "hp" && toId(pokemon) === "shedinja") return 1;
if (boost.startsWith("+")) {
boost = 1 + 0.5 * parseInt(boost.substring(1));
} else if (boost.startsWith("-")) {
boost = 1 / (1 + 0.5 * parseInt(boost.substring(1)));
} else {
boost = 1;
}
// Check for dumb shit
if (boost > 4 || boost < 0.25) {
target.send("Boost must be between +6 and -6");
return [null];
}
if (ev > 252 || ev < 0) {
target.send("ev must be between 0 and 252");
return [null];
}
if (iv > 31 || iv < 0) {
target.send("iv must be between 0 and 31");
return [null];
}
let fin = 0;
if (stat === "hp") {
fin = Math.floor(mon.baseStats[stat] * 2 + iv + ev / 4 + 110);
} else {
fin = Math.floor((mon.baseStats[stat] * 2 + iv + ev / 4 + 5) * nature);
fin = Math.floor(fin * boost);
}
target.send(fin);
},
th: "tourhistory",
tourhistory: function (room, user, args) {
if (!user.can(room, "+")) return;
if (!room.pasttours.length) return room.send("This room has no past tours recorded.");
room.send("**Tour history** (most recent first): " + room.pasttours.reverse().join(", "));
room.pasttours.reverse();
},
lasttour: function (room, user, args) {
if (!user.can(room, "+")) return;
if (!room.lasttour[0]) return room.send("This room has no past tours recorded.");
let ago = Math.floor((Date.now() - room.lasttour[0]) / 60000);
return room.send(`**${room.lasttour[1]}** ${ago} minute${ago === 1 ? "" : "s"} ago.`);
},
hangmon: function (room, user, args) {
if (!user.can(room, "%")) return;
if (room.tournament) return room.send("You can't play hangman while a tournament is going on");
let mons = Object.values(PokeDex);
let mon = Utils.select(mons);
room.send(`/hangman create ${mon.name}, Generation ${getGen(mon)}`);
},
mail: function (room, user, args, val) {
let target = args[0];
let targetid = toId(target);
if (!val) return user.send("Usage: ``.mail [user], [message]``");
let msg = val.substring(target.length + 1).trim();
if (args.length < 2 || !targetid || !msg) return user.send("Usage: ``.mail [user], [message]``");
let message = `[mail] ${user.name}: ${msg}`;
if (message.length > 300) return user.send("Your message is too long...");
if (Users[targetid]) return Users[targetid].send(message);
FS.readFile(`mail/${targetid}.json`, (err, data) => {
let maildata = [];
if (err) {
} else {
try {
maildata = JSON.parse(data);
} catch (e) {}
}
if (maildata.length === Config.mail.inboxSize) return user.send("That user's mailbox is full.");
maildata.push(message);
FS.writeFile(`mail/${targetid}.json`, JSON.stringify(maildata, null, 4), (err) => {
if (err) throw err;
user.send("Mail sent successfully.");
});
});
},
// Staff things
settype: "st",
st: function (room, user, args) {
if (!user.can(room, "%")) return;
let type = args[0];
if (!type) return;
console.log(type);
if (type.startsWith("rr")) {
let count = parseInt(type.substring(2));
if (count) room.send("/tour settype rr,, " + count);
else room.send("/tour settype rr");
} else if (type.startsWith("e")) {
let count = parseInt(type.substring(1));
if (count) room.send("/tour settype elim,, " + count);
else room.send("/tour settype elim");
} else {
room.send("Invalid type.");
}
},
modnote: function (room, user, args, val) {
console.log("test");
if (room != user) return;
console.log("test 2");
if (!args[0]) return user.send("Usage: ``.modnote [room], [message]``");
room = Utils.toRoomId(args[0]);
console.log(Object.keys(Rooms));
console.log(Rooms[room]);
if (!Rooms[room]) return user.send("Room doesn't exist, or I'm not in it");
let self = Users[toId(Config.username)];
if (self.rooms[room] != "*") return user.send("I'm not a bot in that room");
if (!user.can(room, "%")) return user.send("Access denied.");
let escape = require("escape-html");
let msg = val.substring(args[0].length + 1).trim();
if (Config.devs.indexOf(user.id) == -1) msg = escape(msg);
let ret = `/addrankhtmlbox %,<b>${escape(user.rooms[room])}${
user.name
}:</b> ${msg}<br><span style='color:#444444;font-size:10px'>Note: Only users ranked % and above can see this.</span>`;
Send(room, ret);
},
setrules: function (room, user, args, val) {
if (!user.can(room, "%")) return;
let command = toId(args.shift());
if (!command) return room.send("Usage: ``.setrules [add/remove/ban/unban/clear], [args]``");
if (!room.tournament) return room.send("There is no tournament running in this room.");
console.log(room.tournament.rules);
if (command === "add") {
for (let i of args) {
let rem = false;
for (let x = 0; x < room.tournament.rules.remrules.length; x++) {
if (toId(room.tournament.rules.remrules[x]) === toId(i)) {
rem = true;
room.tournament.rules.remrules.splice(x, 1);
}
}
if (!rem) room.tournament.rules.addrules.push(i);
}
}
if (command === "remove") {
for (let i of args) {
let rem = false;
for (let x = 0; x < room.tournament.rules.addrules.length; x++) {
if (toId(room.tournament.rules.addrules[x]) === toId(i)) {
rem = true;
room.tournament.rules.addrules.splice(x, 1);
}
}
if (!rem) room.tournament.rules.remrules.push(i);
}
}
if (command === "ban") {
for (let i of args) {
let rem = false;
for (let x = 0; x < room.tournament.rules.unbans.length; x++) {
if (toId(room.tournament.rules.unbans[x]) === toId(i)) {
rem = true;
room.tournament.rules.unbans.splice(x, 1);
}
}
if (!rem) room.tournament.rules.bans.push(i);
}
}
if (command === "unban") {
for (let i of args) {
let rem = false;
for (let x = 0; x < room.tournament.rules.bans.length; x++) {
if (toId(room.tournament.rules.bans[x]) === toId(i)) {
rem = true;
room.tournament.rules.bans.splice(x, 1);
}
}
if (!rem) room.tournament.rules.unbans.push(i);
}
}
if (command === "clear") {
room.send("/tour clearrules");
room.tournament.rules = {
bans: [],
unbans: [],
addrules: [],
remrules: [],
};
}
room.updateTourRules();
},
// Dev stuff
git: function (room, user, args) {
let target = user.can(room, "+") ? room : user;
if (!target) target = user;
let msg = "No git url is configured for this bot.";
if (Config.git) msg = Config.git;
target.send(msg);
},
rl: "reload",
reload: function (room, user, args) {
if (!user.can(room, "all")) return;
bot.emit("reload", args[0], room);
},
update: function (room, user, args) {
if (!user.can(room, "all")) return;
if (!Config.git) return room.say("No git url is configured for this bot.");
const child_process = require("child_process");
child_process.execSync("git pull " + Config.git + " master", {
stdio: "inherit",
});
room.send("Code updated to the latest version.");
},
js: "eval",
eval: function (room, user, args, val) {
if (!user.can(room, "all")) return;
if (!room) room = user;
if (!val) return;
try {
let ret = eval(val);
if (ret !== undefined) {
ret = ret.toString();
if (ret.indexOf("\n") !== -1) ret = "!code " + ret;
room.send(JSON.stringify(ret));
}
} catch (e) {
room.send(e.name + ": " + e.message);
}
},
ping: function (room, user, args) {
if (!user.can(room, "all")) return;
if (!room) room = user;
room.send("pong!");
},
join: "joinroom",
joinroom: function (room, user, args) {
if (!user.can(room, "all")) return;
if (!args[0]) return user.send("No room given.");
Send("", "/j " + args[0]);
},
disable: function (room, user, args) {
if (!user.can(room, "all")) return;
room.send("Commands disabled.");
room.settings.disabled = true;
room.saveSettings();
},
echo: {
"": "help",
help: function (room, user, args) {
if (!user.can(room, "%") && room !== user) return;
if (!Users.self.can(room, "*"))
return room.send("Usage: ``.echo create, [time interval], [message interval], [message]``");
let ret = `<details><summary><b>Echo</b></summary><hr>`;
ret += `<b>- create:</b> <code>.echo create, [time interval], [message interval], [message]</code> - requires % @ # & ~<br>`;
ret += `<b>- end:</b> <code>.echo end</code> - requires % @ # & ~`;
ret += "</details>";
room.send("/addhtmlbox " + ret);
},
create: "start",
start: function (room, user, args) {
if (!user.can(room, "%")) return;
if (room.repeat) return room.send("An echo is already running");
let time_interval = parseInt(args.shift());
let msg_interval = parseInt(args.shift());
let message = args.join(",");
if (!message) return this.help(room, user, args);
if (isNaN(time_interval) || time_interval < 30)
return room.send("time interval has to be at least 30 minutes.");
if (isNaN(msg_interval)) return room.send("message interval has to be a number");
let repeat = {
msgs: 0,
last: Date.now(),
mintime: time_interval,
minmsg: msg_interval,
message: message,
};
room.repeat = repeat;
room.saveSettings();
return room.send("Repeat started");
},
end: function (room, user, args) {
if (!user.can(room, "%")) return;
if (!room.repeat) return room.send("No echo is currently running");
let msg = room.repeat.message;
room.repeat = false;
room.saveSettings();
return room.send(`Echo "${msg}" ended.`);
},
},
settings: {
'': function(room, user, args) {
},
help: function(room, user, args) {
let target = user.can(room, '#') ? room : user;
if (args.length === 0) {
target.send("Usage: ``.settings [type], [...options]``. Valid types: clear, mutebot, unmutebot, hellothere, autohidetext, tourmessages, nostaff.");
target.send("For more information use ``.settings help, [type]``.");
return;
}
let topic = toId(args[0]);
if (!Commands.settings[topic]) {
target.send("Usage: ``.settings [type], [...options]``. Valid types: clear, mutebot, unmutebot, hellothere, autohidetext, tourmessages, nostaff.");
target.send("For more information use ``.settings help, [type]``.");
return;
}
if (["clear"].includes(topic)) {
return target.send("Irreversibly clears all settings for the selected room.");
}
if (["disable", "mutebot"].includes(topic)) {
return target.send("Prevents the bot from sending messages in the selected room, effectively disabling it.");
}
if (["unmutebot"].includes(topic)) {
return target.send("Reverts the effects of ``.settings mutebot``");
}
if (["hellothere"].includes(topic)) {
return target.send("Sets the bot responding to ``Hello There`` with ``General Kenobi!`` on or off.");
}
if (["autohide", "autohidetext"].includes(topic)) {
return target.send("If set to on, the bot automatically hidetexts anyone who gets muted or hourmuted in the selected room.");
}
if (["tourmessages"].includes(topic)) {
return target.send("Settings for notifying rooms when tours of specific (or all) formats are made in specific (or all) rooms");
}
if (["nostaff"].includes(topic)) {
return target.send("Setup for notifying staff discord servers if there hasn't been online staff in 5 minutes, or all online staff has been idle for 15 minutes.");
}
},
checkPerms: function(room, user, args) {
let targetroom = room;
if (room === user) {
targetroom = Rooms[Utils.toRoomId(args[0])];
}
if (!targetroom) {
return true;
}
if (!user.can(targetroom, "#")) {
if (room === user) user.send("You do not have permission to change settings in that room.");
return false;
}
return targetroom;
},
clear: function(room, user, args) {
let targetroom = Commands.settings.checkPerms(room, user, args);
if (targetroom === false) return;
if (targetroom === true) return user.send("Usage: ``.settings " + cmd + ", [room]``");
delete targetroom.settings.disabled;
delete targetroom.settings.hellothere;
delete targetroom.settings.OTobj; //obsolete anyway
delete targetroom.settings.autohide;
delete targetroom.settings.webhook;
delete targetroom.settings.tourhook;
delete targetroom.settings.announcedFormats;
delete targetroom.settings.tourRooms;
targetroom.saveSettings();
return room.send("All settings cleared");
},
disable: "mutebot",
unmutebot: "mutebot",
mutebot: function(room, user, args, val, time, cmd) {
let targetroom = Commands.settings.checkPerms(room, user, args);
if (targetroom === false) return;
if (targetroom === true) return user.send("Usage: ``.settings " + cmd + ", [room]``");
targetroom.settings.disabled = cmd != "unmutebot";
targetroom.saveSettings();
room.send(`Bot messages ${cmd === "unmutebot" ? "un" : ""}muted for room ${targetroom.id}`);
},
hellothere: function(room, user, args) {
let targetroom = Commands.settings.checkPerms(room, user, args);
if (targetroom === false) return;
if (targetroom === true) return user.send("Usage: ``.settings hellothere, [room], [on/off]``");
if (targetroom.id === Utils.toRoomId(args[0])) args.shift();
if (args.length != 1 || !["on", "off"].includes(toId(args[0]))) {
if (room === user) return user.send("Usage: ``.settings hellothere, [room], [on/off]``");
return room.send("Usage: ``.settings hellothere, [on/off]``");
}
targetroom.settings.hellothere = toId(args[0]) === "on";
targetroom.saveSettings();
room.send("Hello There auto-response turned " + toId(args[0]) + " for room " + targetroom.id);
},
autohide: "autohidetext",
autohidetext: function(room, user, args, val, time, cmd) {
let targetroom = Commands.settings.checkPerms(room, user, args);
if (targetroom === false) return;
if (targetroom === true) return user.send("Usage: ``.settings " + cmd + ", [room], [on/off]``");
if (targetroom.id === Utils.toRoomId(args[0])) args.shift();
if (args.length != 1 || !["on", "off"].includes(toId(args[0]))) {
if (room === user) return user.send("Usage: ``.settings " + cmd + ", [room], [on/off]``");
return room.send("Usage: ``.settings " + cmd + ", [on/off]``");
}
targetroom.settings.autohide = toId(args[0]) === "on";
targetroom.saveSettings();
room.send("Hello There auto-response turned " + toId(args[0]) + " for room " + targetroom.id);
},
tourmessages: function(room, user, args, val, time, cmd) {
let targetroom = Commands.settings.checkPerms(room, user, args);
if (targetroom === false) return;
if (targetroom === true) return user.send("Usage: ``.settings tourmessages, [room], [addroom/addformat/removeroom/removeformat/list], [room/format]``");
if (targetroom.id === Utils.toRoomId(args[0])) args.shift();
if (args.length !== 2 && args[0] != "list") {
return room.send("Usage: ``.settings tourmessages, [room], [addroom/addformat/removeroom/removeformat/list], [room/format/\"all\"]``");
}
if (toId(args[0]) === "addroom") {
let roomid = Utils.toRoomId(args[1]);
if (!Rooms[roomid] && roomid != "all") return user.send(`I can't help you with the room ${roomid} because I'm not in it. Need it anyway? Ask Felucia!`);
if (!targetroom.settings.tourRooms) targetroom.settings.tourRooms = [];
targetroom.settings.tourRooms.push(roomid);
targetroom.saveSettings();
return user.send("Added the room successfully.")
}
if (toId(args[0]) === "removeroom") {
let roomid = Utils.toRoomId(args[1]);
if (!targetroom.settings.tourRooms) targetroom.settings.tourRooms = [];
if (!targetroom.settings.tourRooms.includes(roomid)) return user.send(`The room ${roomid} already isn't set up, so I can't remove it.`);
targetroom.settings.tourRooms.splice(targetroom.settings.tourRooms.indexOf(roomid), 1);
targetroom.saveSettings();
return user.send("Removed the room successfully.")
}
if (toId(args[0]) === "addformat") {
let format = toId(args[1]);
if (!Tournament.formats[format] && format != "all") return user.send(`I can't help you with the format ${format} because I can't find it.`);
if (!targetroom.settings.announcedFormats) targetroom.settings.announcedFormats = [];
targetroom.settings.announcedFormats.push(format);
targetroom.saveSettings();
return user.send("Added the format successfully.")
}
if (toId(args[0]) === "removeformat") {
let format = toId(args[1]);
if (!targetroom.settings.announcedFormats) targetroom.settings.announcedFormats = [];
if (!targetroom.settings.announcedFormats.includes(format)) return user.send(`The format ${format} already isn't set up, so I can't remove it.`);
targetroom.settings.announcedFormats.splice(targetroom.settings.announcedFormats.indexOf(room), 1);
targetroom.saveSettings();
return user.send("Removed the format successfully.")
}
if (toId(args[0]) === "list") {
let ret = `I will send a message to ${targetroom.id} if a tour is made in one of the following rooms:<br>`;
ret += `${targetroom.settings.tourRooms ? targetroom.settings.tourRooms.join(", ") : ""}<br><br>`;
ret += `in one of the following formats:<br>`;
ret += `${targetroom.settings.announcedFormats ? targetroom.settings.announcedFormats.join(", ") : ""}`;
targetroom.send(`/pminfobox ${user.id}, ${ret}`);
return;
}
},
nostaff: function(room, user, args, val, time, cmd) {
let targetroom = Commands.settings.checkPerms(room, user, args);
if (targetroom === false) return;
if (targetroom === true) return user.send("Please ask Felucia to set this up for you.");
if (targetroom.id === Utils.toRoomId(args[0])) args.shift();
if (!user.can(room, "all")) return user.send("Please ask Felucia to set this up for you.");
targetroom.settings.webhook = args[0]
targetroom.saveSettings();
room.send("Webhook set for " + targetroom.id);
},
}
};
let files = FS.readdirSync("commands");
for (let f in files) {
let file = files[f];
if (file.substring(file.length - 3) !== ".js") continue;
if (require.cache[require.resolve("./commands/" + file)])
delete require.cache[require.resolve("./commands/" + file)];
let contents = require("./commands/" + file);
Object.assign(commands, contents);
}
module.exports = commands;