From 7d538316ade3e11dd160a4f4b9096dc1cac36526 Mon Sep 17 00:00:00 2001 From: AlberLC Date: Mon, 17 Mar 2025 08:31:16 +0100 Subject: [PATCH] Add PenaltyBot._check_message_spam --- flanabot/bots/penalty_bot.py | 43 +++++++++++++++++++++++++++++++++++- flanabot/constants.py | 1 + 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/flanabot/bots/penalty_bot.py b/flanabot/bots/penalty_bot.py index 2452399..3cdce46 100644 --- a/flanabot/bots/penalty_bot.py +++ b/flanabot/bots/penalty_bot.py @@ -76,6 +76,46 @@ class PenaltyBot(MultiBot, ABC): await self.punish(message.author.id, message.chat.group_id, punishment_seconds, message, flood=True) await self.send(f'Castigado durante {TimeUnits(seconds=punishment_seconds).to_words()}.', message) + @admin(False) + @group + async def _check_message_spam(self, message: Message) -> bool: + if await self.is_punished(message.author, message.chat): + return True + + spam_messages = self.Message.find({ + 'text': message.text, + 'platform': self.platform.value, + 'author': message.author.object_id, + 'date': {'$gte': datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(hours=1)} + }) + + chats = {message.chat for message in spam_messages} + if len(chats) > constants.SPAM_CHANNELS_LIMIT: + for message in spam_messages: + await self.delete_message(await self.get_message(message.id, message.chat.id)) + await self.punish(message.author.id, message.chat.group_id) + owner_message_parts = [ + 'Castigado spammer:', + 'User:', + f' id: {message.author.id}', + f' name: {message.author.name}', + f' is_admin: {message.author.is_admin}', + f' is_bot: {message.author.is_bot}', + '', + 'Chats:', + '\n\n'.join( + f' id: {chat.id}\n' + f' name: {chat.name}\n' + f' group_id: {chat.group_id}\n' + f' group_name: {chat.group_name}' + for chat in chats + ) + ] + await self.send('\n'.join(owner_message_parts), await self.owner_chat) + return True + + return False + async def _punish(self, user: int | str | User, group_: int | str | Chat | Message, message: Message = None): pass @@ -109,7 +149,8 @@ class PenaltyBot(MultiBot, ABC): await super()._on_new_message_raw(message, whitelist_callbacks, blacklist_callbacks) if message.chat.config['check_flood'] and message.chat.config['punish'] and not message.is_inline: async with self.lock: - await self._check_message_flood(message) + if not await self._check_message_spam(message): + await self._check_message_flood(message) @bot_mentioned @group diff --git a/flanabot/constants.py b/flanabot/constants.py index f197293..75c219b 100644 --- a/flanabot/constants.py +++ b/flanabot/constants.py @@ -23,6 +23,7 @@ PUNISHMENT_INCREMENT_EXPONENT = 6 PUNISHMENTS_RESET_TIME = datetime.timedelta(weeks=2) RECOVERY_DELETED_MESSAGE_BEFORE = datetime.timedelta(hours=1) SCRAPING_TIMEOUT_SECONDS = 10 +SPAM_CHANNELS_LIMIT = 2 STEAM_ALL_APPS_ENDPOINT = 'https://api.steampowered.com/ISteamApps/GetAppList/v2' STEAM_APP_ENDPOINT_TEMPLATE = 'https://store.steampowered.com/api/appdetails?appids={ids}&cc={country_code}&filters=price_overview' STEAM_APP_IDS_FOR_SCRAPE_COUNTRIES = (400, 620, 730, 210970, 252490, 292030, 427520, 1712350)