Skip to content

Commit bcdd98c

Browse files
committed
feat: Update dependencies and add rate limiting to bot`
diff` contains changes to `package-lock.json, package.json`, `src/bot/index.ts, src/decorators/Context.ts, and src/utils/RateLimiter.ts
1 parent 0bc84c1 commit bcdd98c

File tree

5 files changed

+21
-108
lines changed

5 files changed

+21
-108
lines changed

package-lock.json

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"homepage": "https://github.com/CodeModule-ir/cop#readme",
5050
"dependencies": {
51+
"@grammyjs/ratelimiter": "^1.2.1",
5152
"@medishn/logger": "^2.0.0",
5253
"express": "^4.21.1",
5354
"grammy": "^1.27.0",

src/bot/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Bot, webhookCallback } from 'grammy';
1+
import { Bot, webhookCallback, BotError } from 'grammy';
22
import type { Context } from 'grammy';
33
import Config from '../config';
44
import { GenerateCommand } from './handlers/GenerateCommands';
@@ -7,6 +7,7 @@ import { MessageValidator } from '../decorators/Context';
77
import * as express from 'express';
88
import { BotReply } from '../utils/chat/BotReply';
99
import logger from '../utils/logger';
10+
import { limit } from '@grammyjs/ratelimiter';
1011
export class CopBot {
1112
private static instance: CopBot;
1213
private _bot: Bot<Context>;
@@ -29,6 +30,8 @@ export class CopBot {
2930
async start(): Promise<void> {
3031
const startBot = async (mode: string) => {
3132
await this._bot.start({
33+
drop_pending_updates: true,
34+
timeout: 60,
3235
onStart: (botInfo) => {
3336
logger.info(`Bot started in ${mode} mode! Username: ${botInfo.username}`);
3437
},
@@ -93,7 +96,6 @@ export class CopBot {
9396
};
9497

9598
await trySetWebhook();
96-
await startBot(mode);
9799
});
98100
} catch (err: any) {
99101
console.error('Error setting up webhook:', err);
@@ -112,13 +114,19 @@ export class CopBot {
112114
new GenerateCommand(this._bot).generate();
113115
this._bot.on('my_chat_member', (ctx) => this.handleJoinNewChat(ctx));
114116
this._bot.on('message', (ctx) => this.handleMessage(ctx));
115-
this._bot.catch((err) => {
116-
console.error('Middleware error:', err);
117+
this._bot.catch((error: BotError<Context>) => {
118+
logger.error(`Bot error occurred: ${error.error}`);
117119
});
120+
this._bot.use(
121+
limit({
122+
onLimitExceeded: (ctx) => ctx.reply('Too many requests! Please slow down.'),
123+
})
124+
);
118125
await this.start();
119126
logger.info('Bot is running');
120127
}
121128
@MessageValidator()
129+
@SaveUserData()
122130
async handleMessage(ctx: Context) {}
123131
@SaveUserData()
124132
async handleJoinNewChat(ctx: Context) {

src/decorators/Context.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { BotReply } from '../utils/chat/BotReply';
22
import { MessagesService } from '../service/messages';
33
import { RateLimitConfig } from '../types/CommandTypes';
4-
import { RateLimiter } from '../utils/RateLimiter';
54
import { createDecorator } from './index';
65
/**
76
* A decorator to restrict commands to group chats.
@@ -73,20 +72,3 @@ export function RequireReply() {
7372
}
7473
});
7574
}
76-
export function RateLimit(config: RateLimitConfig = {}) {
77-
const limiter = new RateLimiter(config.commandLimit || 5, config.timeFrame || 10000); // Initialize the limiter
78-
79-
return createDecorator(async (ctx, next, close) => {
80-
const userId = ctx.from?.id;
81-
if (!userId) {
82-
close();
83-
return;
84-
}
85-
if (limiter.checkRateLimit(userId)) {
86-
return await next();
87-
} else {
88-
await ctx.reply('⏳ You are being rate-limited. Please try again later.');
89-
close();
90-
}
91-
});
92-
}

src/utils/RateLimiter.ts

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)