-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
440 lines (368 loc) · 14.3 KB
/
command.js
File metadata and controls
440 lines (368 loc) · 14.3 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
import * as utils from "./utils.js";
const prefix = ">";
const superUsers = ["Mortis_666", "ddddddddrrdd3"];
const roleValue = ["member", "co-admin", "admin"];
class Command {
constructor() {
this.commands = [];
}
async parse(ctx, msg) {
for (let [command, func] of this.commands) {
if (msg.match(new RegExp("^" + prefix + command + "(\\s+)?"))) {
return await func(
ctx,
...msg.split(/\s+/).slice(1, msg.length),
);
}
}
return [0, null];
}
/**
* @typedef {Object} Context
* @property {import("socket.io").Server} io - io to emit messages to the frontend
* @property {import("socket.io").Socket} socket - io to emit messages to the frontend
* @property {import("mongodb").WithId<import("mongodb").Document>} user - user object
* @property {import("mongodb").WithId<import("mongodb").Document>} room - room object
*/
/**
* @callback callback
* @param {Context} ctx
* @param {...String} args - arguments in the message
* @returns {Promise<[int, String]>}
*/
/**
* @param {String} command - The command name
* @param {callback} callbackFn
*/
on(command, callbackFn) {
this.commands.push([command, callbackFn]);
}
}
export function getRole(user, room) {
if (superUsers.includes(user.username)) return "admin";
return room.visibility == "public" ? "member" : user.rooms[room._id];
}
export const command = new Command();
command.on("delete", async (ctx, msgId) => {
if (msgId) {
let role = getRole(ctx.user, ctx.room);
let message = ctx.room.messages.find(msg => msg.id == msgId);
if (!message) {
return [2000, `Message id ${msgId} doesn't exist`];
} else if (role == "member" && message.author != ctx.user.username) {
return [2000, utils.NO_PERM];
} else {
await utils.deleteMessage(ctx.room._id, msgId);
ctx.io.emit("delete", msgId);
return [2000, `Deleted message ${msgId}`];
}
} else {
return [2000, `Syntax: ${prefix}delete <message_id>`];
}
});
command.on("purge", async (ctx, amt) => {
let role = getRole(ctx.user, ctx.room);
if (role == "member") return [2000, utils.NO_PERM];
if (amt && (!amt.match(/[^0-9]/g) || amt.toLowerCase() == "all")) {
if (amt.toLowerCase() == "all") amt = ctx.room.messages.length + 1;
let deletedMessages = await utils.deleteLastMessages(ctx.room._id, amt);
for (let deletedMessage of deletedMessages) {
ctx.io.emit("delete", deletedMessage.id);
}
return [2000, `Deleted ${amt} messages`];
} else {
return [2000, `Syntax: ${prefix}purge <amount>`];
}
});
command.on("kick", async (ctx, username) => {
if (!username) return [2000, `Syntax: ${prefix}kick <username>`];
if (ctx.room.visibility == "public")
return [2000, "You can't kick anyone out of a public ctx.room!"];
username = username.replace(/^@/, "");
let target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
let authorRole = getRole(ctx.user, ctx.room);
let targetRole = getRole(target, ctx.room);
if (roleValue.indexOf(authorRole) < roleValue.indexOf(targetRole)) {
return [2000, utils.NO_PERM];
} else {
await utils.removeUser(target._id, ctx.room._id.toString());
return [2000, `Kicked ctx.user ${username}`];
}
});
command.on("mute", async (ctx, username, ...reason) => {
if (!username)
return [2000, `Syntax: ${prefix}mute <username> [reason="no reason"]`];
username = username.replace(/^@/, "");
let target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
let authorRole = getRole(ctx.user, ctx.room);
let targetRole = getRole(target, ctx.room);
if (roleValue.indexOf(authorRole) <= roleValue.indexOf(targetRole)) {
return [2000, utils.NO_PERM];
} else {
await utils.mute(
ctx.room._id.toString(),
username,
reason.join(" ") || "no reason",
);
return [2000, `Muted ctx.user ${username}`];
}
});
command.on("unmute", async (ctx, username) => {
if (!username) return [2000, `Syntax: ${prefix}unmute <username>`];
username = username.replace(/^@/, "");
let target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
let authorRole = getRole(ctx.user, ctx.room);
let targetRole = getRole(target, ctx.room);
if (roleValue.indexOf(authorRole) <= roleValue.indexOf(targetRole)) {
return [2000, utils.NO_PERM];
} else {
await utils.unmute(ctx.room._id.toString(), username);
return [2000, `Unmuted ctx.user ${username}`];
}
});
command.on("ban", async (ctx, username, ...reason) => {
if (!username)
return [2000, `Syntax: ${prefix}ban <username> [reason="no reason"]`];
username = username.replace(/^@/, "");
let target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
if (!superUsers.includes(ctx.user.username)) {
return [2000, utils.NO_PERM];
} else {
await utils.ban(target._id.toString(), reason.join(" ") || "no reason");
ctx.socket.emit("ban");
return [2000, `Banned ctx.user ${username}`];
}
});
command.on("unban", async (ctx, username) => {
if (!username)
return [2000, `Syntax: ${prefix}unban <username> [reason="no reason"]`];
username = username.replace(/^@/, "");
let target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
if (!superUsers.includes(ctx.user.username)) {
return [2000, utils.NO_PERM];
} else {
await utils.unban(target._id.toString());
return [2000, `Unbanned ctx.user ${username}`];
}
});
command.on("lock", async ctx => {
if (getRole(ctx.user, ctx.room) == "member") {
return [2000, utils.NO_PERM];
} else {
await utils.lock(ctx.room._id.toString());
return [2000, "Topic locked"];
}
});
command.on("unlock", async ctx => {
if (getRole(ctx.user, ctx.room) == "member") {
return [2000, utils.NO_PERM];
} else {
await utils.unlock(ctx.room._id.toString());
return [2000, "Topic unlocked"];
}
});
command.on("promote", async (ctx, username) => {
if (!username) return [2000, `Syntax: ${prefix}promote <username>`];
if (ctx.room.visibility == "public")
return [0, "You can't promote anyone in public ctx.room"];
username = username.replace(/^@/, "");
let target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
let authorRole = getRole(ctx.user, ctx.room);
let targetRole = getRole(target, ctx.room);
if (authorRole != "admin") {
return [2000, utils.NO_PERM];
} else if (targetRole != "member") {
return [2000, `You can't promote a ${targetRole}!`];
} else {
await utils.assignRole(username, ctx.room._id.toString(), "co-admin");
return [2000, `Promoted ${username} to co-admin`];
}
});
command.on("demote", async (ctx, username) => {
if (!username) return [2000, `Syntax: ${prefix}demote <username>`];
if (ctx.room.visibility == "public")
return [0, "You can't demote anyone in public ctx.room"];
username = username.replace(/^@/, "");
let target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
let authorRole = getRole(ctx.user, ctx.room);
let targetRole = getRole(target, ctx.room);
if (authorRole != "admin") {
return [2000, utils.NO_PERM];
} else if (targetRole != "co-admin") {
return [2000, `You can't demote a ${targetRole}!`];
} else {
await utils.assignRole(username, ctx.room._id.toString(), "member");
return [2000, `Demoted ${username} to member`];
}
});
command.on("check-role", async (ctx, username) => {
if (!username) username = ctx.user.username;
username = username.replace(/^@/, "");
let target = await utils.findUserByUsername(username);
return [
0,
`${username} is a ${getRole(target, ctx.room)} of ${ctx.room.name}`,
];
});
command.on("count-msg", async ctx => {
return [0, `${ctx.room.messages.length} messages`];
});
command.on("define", async (ctx, ...args) => {
if (!args) return [0, `Syntax: ${prefix}define "<phrase>" <definition>`];
let all = args.join(" ");
let word = all.match(/(?<=")[^"]+(?=" )/);
if (all.match(/^"/) && (all.match(/"/g) || []).length > 2)
return [0, 'Too many "double quotes"!!'];
let phrase, definition;
if (word) {
phrase = word[0];
definition = all.replace(phrase, "").split(/\s+/).slice(1).join(" ");
} else if (args.length > 1) {
phrase = args[0];
definition = args.slice(1, args.length).join(" ");
} else {
return [0, "Please provide the definition!"];
}
await utils.defineWord(ctx.room._id.toString(), phrase, definition);
return [0, `Defined ${phrase} as ${definition}`];
});
command.on("whatis", async (ctx, ...args) => {
if (!args) return [0, `Syntax: ${prefix}whatis <phrase>`];
let phrase = args.join(" ");
let definition = await utils.getWordDefinition(
ctx.room._id.toString(),
phrase,
);
if (definition) {
return [0, `${phrase} is ${definition}`];
} else {
return [
0,
`Can't find the definition of ${phrase},
do >define "${phrase}" <definition> to define it`,
];
}
});
command.on("forget", async (ctx, ...args) => {
if (!args) return [0, `Syntax: ${prefix}whatis <phrase>`];
let phrase = args.join(" ");
let definition = await utils.getWordDefinition(
ctx.room._id.toString(),
phrase,
);
if (definition) {
await utils.undefineWord(ctx.room._id.toString(), phrase);
return [0, `Successfully forgotten ${phrase}`];
} else {
return [0, `I've never known the definition of ${phrase}`];
}
});
command.on("remindme", async (ctx, time, ...args) => {
if (!time) {
return [0, `Syntax: ${prefix}remindme <hh:mm:ss> <content>`];
} else if (!time.match(/^(\d?\d:)?(\d?\d:)?\d?\d$/)) {
return [0, "Time format wrong! Use hh:mm:ss time format!"];
} else {
let timeParse = time.split(":");
let hour = timeParse.at(-3) || 0;
let minute = timeParse.at(-2) || 0;
let second = +timeParse.at(-1);
let total = hour * 3600 + minute * 60 + +second;
console.log("🚀 ~ file: command.js:274 ~ command.on ~ total:", total);
if (total === 0) return [0, "The time can't be zero!"];
let content = args.join(" ");
let hourMsg = hour ? ` ${hour} hours `.slice(0, -1 - (hour == 1)) : "";
let minuteMsg = minute
? ` ${minute} minutes `.slice(0, -1 - (minute == 1))
: "";
let secondMsg = ` ${second} seconds `.slice(0, -1 - (second == 1));
setTimeout(async () => {
let now = Date.now();
let msg = `@${ctx.user.username} your timer has ended!\n${content}`;
let systemMsgId = await utils.insertMessage(
ctx.room._id.toString(),
"system",
msg,
now,
"SYSTEM0" + "$",
);
ctx.io.emit(
"msg",
systemMsgId,
"System",
"system",
"/assets/system.png",
ctx.room._id.toString(),
msg,
now,
await utils.findPings(msg),
[],
);
}, total * 1000);
return [
0,
`Ok I will remind you after${hourMsg}${minuteMsg}${secondMsg}.`,
];
}
});
command.on("help-cmd", async ctx => {
return [
0,
`List of cmd:
${prefix}purge <amount> Desc: delete an amount of message
${prefix}delete <message id> Desc: delete a specific message
${prefix}kick <username> Desc: kick users out of topic
${prefix}mute <username> Desc: mute users
${prefix}unmute <username> Desc: unmute users
${prefix}promote <username> Desc: promote users
${prefix}demote <username> Desc: demote users
${prefix}check-role <username> Desc: check role of the ctx.user in this topic
${prefix}count-msg Desc: count the amount of message in this topic
${prefix}define "<phrase>" <definition> Desc: define a word for this topic
${prefix}whatis "<phrase>" Desc: check definition of a word for this topic
${prefix}forget "<phrase>" Desc: delete definition of a word for this topic
${prefix}remindme <hh:mm:ss> <content> Desc: remind you to do something`,
];
});
command.on(">tic-tac-toe", async ctx => {
return [2000, `\n[] [] []\n[] [] []\n[] [] []`];
//how do i get ctx.user msg
});
command.on(">chess", async ctx => {
return [2000, `qxf7 checkmate gg good game`];
});
command.on("showcase", (ctx, arg1, arg2, arg3) => {
// >showcase; => arg1 = undefined; arg2 = undefined; arg3 = undefined
// >showcase hello; => arg1 = "hello"; arg2 = undefined; arg3 = undefined
// >showcase 1 2 3; => arg1 = "1"; arg2 = "2"; arg3 = "3"
// >showcase 1 2 3 4; => arg1 = "1"; arg2 = "2"; arg3 = "3"
// The second one is the message that will be
// sent to the topic
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
return [2000, `arg1=${arg1}; arg2=${arg2}; arg3=${arg3}`];
// ^^^^
// Delete message after this ms
// Put `0` to never delete this message
});
command.on("generate", ctx => {
let consonant = "bcdfghjklmnpqrstvwxyz";
let consonantArray = consonant.split("");
let vowelArray = ["a", "e", "i", "o", "u"];
let randWord = "";
for (let i = 0; i < 10; i++) {
randWord += String(
consonantArray[Math.floor(Math.random() * consonant.length)],
);
randWord += String(
vowelArray[Math.floor(Math.random() * vowelArray.length)],
);
}
return [0, randWord];
});