From d40b55841201b7546d49f9125fd54d181d67a43f Mon Sep 17 00:00:00 2001 From: Deniz Date: Mon, 25 Nov 2019 15:21:08 +0100 Subject: [PATCH 1/5] Update antimalware.py to be more consistent with other information messages (like the codeblock reminder) & improve code a slight bit --- bot/cogs/antimalware.py | 44 +++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py index ababd6f18e..e0c127d9a0 100644 --- a/bot/cogs/antimalware.py +++ b/bot/cogs/antimalware.py @@ -1,11 +1,12 @@ import logging -from discord import Message, NotFound +from discord import Message, Embed from discord.ext.commands import Bot, Cog from bot.constants import AntiMalware as AntiMalwareConfig, Channels log = logging.getLogger(__name__) +PASTE_URL = "https://paste.pythondiscord.com/" class AntiMalware(Cog): @@ -17,37 +18,32 @@ def __init__(self, bot: Bot): @Cog.listener() async def on_message(self, message: Message) -> None: """Identify messages with prohibited attachments.""" - rejected_attachments = False - detected_pyfile = False + if len(message.attachments) == 0: + return + + embed = Embed() for attachment in message.attachments: if attachment.filename.lower().endswith('.py'): - detected_pyfile = True + embed.description = ( + "It looks like you tried to attach a Python file - please " + f"use a code-pasting service such as [{PASTE_URL}]" + f"({PASTE_URL}) instead." + ) break # Other detections irrelevant because we prioritize the .py message. if not attachment.filename.lower().endswith(tuple(AntiMalwareConfig.whitelist)): - rejected_attachments = True - - if detected_pyfile or rejected_attachments: - # Send a message to the user indicating the problem (with special treatment for .py) - author = message.author - if detected_pyfile: - msg = ( - f"{author.mention}, it looks like you tried to attach a Python file - please " - f"use a code-pasting service such as https://paste.pythondiscord.com/ instead." - ) - else: meta_channel = self.bot.get_channel(Channels.meta) - msg = ( - f"{author.mention}, it looks like you tried to attach a file type we don't " - f"allow. Feel free to ask in {meta_channel.mention} if you think this is a mistake." + embed.description = ( + "It looks like you tried to attach a file type that we " + "do not allow. We currently allow the following file " + f"types: **{', '.join(AntiMalwareConfig.whitelist)}**. \n\n" + f"Feel free to ask in {meta_channel.mention} if you think " + "this is a mistake." ) - - await message.channel.send(msg) + if embed.description: + await message.channel.send(message.author.mention, embed=embed) # Delete the offending message: - try: - await message.delete() - except NotFound: - log.info(f"Tried to delete message `{message.id}`, but message could not be found.") + await message.delete() def setup(bot: Bot) -> None: From 98f4aee9b5b49628b86d0b9e1c952abb9389a839 Mon Sep 17 00:00:00 2001 From: Deniz Date: Mon, 25 Nov 2019 15:24:22 +0100 Subject: [PATCH 2/5] Forgot the word 'hey' --- bot/cogs/antimalware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py index e0c127d9a0..4e8a3269b6 100644 --- a/bot/cogs/antimalware.py +++ b/bot/cogs/antimalware.py @@ -40,7 +40,7 @@ async def on_message(self, message: Message) -> None: "this is a mistake." ) if embed.description: - await message.channel.send(message.author.mention, embed=embed) + await message.channel.send(f"Hey {message.author.mention}!", embed=embed) # Delete the offending message: await message.delete() From bc96cf43825dcdfea5f819e2bf52de46371b0b58 Mon Sep 17 00:00:00 2001 From: Deniz Date: Mon, 25 Nov 2019 15:56:57 +0100 Subject: [PATCH 3/5] Change order of imports --- bot/cogs/antimalware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py index 4e8a3269b6..24ce501c32 100644 --- a/bot/cogs/antimalware.py +++ b/bot/cogs/antimalware.py @@ -1,6 +1,6 @@ import logging -from discord import Message, Embed +from discord import Embed, Message from discord.ext.commands import Bot, Cog from bot.constants import AntiMalware as AntiMalwareConfig, Channels From 0a417f4828e229516d552d21ac678a8dc150beed Mon Sep 17 00:00:00 2001 From: Deniz Date: Mon, 25 Nov 2019 19:50:20 +0100 Subject: [PATCH 4/5] Update PASTE_URL constant to be pydis instead of pythondiscord --- bot/cogs/antimalware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py index 24ce501c32..72db5bc111 100644 --- a/bot/cogs/antimalware.py +++ b/bot/cogs/antimalware.py @@ -6,7 +6,7 @@ from bot.constants import AntiMalware as AntiMalwareConfig, Channels log = logging.getLogger(__name__) -PASTE_URL = "https://paste.pythondiscord.com/" +PASTE_URL = "https://paste.pydis.com/" class AntiMalware(Cog): From c73c9bae2767da1a6dff5b4098d4af50a61aabe5 Mon Sep 17 00:00:00 2001 From: Deniz Date: Mon, 25 Nov 2019 20:17:08 +0100 Subject: [PATCH 5/5] Make requested tweaks: Use URL constant from constants.py, re-add try/except block and implement the changes requested by Ava --- bot/cogs/antimalware.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py index 72db5bc111..745dd8082b 100644 --- a/bot/cogs/antimalware.py +++ b/bot/cogs/antimalware.py @@ -1,12 +1,11 @@ import logging -from discord import Embed, Message +from discord import Embed, Message, NotFound from discord.ext.commands import Bot, Cog -from bot.constants import AntiMalware as AntiMalwareConfig, Channels +from bot.constants import AntiMalware as AntiMalwareConfig, Channels, URLs log = logging.getLogger(__name__) -PASTE_URL = "https://paste.pydis.com/" class AntiMalware(Cog): @@ -18,32 +17,35 @@ def __init__(self, bot: Bot): @Cog.listener() async def on_message(self, message: Message) -> None: """Identify messages with prohibited attachments.""" - if len(message.attachments) == 0: + if not message.attachments: return embed = Embed() for attachment in message.attachments: - if attachment.filename.lower().endswith('.py'): + filename = attachment.filename.lower() + if filename.endswith('.py'): embed.description = ( - "It looks like you tried to attach a Python file - please " - f"use a code-pasting service such as [{PASTE_URL}]" - f"({PASTE_URL}) instead." + f"It looks like you tried to attach a Python file - please " + f"use a code-pasting service such as {URLs.paste_service}" ) break # Other detections irrelevant because we prioritize the .py message. - if not attachment.filename.lower().endswith(tuple(AntiMalwareConfig.whitelist)): + if not filename.endswith(tuple(AntiMalwareConfig.whitelist)): + whitelisted_types = ', '.join(AntiMalwareConfig.whitelist) meta_channel = self.bot.get_channel(Channels.meta) embed.description = ( - "It looks like you tried to attach a file type that we " - "do not allow. We currently allow the following file " - f"types: **{', '.join(AntiMalwareConfig.whitelist)}**. \n\n" - f"Feel free to ask in {meta_channel.mention} if you think " - "this is a mistake." + f"It looks like you tried to attach a file type that we " + f"do not allow. We currently allow the following file " + f"types: **{whitelisted_types}**. \n\n Feel free to ask " + f"in {meta_channel.mention} if you think this is a mistake." ) if embed.description: await message.channel.send(f"Hey {message.author.mention}!", embed=embed) # Delete the offending message: - await message.delete() + try: + await message.delete() + except NotFound: + log.info(f"Tried to delete message `{message.id}`, but message could not be found.") def setup(bot: Bot) -> None: