From 02ebf99cfe1b23d944df77d25beef27719d883d1 Mon Sep 17 00:00:00 2001 From: JefeThePug <107787719+JefeThePug@users.noreply.github.com> Date: Sat, 20 Jul 2024 11:15:21 +0900 Subject: [PATCH] Create admin_block_exe.py FEATURE/ Bot message listener to prevent users from sending executable or zipped files in message attachments. --- src/zorak/cogs/admin/admin_block_exe.py | 62 +++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/zorak/cogs/admin/admin_block_exe.py diff --git a/src/zorak/cogs/admin/admin_block_exe.py b/src/zorak/cogs/admin/admin_block_exe.py new file mode 100644 index 00000000..398e13c4 --- /dev/null +++ b/src/zorak/cogs/admin/admin_block_exe.py @@ -0,0 +1,62 @@ +""" +Listener to scan for exe or zip files. +""" +import logging + +from discord.ext import commands + +from zorak.utilities.cog_helpers._embeds import embed_cant_do_that + +logger = logging.getLogger(__name__) + + +class FiletypeChecker(commands.Cog): + """ + Listener to check if a exe file or a zip file is attached in + a user's message to prevent malicious files from being sent. + """ + + def __init__(self, bot): + self.bot = bot + + @commands.Cog.listener() + async def on_message(self, message): + for attachment in message.attachments: + print(f"content type: {attachment.content_type}") + if attachment.content_type in [ + "application/x-msdownload", + "application/x-dosexec", + "application/x-msdos-program", + "application/octet-stream", + "application/x-binary", + "application/x-mach-binary", + "application/x-apple-diskimage", + "application/java-archive", + "application/x-java-class", + "application/zip", + "application/x-zip-compressed", + "multipart/x-zip", + "application/x-compressed", + "application/x-compress", + "application/x-zip", + "application/zip-compressed", + "multipart/x-compressed", + ]: + await message.delete() + await message.channel.send( + embed=embed_cant_do_that( + "You cannot send executable files or zip files." + ), + delete_after=10, + ) + logger.info( + f"Blocked attachment from {message.author}:\n{message.clean_content}\nAttachment:{attachment.filename}" + ) + return + + +def setup(bot): + """ + required. + """ + bot.add_cog(FiletypeChecker(bot))