Skip to content

Commit 231ebda

Browse files
committed
feat: bug fixes and improvements
1 parent bc67cdf commit 231ebda

8 files changed

Lines changed: 64 additions & 49 deletions

File tree

src/commands/event.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,9 @@ export const info = {
1212
export default async function (msg: Message) {
1313
if (!/^event/i.test(msg.body)) return;
1414

15-
let event: Message;
15+
let event: Message = msg;
1616

17-
if (msg.hasQuotedMsg) {
18-
const quotedEvent = await msg.getQuotedMessage();
19-
event = quotedEvent;
20-
} else {
21-
event = msg;
22-
}
17+
if (msg.hasQuotedMsg) event = await msg.getQuotedMessage();
2318

2419
await msg.reply("```" + JSON.stringify(event, null, 2) + "```");
2520
}

src/commands/lid.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Message } from "../../types/message";
2+
import log from "../components/utils/log";
3+
4+
export const info = {
5+
command: "lid",
6+
description: "Get your LID",
7+
usage: "lid",
8+
example: "lid",
9+
role: "user",
10+
cooldown: 5000,
11+
};
12+
13+
export default async function (msg: Message) {
14+
if (!/^lid/i.test(msg.body)) return;
15+
16+
let message: Message = msg;
17+
if (msg.hasQuotedMsg) message = await msg.getQuotedMessage();
18+
19+
const lid = message.author
20+
? message.author.split("@")[0]
21+
: message.from.split("@")[0];
22+
23+
await msg.reply(`
24+
\`Your LID\`
25+
26+
${lid}
27+
`);
28+
}

src/commands/reload.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,18 @@ export default async function (msg: Message) {
3434
}
3535
}
3636

37-
if (!found) {
38-
await msg.reply(`
39-
\`Failed to load\`
37+
if (!found)
38+
return await msg.reply(`
39+
\`Failed to load\`
4040
41-
${query}
41+
${query}
4242
`);
43-
} else {
44-
await msg.reply(`
45-
\`Successfully reloaded\`
4643

47-
${query}
48-
`);
49-
}
44+
return await msg.reply(`
45+
\`Successfully reloaded\`
5046
51-
return;
47+
${query}
48+
`);
5249
}
5350

5451
// Reload all commands

src/commands/stalk.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Message } from "../../types/message";
2-
import { getQuizCount } from "../components/services/quiz";
32
import { getUserbyLid, isBlocked } from "../components/services/user";
3+
import redis from "../components/redis";
44

55
export const info = {
66
command: "stalk",
@@ -18,12 +18,15 @@ export default async function (msg: Message) {
1818
}
1919

2020
const lid = msg.mentionedIds[0].split("@")[0];
21-
const [user, isBlockUser] = await Promise.all([
21+
const [user, isBlockPermanently, isBlockedTemporarily] = await Promise.all([
2222
getUserbyLid(lid),
2323
isBlocked(lid),
24+
redis.get(`rate:${lid}`),
2425
]);
2526
if (!user) return await msg.reply("User not found.");
2627

28+
console.log(JSON.stringify(isBlockedTemporarily))
29+
2730
const text = `
2831
\`${user.name}\`
2932
${user.about || "No about information available."}
@@ -35,7 +38,7 @@ export default async function (msg: Message) {
3538
Mode: ${user.mode}
3639
Command Count: ${user.commandCount}
3740
Last Seen: ${new Date(user.updatedAt).toLocaleString()}
38-
Blocked: ${isBlockUser ? "Yes" : "No"}
41+
Blocked: ${isBlockPermanently ? "Permanently Blocked" : (isBlockedTemporarily ? JSON.parse(isBlockedTemporarily).penaltyCount : "No")}
3942
`;
4043
await msg.reply(text);
4144
}

src/commands/unblock.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { Message } from "../../types/message";
2-
import log from "../components/utils/log";
3-
import { exec } from "child_process";
4-
import util from "util";
52
import { prisma } from "../components/prisma";
6-
import { getKey } from "../components/utils/rateLimiter";
73
import redis from "../components/redis";
84

95
export const info = {
@@ -29,5 +25,14 @@ export default async function (msg: Message) {
2925
where: { lid: { in: lids } },
3026
});
3127

28+
await Promise.all(
29+
msg.mentionedIds.map((lid) =>
30+
redis.set(
31+
`rate:${lid}`,
32+
JSON.stringify({ timestamps: [], penaltyCount: 0, penaltyUntil: 0 }),
33+
),
34+
),
35+
);
36+
3237
await msg.react("✅");
3338
}

src/commands/update.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { Message } from "../../types/message";
22
import log from "../components/utils/log";
33
import { exec } from "child_process";
44
import util from "util";
5-
import { prisma } from "../components/prisma";
6-
import { getKey } from "../components/utils/rateLimiter";
75
import redis from "../components/redis";
86

97
export const info = {

src/components/events/message.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default async function (msg: Message, type: string) {
5151
.trim();
5252

5353
const prefix = !msg.body.startsWith(commandPrefix);
54-
const senderId = msg.from.split("@")[0];
54+
const lid = msg.author ? msg.author.split("@")[0] : msg.from.split("@")[0];
5555

5656
/*
5757
* Prefix
@@ -73,21 +73,17 @@ export default async function (msg: Message, type: string) {
7373
/*
7474
* Block users from running commands.
7575
*/
76-
const isBlockedUser = await isBlocked(
77-
msg.author ? msg.author.split("@")[0] : senderId,
78-
);
79-
if (isBlockedUser) {
80-
return;
81-
}
76+
const isBlockedUser = await isBlocked(lid);
77+
if (isBlockedUser) return;
8278

8379
/*
8480
* Rate limit commands to prevent abuse.
8581
*/
8682
if (!msg.fromMe) {
87-
const rate = await rateLimiter(msg);
83+
const rate = await rateLimiter(lid);
8884
if (rate) return;
8985
if (rate === null) {
90-
msg.reply("Please wait a minute or so.");
86+
await msg.reply("Please wait a minute or so.");
9187
return;
9288
}
9389

@@ -118,7 +114,7 @@ export default async function (msg: Message, type: string) {
118114
return;
119115
}
120116

121-
log.info("Message", senderId, msg.body.slice(0, 150));
117+
log.info("Message", lid, msg.body.slice(0, 150));
122118
msg.body = !bodyHasPrefix ? msg.body : msg.body.slice(commandPrefix.length);
123119

124120
const originalReply = msg.reply.bind(msg);
@@ -129,9 +125,8 @@ export default async function (msg: Message, type: string) {
129125
): Promise<Message> => {
130126
let messageBody = typeof content === "string" ? Font(content) : content;
131127

132-
if (Math.random() < 0.5) {
128+
if (Math.random() < 0.5)
133129
return await client.sendMessage(msg.id.remote, messageBody, options);
134-
}
135130
return await originalReply(messageBody, chatId, options);
136131
};
137132

src/components/utils/rateLimiter.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Message } from "whatsapp-web.js";
21
import redis from "../redis";
32
import log from "./log";
43
import { prisma } from "../prisma";
@@ -7,16 +6,11 @@ const LIMIT = 5;
76
const BASE_WINDOW_MS = 30 * 1000;
87
const PENALTY_INCREMENT_MS = 10 * 1000;
98

10-
export function getKey(number: string) {
11-
return `rate:${number}`;
12-
}
13-
149
export default async function rateLimiter(
15-
msg: Message,
10+
lid: string,
1611
): Promise<boolean | null> {
17-
const number = msg.from;
1812
const now = Date.now();
19-
const key = getKey(number);
13+
const key = `rate:${lid}`;
2014

2115
const entryRaw = await redis.get(key);
2216
let entry = entryRaw
@@ -47,14 +41,14 @@ export default async function rateLimiter(
4741

4842
log.warn(
4943
"RateLimiter",
50-
`User ${number} blocked until ${new Date(entry.penaltyUntil).toLocaleTimeString()}`,
44+
`User ${lid} blocked until ${new Date(entry.penaltyUntil)}`,
5145
);
5246

5347
await Promise.all([
5448
redis.set(key, JSON.stringify(entry)),
5549
prisma.user.update({
5650
where: {
57-
lid: msg.author ? msg.author.split("@")[0] : msg.from.split("@")[0],
51+
lid,
5852
},
5953
data: {
6054
quizAnswered: {

0 commit comments

Comments
 (0)