Skip to content

Commit 738d4b0

Browse files
committed
feat(controller): Adding Rate Limit and checking the user's userId, and if it doesn't exist, the program won't run.
1 parent d539fb0 commit 738d4b0

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

src/controller/command.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { SafeExecution } from "../decorators/SafeExecution";
44
import { COMMANDS } from "../helper";
55
import { AdminCommand } from "../group-management/AdminCommand";
66
import { Logger } from "../config/logger";
7-
const logger = new Logger({ file: "command.log", level: "debug" });
7+
import { RateLimiter } from "../helper/RateLimiter";
8+
const logger = new Logger({
9+
file: "command.log",
10+
level: "debug",
11+
timestampFormat: "locale",
12+
});
813

914
export class Command {
1015
@SafeExecution()
@@ -30,7 +35,13 @@ export class Command {
3035
},
3136
});
3237
}
33-
38+
@SafeExecution()
39+
static async shahin(ctx: Context) {
40+
await ctx.reply("دوستان.");
41+
setTimeout(() => {
42+
return ctx.reply("بحث تخصصی.");
43+
}, 2500);
44+
}
3445
@SafeExecution()
3546
static future(ctx: Context) {
3647
return ctx.reply("We will go to ga", {
@@ -42,8 +53,15 @@ export class Command {
4253

4354
@SafeExecution()
4455
static async handleCommand(ctx: Context): Promise<void> {
56+
const userId = ctx.from?.id;
57+
if (!userId) return;
58+
if (!RateLimiter.limit(userId)) {
59+
logger.debug(`Rate limit exceeded for user: ${userId}`, "RATE_LIMIT");
60+
return;
61+
}
4562
const command = ctx.message?.text?.split("/")[1]?.split(/[\s@]/)[0];
46-
logger.debug(command, "COMMAND");
63+
logger.info(`Received command: ${command}`, "COMMAND");
64+
4765
if (command && (Command as any)[command]) {
4866
await (Command as any)[command](ctx);
4967
} else {

0 commit comments

Comments
 (0)