Skip to content

Commit 470782c

Browse files
committed
feat: update to block/unblock command
1 parent b060913 commit 470782c

2 files changed

Lines changed: 35 additions & 21 deletions

File tree

src/commands/block.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Message } from "../types/message";
22
import {
33
addBlockUser,
44
deductUserPoints,
5+
getBlockUser,
56
isAdmin,
67
} from "../components/services/user";
78
import client from "../components/client";
@@ -21,17 +22,26 @@ export default async function (msg: Message): Promise<void> {
2122
return;
2223
}
2324

25+
const jid = msg.mentionedIds[0];
26+
const lid = jid.split("@")[0];
27+
2428
const botId = (await client()).info.wid._serialized;
29+
const isUserAdmin = await isAdmin(lid);
30+
31+
if (!msg.fromMe && (isUserAdmin || lid === botId.split("@")[0])) {
32+
await msg.reply(`Unable block an ${isUserAdmin ? "Admin" : "Super Admin"}`);
33+
return;
34+
}
2535

26-
for (const userId of msg.mentionedIds) {
27-
const lid = userId.split("@")[0];
28-
const isUserAdmin = await isAdmin(lid);
29-
if (isUserAdmin || lid === botId.split("@")[0]) {
30-
await msg.reply(
31-
`Unable to block ${isUserAdmin ? "Admin" : "Super Admin"}.`,
32-
);
33-
continue;
34-
}
35-
await Promise.allSettled([addBlockUser(lid), deductUserPoints(lid, 30)]);
36+
const isBlocked = await getBlockUser(lid);
37+
if (isBlocked) {
38+
await msg.reply("The user is already blocked.");
39+
return;
3640
}
41+
42+
await Promise.allSettled([
43+
addBlockUser(lid),
44+
deductUserPoints(lid, 30),
45+
msg.reply("The user has been blocked."),
46+
]);
3747
}

src/commands/unblock.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Message } from "../types/message"
1+
import { Message } from "../types/message";
22
import redis from "../components/redis";
3-
import { unblockUser } from "../components/services/user";
3+
import { getBlockUser, unblockUser } from "../components/services/user";
44

55
export const info = {
66
command: "unblock",
@@ -17,17 +17,21 @@ export default async function (msg: Message): Promise<void> {
1717
return;
1818
}
1919

20-
const lids = msg.mentionedIds.map((id) => id.split("@")[0]);
20+
const jid = msg.mentionedIds[0];
21+
const lid = jid.split("@")[0];
22+
23+
const isBlocked = await getBlockUser(lid);
24+
if (!isBlocked) {
25+
await msg.reply("The user is not block.");
26+
return;
27+
}
2128

2229
await Promise.all([
23-
lids.map((lid) => unblockUser(lid)),
24-
lids.map((lid) =>
25-
redis.set(
26-
`rate:${lid}`,
27-
JSON.stringify({ timestamps: [], penaltyCount: 0, penaltyUntil: 0 }),
28-
),
30+
unblockUser(lid),
31+
redis.set(
32+
`rate:${lid}`,
33+
JSON.stringify({ timestamps: [], penaltyCount: 0, penaltyUntil: 0 }),
2934
),
35+
msg.reply("The user has been unblocked."),
3036
]);
31-
32-
await msg.react("✅");
3337
}

0 commit comments

Comments
 (0)