-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.ts
More file actions
334 lines (308 loc) · 12.1 KB
/
Copy pathcommand.ts
File metadata and controls
334 lines (308 loc) · 12.1 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
import type { RoomDoc, SocketContext, UserDoc } from "./types.ts";
import * as utils from "./utils.ts";
const prefix = ">";
const superUsers = ["Mortis_666", "ddddddddrrdd3"];
const roleValue = ["member", "co-admin", "admin"];
class Command {
commands: Array<
[
string,
(
ctx: SocketContext,
...args: string[]
) => Promise<[number, string | null]> | [number, string | null],
]
> = [];
async parse(ctx: SocketContext, msg: string) {
for (const [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];
}
on(
command: string,
callbackFn: (
ctx: SocketContext,
...args: string[]
) => Promise<[number, string | null]> | [number, string | null],
) {
this.commands.push([command, callbackFn]);
}
}
export function getRole(
user: Pick<UserDoc, "username" | "rooms"> | null | undefined,
room: Pick<RoomDoc, "visibility" | "_id"> | null | undefined,
) {
if (!user || !room) return "member";
if (superUsers.includes(user.username)) return "admin";
return room.visibility === "public"
? "member"
: user.rooms[room._id.toString()];
}
export const command = new Command();
command.on("delete", async (ctx, msgId) => {
if (!msgId) return [2000, `Syntax: ${prefix}delete <message_id>`];
if (!ctx.room?.messages) return [2000, utils.NO_ROOM];
const role = getRole(ctx.user, ctx.room);
const message = ctx.room.messages.find(msg => msg.id === msgId);
if (!message) return [2000, `Message id ${msgId} doesn't exist`];
if (role === "member" && message.author !== ctx.user.username)
return [2000, utils.NO_PERM];
await utils.deleteMessage(ctx.room._id.toString(), msgId);
ctx.io.emit("delete", msgId);
return [2000, `Deleted message ${msgId}`];
});
command.on("purge", async (ctx, amt) => {
const role = getRole(ctx.user, ctx.room);
if (role === "member") return [2000, utils.NO_PERM];
if (!amt || (!/^\d+$/.test(amt) && amt.toLowerCase() !== "all"))
return [2000, `Syntax: ${prefix}purge <amount>`];
if (amt.toLowerCase() === "all") amt = String(ctx.room.messages.length + 1);
const deletedMessages = await utils.deleteLastMessages(
ctx.room._id.toString(),
Number(amt),
);
for (const deletedMessage of deletedMessages)
ctx.io.emit("delete", deletedMessage.id);
return [2000, `Deleted ${amt} messages`];
});
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(/^@/, "");
const target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
const authorRole = getRole(ctx.user, ctx.room);
const targetRole = getRole(target, ctx.room);
if (roleValue.indexOf(authorRole) <= roleValue.indexOf(targetRole))
return [2000, utils.NO_PERM];
await utils.removeUser(target._id.toString(), 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(/^@/, "");
const target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
const authorRole = getRole(ctx.user, ctx.room);
const targetRole = getRole(target, ctx.room);
if (roleValue.indexOf(authorRole) <= roleValue.indexOf(targetRole))
return [2000, utils.NO_PERM];
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(/^@/, "");
const target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
const authorRole = getRole(ctx.user, ctx.room);
const targetRole = getRole(target, ctx.room);
if (roleValue.indexOf(authorRole) <= roleValue.indexOf(targetRole))
return [2000, utils.NO_PERM];
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(/^@/, "");
const 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];
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(/^@/, "");
const 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];
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];
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];
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(/^@/, "");
const target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
const authorRole = getRole(ctx.user, ctx.room);
const targetRole = getRole(target, ctx.room);
if (authorRole !== "admin") return [2000, utils.NO_PERM];
if (targetRole !== "member")
return [2000, `You can't promote a ${targetRole}!`];
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(/^@/, "");
const target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
const authorRole = getRole(ctx.user, ctx.room);
const targetRole = getRole(target, ctx.room);
if (authorRole !== "admin") return [2000, utils.NO_PERM];
if (targetRole !== "co-admin")
return [2000, `You can't demote a ${targetRole}!`];
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(/^@/, "");
const target = await utils.findUserByUsername(username);
if (!target) return [2000, `User ${username} doesn't exist`];
return [
0,
`${username} is a ${getRole(target, ctx.room)} of ${ctx.room.name}`,
];
});
command.on("count-msg", async ctx => [
0,
`${ctx.room.messages.length} messages`,
]);
command.on("define", async (ctx, ...args) => {
if (!args.length)
return [0, `Syntax: ${prefix}define "<phrase>" <definition>`];
const all = args.join(" ");
const word = all.match(/(?<=")[^"]+(?=" )/);
if (all.match(/^"/) && (all.match(/"/g) || []).length > 2)
return [0, 'Too many "double quotes"!!'];
let phrase: string, definition: string;
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).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.length) return [0, `Syntax: ${prefix}whatis <phrase>`];
const phrase = args.join(" ");
const definition = await utils.getWordDefinition(
ctx.room._id.toString(),
phrase,
);
if (definition) return [0, `${phrase} is ${definition}`];
return [
0,
`Can't find the definition of ${phrase},\n do >define "${phrase}" <definition> to define it`,
];
});
command.on("forget", async (ctx, ...args) => {
if (!args.length) return [0, `Syntax: ${prefix}whatis <phrase>`];
const phrase = args.join(" ");
const definition = await utils.getWordDefinition(
ctx.room._id.toString(),
phrase,
);
if (definition) {
await utils.undefineWord(ctx.room._id.toString(), phrase);
return [0, `Successfully forgotten ${phrase}`];
}
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>`];
if (!time.match(/^(\d?\d:)?(\d?\d:)?\d?\d$/))
return [0, "Time format wrong! Use hh:mm:ss time format!"];
const timeParse = time.split(":");
const hour = Number(timeParse.at(-3)) || 0;
const minute = Number(timeParse.at(-2)) || 0;
const second = Number(timeParse.at(-1));
const total = hour * 3600 + minute * 60 + second;
if (total === 0) return [0, "The time can't be zero!"];
const content = args.join(" ");
const hourMsg = hour
? ` ${hour} hours `.slice(0, -(hour === 1 ? 2 : 1))
: "";
const minuteMsg = minute
? ` ${minute} minutes `.slice(0, -(minute === 1 ? 2 : 1))
: "";
const secondMsg = ` ${second} seconds `.slice(0, -(second === 1 ? 2 : 1));
setTimeout(async () => {
const now = Date.now();
const msg = `@${ctx.user.username} your timer has ended!\n${content}`;
const systemMsgId = await utils.insertMessage(
ctx.room._id.toString(),
"system",
msg,
now,
0,
"SYSTEM0$",
);
ctx.io.emit("msg", {
id: systemMsgId,
authorName: "System",
authorUsername: "system",
avatar: "/assets/system.png",
roomId: ctx.room._id.toString(),
content: msg,
time: now,
pings: await utils.findPings(msg),
topicIds: [],
});
}, total * 1000);
return [0, `Ok I will remind you after${hourMsg}${minuteMsg}${secondMsg}.`];
});
command.on("help-cmd", async () => [
0,
`List of cmd:\n ${prefix}purge <amount> Desc: delete an amount of message\n ${prefix}delete <message id> Desc: delete a specific message\n ${prefix}kick <username> Desc: kick users out of topic\n ${prefix}mute <username> Desc: mute users\n ${prefix}unmute <username> Desc: unmute users\n ${prefix}promote <username> Desc: promote users\n ${prefix}demote <username> Desc: demote users\n ${prefix}check-role <username> Desc: check role of the ctx.user in this topic\n ${prefix}count-msg Desc: count the amount of message in this topic\n ${prefix}define "<phrase>" <definition> Desc: define a word for this topic\n ${prefix}whatis "<phrase>" Desc: check definition of a word for this topic\n ${prefix}forget "<phrase>" Desc: delete definition of a word for this topic\n ${prefix}remindme <hh:mm:ss> <content> Desc: remind you to do something`,
]);
command.on(">tic-tac-toe", async () => [
2000,
`\n[] [] []\n[] [] []\n[] [] []`,
]);
command.on(">chess", async () => [2000, `qxf7 checkmate gg good game`]);
command.on("showcase", (_ctx, arg1, arg2, arg3) => [
2000,
`arg1=${arg1}; arg2=${arg2}; arg3=${arg3}`,
]);
command.on("generate", () => {
const consonant = "bcdfghjklmnpqrstvwxyz";
const consonantArray = consonant.split("");
const 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];
});