aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar bendiller <[email protected]>2019-10-14 17:59:37 -0600
committerGravatar bendiller <[email protected]>2019-10-14 17:59:37 -0600
commit003f8fe85cbb90d51225580bc35b91ebf21fd0a3 (patch)
tree557161f23582ae770c054e3d6a91080c15db576d
parentFix Constants.AntiMalware.whitelist type (diff)
Improve code readability and docstring
-rw-r--r--bot/cogs/antimalware.py25
-rw-r--r--bot/constants.py1
-rw-r--r--config-default.yml20
3 files changed, 33 insertions, 13 deletions
diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py
index 94566c156..156239a63 100644
--- a/bot/cogs/antimalware.py
+++ b/bot/cogs/antimalware.py
@@ -9,29 +9,32 @@ log = logging.getLogger(__name__)
class AntiMalware(Cog):
- """Cog providing anti-malware behavior."""
+ """Delete messages which contain attachments with non-whitelisted file extensions."""
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."""
- 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:
+ rejected_attachments = list()
+ detected_pyfile = list()
+ for attachment in message.attachments:
+ if not attachment.filename.lower().endswith(tuple(AntiMalwareConfig.whitelist)):
+ rejected_attachments.append(attachment)
+ if attachment.filename.lower().endswith('.py'):
+ detected_pyfile.append(attachment)
+
+ if 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."
+ 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."
+ 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)
diff --git a/bot/constants.py b/bot/constants.py
index aecd6be59..13f25e4f8 100644
--- a/bot/constants.py
+++ b/bot/constants.py
@@ -345,6 +345,7 @@ class Channels(metaclass=YAMLGetter):
help_7: int
helpers: int
message_log: int
+ meta: int
mod_alerts: int
modlog: int
off_topic_0: int
diff --git a/config-default.yml b/config-default.yml
index 30d505d6d..071478206 100644
--- a/config-default.yml
+++ b/config-default.yml
@@ -324,8 +324,24 @@ anti_spam:
anti_malware:
- whitelist: ['.bmp', '.gif', '.jpg', '.jpeg', '.png', '.tiff', # Images
- '.3gp', '.3g2', '.avi', '.h264', '.m4v', '.mkv', '.mov', '.mp4', '.mpeg', '.mpg', '.wmv' ] # Videos
+ whitelist:
+ - '.3gp'
+ - '.3g2'
+ - '.avi'
+ - '.bmp'
+ - '.gif'
+ - '.h264'
+ - '.jpg'
+ - '.jpeg'
+ - '.m4v'
+ - '.mkv'
+ - '.mov'
+ - '.mp4'
+ - '.mpeg'
+ - '.mpg'
+ - '.png'
+ - '.tiff'
+ - '.wmv'
reddit: