Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
1 change: 1 addition & 0 deletions sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
81 changes: 81 additions & 0 deletions src/discord_bot/cogs/autoban.py
Original file line number Diff line number Diff line change
@@ -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))
Loading