diff options
Diffstat (limited to '')
| -rw-r--r-- | bot/cogs/antimalware.py | 44 | 
1 files 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: | 
