Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions bot/cogs/antimalware.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand All @@ -17,31 +17,29 @@ def __init__(self, bot: Bot):
@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:
Expand Down