refactor(bot): streamline bot middleware and improve key generator function
- Removed unused session middleware and sequentialize function from the bot's error boundary. - Simplified the key generator function for rate limiting by condensing its implementation. - Enhanced overall code clarity and maintainability in the bot's configuration.
This commit is contained in:
parent
f9e50972cd
commit
bad65204c4
@ -4,18 +4,15 @@ import { unhandledFeature } from './features/unhandled';
|
||||
import { errorHandler } from './handlers/errors';
|
||||
import { i18n } from './i18n';
|
||||
import * as middlewares from './middlewares';
|
||||
import { session } from './middlewares';
|
||||
import { setCommands } from './settings/commands';
|
||||
import { setInfo } from './settings/info';
|
||||
import { env } from '@/config/env';
|
||||
import { logger } from '@/utils/logger';
|
||||
import { getRedisInstance } from '@/utils/redis';
|
||||
import { getSessionKey } from '@/utils/session';
|
||||
import { autoChatAction } from '@grammyjs/auto-chat-action';
|
||||
import { conversations } from '@grammyjs/conversations';
|
||||
import { hydrate } from '@grammyjs/hydrate';
|
||||
import { limit } from '@grammyjs/ratelimiter';
|
||||
import { sequentialize } from '@grammyjs/runner';
|
||||
import { Bot } from 'grammy';
|
||||
|
||||
type Parameters_ = {
|
||||
@ -31,9 +28,7 @@ export function createBot({ token }: Parameters_) {
|
||||
|
||||
bot.use(
|
||||
limit({
|
||||
keyGenerator: (ctx) => {
|
||||
return ctx.from?.id.toString();
|
||||
},
|
||||
keyGenerator: (ctx) => ctx.from?.id.toString(),
|
||||
limit: env.RATE_LIMIT,
|
||||
onLimitExceeded: async (ctx) => {
|
||||
await ctx.reply(ctx.t('err-limit-exceeded'));
|
||||
@ -47,7 +42,6 @@ export function createBot({ token }: Parameters_) {
|
||||
context.logger = logger.child({
|
||||
update_id: context.update.update_id,
|
||||
});
|
||||
|
||||
await next();
|
||||
});
|
||||
|
||||
@ -58,9 +52,6 @@ export function createBot({ token }: Parameters_) {
|
||||
|
||||
const protectedBot = bot.errorBoundary(errorHandler);
|
||||
|
||||
protectedBot.use(sequentialize(getSessionKey));
|
||||
protectedBot.use(session());
|
||||
|
||||
protectedBot.use(middlewares.updateLogger());
|
||||
protectedBot.use(autoChatAction(bot.api));
|
||||
protectedBot.use(hydrate());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user