diff options
| author | 2019-11-30 13:50:09 +0100 | |
|---|---|---|
| committer | 2019-11-30 13:50:09 +0100 | |
| commit | be1162243fd0286f9546afb22d52c7351462ae24 (patch) | |
| tree | e63ec9744f6f035f05736c5e669bef2eedc88dca | |
| parent | Change `on_message_edit` - only show differences, highlighted in… (#646) (diff) | |
| parent | Merge branch 'master' into master (diff) | |
Merge pull request #671 from Denayder/master
Update antimalware.py to be more consistent with other information messages & improve code a slight bit
| -rw-r--r-- | bot/cogs/antimalware.py | 44 | 
1 files changed, 21 insertions, 23 deletions
| diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py index ababd6f18..745dd8082 100644 --- a/bot/cogs/antimalware.py +++ b/bot/cogs/antimalware.py @@ -1,9 +1,9 @@  import logging -from discord import Message, NotFound +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__) @@ -17,31 +17,29 @@ 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 not message.attachments: +            return + +        embed = Embed()          for attachment in message.attachments: -            if attachment.filename.lower().endswith('.py'): -                detected_pyfile = True -                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." +            filename = attachment.filename.lower() +            if filename.endswith('.py'): +                embed.description = ( +                    f"It looks like you tried to attach a Python file - please " +                    f"use a code-pasting service such as {URLs.paste_service}"                  ) -            else: +                break  # Other detections irrelevant because we prioritize the .py message. +            if not filename.endswith(tuple(AntiMalwareConfig.whitelist)): +                whitelisted_types = ', '.join(AntiMalwareConfig.whitelist)                  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 = ( +                    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."                  ) - -            await message.channel.send(msg) +        if embed.description: +            await message.channel.send(f"Hey {message.author.mention}!", embed=embed)              # Delete the offending message:              try: | 
