Skip to content

Commit fab40b6

Browse files
committed
feat: added unblock options for admins
1 parent fb0ca01 commit fab40b6

3 files changed

Lines changed: 52 additions & 16 deletions

File tree

src/commands/block.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { Message } from "../../types/message"
1+
import { Message } from "../../types/message";
22
import log from "../components/utils/log";
33
import { exec } from "child_process";
44
import util from "util";
55
import { prisma } from "../components/prisma";
66

77
export const info = {
88
command: "block",
9-
description: "Block or unblock users from sending messages.",
10-
usage: "block <@user> [--rem]",
9+
description: "Block users from the bot.",
10+
usage: "block <@user>",
1111
example: "block @user123",
1212
role: "admin",
1313
cooldown: 5000,
@@ -19,18 +19,7 @@ export default async function (msg: Message) {
1919
return;
2020
}
2121

22-
if (/^block\s--rem/.test(msg.body)) {
23-
for (const userId of msg.mentionedIds) {
24-
const lid = userId.split("@")[0];
25-
26-
await prisma.block.delete({
27-
where: { lid },
28-
});
29-
}
30-
31-
await msg.react("❌");
32-
return;
33-
}
22+
await msg.react("🔄");
3423

3524
for (const userId of msg.mentionedIds) {
3625
const lid = userId.split("@")[0];

src/commands/unblock.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Message } from "../../types/message";
2+
import log from "../components/utils/log";
3+
import { exec } from "child_process";
4+
import util from "util";
5+
import { prisma } from "../components/prisma";
6+
import { getKey } from "../components/utils/rateLimiter";
7+
import redis from "../components/redis";
8+
9+
export const info = {
10+
command: "unblock",
11+
description: "Unblock the users from the bot & rate limiter.",
12+
usage: "unblock <@user>",
13+
example: "unblock @user123",
14+
role: "admin",
15+
cooldown: 5000,
16+
};
17+
18+
export default async function (msg: Message) {
19+
if (msg.mentionedIds.length === 0) {
20+
await msg.reply("Please mention a user to unblock.");
21+
return;
22+
}
23+
24+
await msg.react("🔄");
25+
26+
for (const userId of msg.mentionedIds) {
27+
const lid = userId.split("@")[0];
28+
29+
await prisma.block.delete({
30+
where: { lid },
31+
});
32+
33+
const key = getKey(userId);
34+
const entryRaw = await redis.get(key);
35+
36+
await redis.set(
37+
key,
38+
JSON.stringify({
39+
timestamps: [] as number[],
40+
penaltyCount: 0,
41+
penaltyUntil: 0,
42+
}),
43+
);
44+
}
45+
46+
await msg.react("✅");
47+
}

src/components/utils/rateLimiter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const LIMIT = 5;
77
const BASE_WINDOW_MS = 30 * 1000;
88
const PENALTY_INCREMENT_MS = 10 * 1000;
99

10-
function getKey(number: string) {
10+
export function getKey(number: string) {
1111
return `rate:${number}`;
1212
}
1313

0 commit comments

Comments
 (0)