From d40b55841201b7546d49f9125fd54d181d67a43f Mon Sep 17 00:00:00 2001 From: Deniz Date: Mon, 25 Nov 2019 15:21:08 +0100 Subject: 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 ababd6f18..e0c127d9a 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 @@ class AntiMalware(Cog): @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: -- cgit v1.2.3