diff options
-rw-r--r-- | bot/cogs/antimalware.py | 43 | ||||
-rw-r--r-- | bot/constants.py | 6 | ||||
-rw-r--r-- | config-default.yml | 6 |
3 files changed, 55 insertions, 0 deletions
diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py new file mode 100644 index 000000000..e4688295e --- /dev/null +++ b/bot/cogs/antimalware.py @@ -0,0 +1,43 @@ +import logging + +from discord import Message, utils +from discord.ext.commands import Bot, Cog + +from bot.constants import AntiMalware as AntiMalwareConfig, Channels + +log = logging.getLogger(__name__) + + +class AntiMalware(Cog): + """Cog providing anti-malware behavior.""" + def __init__(self, bot: Bot): + self.bot = bot + self.whitelist = tuple(AntiMalwareConfig.whitelist) + + @Cog.listener() + async def on_message(self, message: Message) -> None: + """Identify messages with prohibited attachments.""" + log.trace("Entered AntiMalware.on_message()") + rejected_attachments = [a for a in message.attachments if + not a.filename.lower().endswith(self.whitelist)] + detected_pyfile = len([a for a in message.attachments if a.filename.lower().endswith('.py')]) > 0 + + if len(rejected_attachments) > 0: + log.trace("Identified rejected attachment(s)") + # Send a message indicating the problem to the user (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 = utils.get(message.guild.channels, id=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." + + await message.channel.send(msg) + + +def setup(bot: Bot) -> None: + """AntiMalware cog load.""" + bot.add_cog(AntiMalware(bot)) + log.info("Cog loaded: AntiMalware") diff --git a/bot/constants.py b/bot/constants.py index 1deeaa3b8..81f316d57 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -460,6 +460,12 @@ class AntiSpam(metaclass=YAMLGetter): rules: Dict[str, Dict[str, int]] +class AntiMalware(metaclass=YAMLGetter): + section = "anti_malware" + + whitelist: tuple + + class BigBrother(metaclass=YAMLGetter): section = 'big_brother' diff --git a/config-default.yml b/config-default.yml index 0dac9bf9f..30d505d6d 100644 --- a/config-default.yml +++ b/config-default.yml @@ -107,6 +107,7 @@ guild: help_7: 587375768556797982 helpers: 385474242440986624 message_log: &MESSAGE_LOG 467752170159079424 + meta: 429409067623251969 mod_alerts: 473092532147060736 modlog: &MODLOG 282638479504965634 off_topic_0: 291284109232308226 @@ -322,6 +323,11 @@ anti_spam: max: 3 +anti_malware: + whitelist: ['.bmp', '.gif', '.jpg', '.jpeg', '.png', '.tiff', # Images + '.3gp', '.3g2', '.avi', '.h264', '.m4v', '.mkv', '.mov', '.mp4', '.mpeg', '.mpg', '.wmv' ] # Videos + + reddit: request_delay: 60 subreddits: |