Skip to content

Commit

Permalink
Merge pull request #244 from MoC-OSS/feature/UABOT-88_fix_command_scopes
Browse files Browse the repository at this point in the history
[Fix] [UABOT-88] Adjust command and other scopes to not work in channels
  • Loading branch information
DrSmile444 authored Dec 5, 2022
2 parents 0cc5ddb + 139ac45 commit 58fc328
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
30 changes: 19 additions & 11 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -163,6 +164,11 @@ const rootMenu = new Menu<GrammyMenuContext>('root');

rootMenu.register(tensorListener.initMenu(trainingThrottler));

// Not channel handlers
const notChannelRegisterComposer = new Composer<GrammyContext>();

const notChannelComposer = notChannelRegisterComposer.filter((context) => isNotChannel(context));

bot.use(
sequentialize((context: GrammyContext) => {
const chat = context.chat?.id.toString();
Expand Down Expand Up @@ -195,27 +201,29 @@ const rootMenu = new Menu<GrammyMenuContext>('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);

Expand Down
4 changes: 1 addition & 3 deletions src/bot/composers/messages.composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 58fc328

Please sign in to comment.