diff --git a/README.md b/README.md index 6ff858c..ee609d7 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ platforms such as GitHub discussions/issues might be added in the future. |----------------------------------|----------|------------------------------------------------------|--------------------------------------------------------------------------------------------| | DATA_REPO | False | `https://github.com/LizardByte/support-bot-data` | Repository to store persistent data. This repository should be private! | | DATA_REPO_BRANCH | False | `master` | Branch to store persistent data. | +| DISCORD_AUTOBAN_CHANNEL_ID | False | `None` | Channel ID where any message posted results in an automatic ban (7-day message deletion). | | DISCORD_BOT_TOKEN | True | `None` | Token from Bot page on discord developer portal. | | DISCORD_CLIENT_ID | True | `None` | Discord OAuth2 client id. | | DISCORD_CLIENT_SECRET | True | `None` | Discord OAuth2 client secret. | diff --git a/sample.env b/sample.env index 49f8a38..5859cc1 100644 --- a/sample.env +++ b/sample.env @@ -6,6 +6,7 @@ DAILY_TASKS_UTC_HOUR=12 SUPPORT_COMMANDS_BRANCH=master # Secret settings +DISCORD_AUTOBAN_CHANNEL_ID= DISCORD_BOT_TOKEN= DISCORD_LEVEL_UP_CHANNEL_ID= DISCORD_LOG_CHANNEL_ID= diff --git a/src/discord_bot/cogs/autoban.py b/src/discord_bot/cogs/autoban.py new file mode 100644 index 0000000..ca67136 --- /dev/null +++ b/src/discord_bot/cogs/autoban.py @@ -0,0 +1,81 @@ +# standard imports +import logging +import os + +# lib imports +import discord +from discord.ext import commands + +# Get logger for this module +logger = logging.getLogger(__name__) + + +class AutoBanCog(discord.Cog): + """ + Discord cog that automatically bans any user who posts in the configured autoban channel. + """ + + def __init__(self, bot): + self.bot = bot + + @commands.Cog.listener() + async def on_message(self, message: discord.Message): + """ + Listen for messages and ban the author if the message is in the autoban channel. + + Any message sent to the channel defined by ``DISCORD_AUTOBAN_CHANNEL_ID`` will result in + the author being banned from the guild. All messages from the user within the past 7 days + are also deleted. Bot messages are ignored. + + Parameters + ---------- + message : discord.Message + The message object that triggered this event. + """ + if message.author.bot: + return + + autoban_channel_id = os.getenv('DISCORD_AUTOBAN_CHANNEL_ID') + if not autoban_channel_id: + return + + if message.channel.id != int(autoban_channel_id): + return + + guild = message.guild + if not guild: + return + + try: + await guild.ban( + user=message.author, + reason="Automatic ban: posted in restricted channel.", + delete_message_seconds=604800, # Delete messages from the past 7 days + ) + logger.warning( + "Auto-banned user %s (%s) for posting in channel %s (%s).", + message.author, + message.author.id, + message.channel.name, + message.channel.id, + ) + except discord.Forbidden: + logger.error( + "Missing permissions to ban user %s (%s) in guild %s (%s).", + message.author, + message.author.id, + guild.name, + guild.id, + ) + except discord.HTTPException as e: + logger.error( + "HTTP error while banning user %s (%s): %s", + message.author, + message.author.id, + e, + exc_info=True, + ) + + +def setup(bot: discord.Bot): + bot.add_cog(AutoBanCog(bot=bot))