1- import { Bot , webhookCallback } from 'grammy' ;
1+ import { Bot , webhookCallback , BotError } from 'grammy' ;
22import type { Context } from 'grammy' ;
33import Config from '../config' ;
44import { GenerateCommand } from './handlers/GenerateCommands' ;
@@ -7,6 +7,7 @@ import { MessageValidator } from '../decorators/Context';
77import * as express from 'express' ;
88import { BotReply } from '../utils/chat/BotReply' ;
99import logger from '../utils/logger' ;
10+ import { limit } from '@grammyjs/ratelimiter' ;
1011export 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 ) {
0 commit comments