From bcf3b2e8a09794832aa915dda2695a7fa49edbf8 Mon Sep 17 00:00:00 2001 From: Dmytro Vakulenko Date: Fri, 2 Dec 2022 15:15:25 +0200 Subject: [PATCH] feat(UABOT-88): hide join and leave messages --- src/bot.ts | 7 ++++++ src/bot/commands/public/settings.command.ts | 2 ++ src/bot/composers/index.ts | 1 + src/bot/composers/join-leave.composer.ts | 26 +++++++++++++++++++++ src/message.ts | 9 ++++++- src/types/session.ts | 1 + 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/bot/composers/join-leave.composer.ts diff --git a/src/bot.ts b/src/bot.ts index 5db86e67..388a68f2 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -13,6 +13,7 @@ import { getBeforeAnyComposer, getCreatorCommandsComposer, getHealthCheckComposer, + getJoinLeaveComposer, getMessagesComposer, getPrivateCommandsComposer, getPublicCommandsComposer, @@ -134,6 +135,9 @@ const rootMenu = new Menu('root'); updateMethod: swindlersGoogleService.appendTrainingPositives.bind(swindlersGoogleService), }); + // Join and leave composer + const { joinLeaveComposer } = getJoinLeaveComposer(); + // Tensor testing old logic const { tensorTrainingComposer } = getTensorTrainingComposer({ tensorListener, @@ -204,6 +208,9 @@ const rootMenu = new Menu('root'); bot.use(swindlerBotsSaveToSheetComposer); bot.use(swindlerHelpSaveToSheetComposer); + // Join and leave composer + bot.use(joinLeaveComposer); + // Tensor testing old logic bot.use(tensorTrainingComposer); diff --git a/src/bot/commands/public/settings.command.ts b/src/bot/commands/public/settings.command.ts index cb98dfbf..22b66fe3 100644 --- a/src/bot/commands/public/settings.command.ts +++ b/src/bot/commands/public/settings.command.ts @@ -6,6 +6,7 @@ import { deleteForwardedButton, deleteMentionButton, deleteMessageButton, + deleteServiceMessageButton, deleteSwindlerButton, deleteTensorButton, deleteUrlsButton, @@ -90,6 +91,7 @@ export class SettingsCommand { .row() .text(deleteMentionButton, (context) => toggleSetting(context, 'enableDeleteMentions')) .text(deleteForwardedButton, (context) => toggleSetting(context, 'enableDeleteForwards')) + .text(deleteServiceMessageButton, (context) => toggleSetting(context, 'disableDeleteServiceMessage')) .row() .text(airAlarmAlertButton, isAlarmNow, async (context) => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment diff --git a/src/bot/composers/index.ts b/src/bot/composers/index.ts index e461a340..e9ed5fda 100644 --- a/src/bot/composers/index.ts +++ b/src/bot/composers/index.ts @@ -1,6 +1,7 @@ export * from './before-any.composer'; export * from './creator-command.composer'; export * from './health-check.composer'; +export * from './join-leave.composer'; export * from './messages.composer'; export * from './private-command.composer'; export * from './public-command.composer'; diff --git a/src/bot/composers/join-leave.composer.ts b/src/bot/composers/join-leave.composer.ts new file mode 100644 index 00000000..4e441bcc --- /dev/null +++ b/src/bot/composers/join-leave.composer.ts @@ -0,0 +1,26 @@ +import { Composer } from 'grammy'; + +import type { GrammyContext } from '../../types'; +import { onlyNotDeletedFilter } from '../filters'; + +/** + * @description Remove join and leave messages from chat + * */ +export const getJoinLeaveComposer = () => { + const joinLeaveComposer = new Composer(); + + joinLeaveComposer + // Filter that feature is enabled + .filter((context) => !context.chatSession.chatSettings.disableDeleteServiceMessage) + // Filter if the message is already deleted + .filter((context) => onlyNotDeletedFilter(context)) + // Queries to filter + .on([':new_chat_members', ':left_chat_member']) + // Delete message + .use(async (context, next) => { + await context.deleteMessage(); + return next(); + }); + + return { joinLeaveComposer }; +}; diff --git a/src/message.ts b/src/message.ts index 21dfd51c..df3d5fc0 100644 --- a/src/message.ts +++ b/src/message.ts @@ -146,6 +146,11 @@ export const getSettingsMenuMessage = (settings: ChatSessionData['chatSettings'] ⚓ ${settings.enableDeleteMentions ? '✅ Бот видаляє повідомлення зі @ згадуваннями.' : '⛔ Бот не видаляє повідомлення зі @ згадуваннями.'} ↩️ ${settings.enableDeleteForwards ? '✅ Бот видаляє повідомлення з пересиланнями.' : '⛔ Бот не видаляє повідомлення з пересиланнями.'} +✋ ${ + settings.disableDeleteServiceMessage + ? '⛔ Бот не видаляє повідомлення приєдання та прощання.' + : '✅ Бот видаляє повідомлення приєдання та прощання.' + } Налаштування повітряної тривоги. 🏰 ${ @@ -231,6 +236,7 @@ export const deleteUrlsButton = '🔗 Посилання'; export const deleteMentionButton = '⚓ Згадування'; export const deleteForwardedButton = '↩️ Пересилання'; +export const deleteServiceMessageButton = '✋ Приєднання'; export const airAlarmAlertButton = '🏰 Регіон'; export const airAlarmNotificationMessage = '📢 Тривога'; @@ -244,10 +250,11 @@ export const englishDeleteMessageButton = '❗ Reason'; export const englishDeleteSwindlerButton = '💰 Scam'; export const englishDeleteCardsButton = '💳 Cards'; - export const englishDeleteUrlsButton = '🔗 Link'; + export const englishDeleteMentionButton = '⚓ Mention'; export const englishDeleteForwardedButton = '↩️ Forward'; +export const englishDeleteServiceMessageButton = '✋ Join'; export const englishAirAlarmAlertButton = '🏰 Region'; export const englishAirAlarmNotificationMessage = '📢 Alarm'; diff --git a/src/types/session.ts b/src/types/session.ts index 834615c6..32273d47 100644 --- a/src/types/session.ts +++ b/src/types/session.ts @@ -42,6 +42,7 @@ export interface DefaultChatSettings { disableStrategicInfo?: boolean; disableDeleteMessage?: boolean; disableSwindlerMessage?: boolean; + disableDeleteServiceMessage?: boolean; } export interface OptionalChatSettings {