From 139ac452410170d048a640ea6b0ee5917b320b58 Mon Sep 17 00:00:00 2001 From: Dmytro Vakulenko Date: Mon, 5 Dec 2022 14:05:45 +0200 Subject: [PATCH] fix(UABOT-88): adjust command and other scopes to not work in channels --- src/bot.ts | 30 ++++++++++++++++---------- src/bot/composers/messages.composer.ts | 4 +--- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 388a68f2..79c138e7 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -3,7 +3,7 @@ import { Menu } from '@grammyjs/menu'; import { hydrateReply } from '@grammyjs/parse-mode'; import { run, sequentialize } from '@grammyjs/runner'; import { apiThrottler } from '@grammyjs/transformer-throttler'; -import { Bot } from 'grammy'; +import { Bot, Composer } from 'grammy'; import Keyv from 'keyv'; import moment from 'moment-timezone'; import ms from 'ms'; @@ -28,6 +28,7 @@ import { getSwindlersComposer, } from './bot/composers/messages'; import { getNoForwardsComposer } from './bot/composers/messages/no-forward.composer'; +import { isNotChannel } from './bot/filters'; import { OnTextListener, TestTensorListener } from './bot/listeners'; import { MessageHandler } from './bot/message.handler'; import { DeleteSwindlersMiddleware, GlobalMiddleware, stateMiddleware } from './bot/middleware'; @@ -163,6 +164,11 @@ const rootMenu = new Menu('root'); rootMenu.register(tensorListener.initMenu(trainingThrottler)); + // Not channel handlers + const notChannelRegisterComposer = new Composer(); + + const notChannelComposer = notChannelRegisterComposer.filter((context) => isNotChannel(context)); + bot.use( sequentialize((context: GrammyContext) => { const chat = context.chat?.id.toString(); @@ -195,27 +201,29 @@ const rootMenu = new Menu('root'); bot.use(wrapperErrorHandler(globalMiddleware.middleware())); // Generic composers - bot.use(healthCheckComposer); bot.use(beforeAnyComposer); // Commands - bot.use(creatorCommandsComposer); - bot.use(privateCommandsComposer); - bot.use(publicCommandsComposer); + notChannelComposer.use(healthCheckComposer); + notChannelComposer.use(creatorCommandsComposer); + notChannelComposer.use(privateCommandsComposer); + notChannelComposer.use(publicCommandsComposer); // Swindlers helpers - bot.use(swindlerMessageSaveToSheetComposer); - bot.use(swindlerBotsSaveToSheetComposer); - bot.use(swindlerHelpSaveToSheetComposer); + notChannelComposer.use(swindlerMessageSaveToSheetComposer); + notChannelComposer.use(swindlerBotsSaveToSheetComposer); + notChannelComposer.use(swindlerHelpSaveToSheetComposer); // Join and leave composer - bot.use(joinLeaveComposer); + notChannelComposer.use(joinLeaveComposer); // Tensor testing old logic - bot.use(tensorTrainingComposer); + notChannelComposer.use(tensorTrainingComposer); // Main message composer - bot.use(messagesComposer); + notChannelComposer.use(messagesComposer); + + bot.use(notChannelRegisterComposer); bot.catch(globalErrorHandler); diff --git a/src/bot/composers/messages.composer.ts b/src/bot/composers/messages.composer.ts index 55cfd5cc..2a9cae80 100644 --- a/src/bot/composers/messages.composer.ts +++ b/src/bot/composers/messages.composer.ts @@ -2,7 +2,7 @@ import { Composer } from 'grammy'; import { messageQuery } from '../../const'; import type { DefaultChatSettings, GrammyContext, GrammyMiddleware, OptionalChatSettings } from '../../types'; -import { isNotChannel, onlyActiveDefaultSettingFilter, onlyActiveOptionalSettingFilter, onlyNotDeletedFilter } from '../filters'; +import { onlyActiveDefaultSettingFilter, onlyActiveOptionalSettingFilter, onlyNotDeletedFilter } from '../filters'; import { botActiveMiddleware, botRedisActive, @@ -47,8 +47,6 @@ export const getMessagesComposer = ({ const readyMessagesComposer = messagesComposer // Queries to follow .on(messageQuery) - // Filtering messages from channel - .filter((context) => isNotChannel(context)) // Filtering messages .use(botRedisActive, ignoreOld(60), botActiveMiddleware, onlyNotAdmin, onlyWhenBotAdmin) // Parse message text and add it to state