diff --git a/flanabot/bots/penalty_bot.py b/flanabot/bots/penalty_bot.py index cc81d85..ee0b08b 100644 --- a/flanabot/bots/penalty_bot.py +++ b/flanabot/bots/penalty_bot.py @@ -86,36 +86,54 @@ class PenaltyBot(MultiBot, ABC): 'text': message.text, 'platform': self.platform.value, 'author': message.author.object_id, - 'date': {'$gte': datetime.datetime.now(datetime.timezone.utc) - constants.SPAM_TIME_RANGE} + 'date': {'$gte': datetime.datetime.now(datetime.timezone.utc) - constants.SPAM_TIME_RANGE}, }) 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) - groups_data = {chat.group_id: chat.group_name for chat in chats} - owner_message_parts = [ - 'Spammer castigado:', - '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}', - '', - f'Chats: {len(chats)}', - '', - 'Groups:', - '\n\n'.join( - f' group_id: {group_id}\n' - f' group_name: {group_name}' - for group_id, group_name in groups_data.items() - ) - ] - await self.send('\n'.join(owner_message_parts), await self.owner_chat) - return True + if len(chats) <= constants.SPAM_CHANNELS_LIMIT: + return False - return False + await self.punish(message.author.id, message.chat.group_id) + await asyncio.sleep(constants.SPAM_DELETION_DELAY.total_seconds()) # We make sure to also delete any messages they may have sent before the punishment + 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) + - + constants.SPAM_TIME_RANGE + - + constants.SPAM_DELETION_DELAY + }, + 'is_deleted': False + }) + chats = {message.chat for message in spam_messages} + + for message in spam_messages: + await self.delete_message(await self.get_message(message.id, message.chat.id)) + + groups_data = {chat.group_id: chat.group_name for chat in chats} + owner_message_parts = [ + 'Spammer castigado:', + '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}', + '', + f'Chats: {len(chats)}', + '', + 'Groups:', + '\n\n'.join( + f' group_id: {group_id}\n' + f' group_name: {group_name}' + for group_id, group_name in groups_data.items() + ) + ] + await self.send('\n'.join(owner_message_parts), await self.owner_chat) + + return True async def _punish(self, user: int | str | User, group_: int | str | Chat | Message, message: Message = None): pass diff --git a/flanabot/constants.py b/flanabot/constants.py index 63cf891..b9f57ca 100644 --- a/flanabot/constants.py +++ b/flanabot/constants.py @@ -24,6 +24,7 @@ PUNISHMENTS_RESET_TIME = datetime.timedelta(weeks=2) RECOVERY_DELETED_MESSAGE_BEFORE = datetime.timedelta(hours=1) SCRAPING_TIMEOUT_SECONDS = 10 SPAM_CHANNELS_LIMIT = 2 +SPAM_DELETION_DELAY = datetime.timedelta(seconds=5) SPAM_TIME_RANGE = datetime.timedelta(hours=1) 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'