Skip to content

Commit 486e608

Browse files
feat: add autoban (#509)
1 parent f8b80b5 commit 486e608

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ platforms such as GitHub discussions/issues might be added in the future.
4545
|----------------------------------|----------|------------------------------------------------------|--------------------------------------------------------------------------------------------|
4646
| DATA_REPO | False | `https://github.com/LizardByte/support-bot-data` | Repository to store persistent data. This repository should be private! |
4747
| DATA_REPO_BRANCH | False | `master` | Branch to store persistent data. |
48+
| DISCORD_AUTOBAN_CHANNEL_ID | False | `None` | Channel ID where any message posted results in an automatic ban (7-day message deletion). |
4849
| DISCORD_BOT_TOKEN | True | `None` | Token from Bot page on discord developer portal. |
4950
| DISCORD_CLIENT_ID | True | `None` | Discord OAuth2 client id. |
5051
| DISCORD_CLIENT_SECRET | True | `None` | Discord OAuth2 client secret. |

sample.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ DAILY_TASKS_UTC_HOUR=12
66
SUPPORT_COMMANDS_BRANCH=master
77

88
# Secret settings
9+
DISCORD_AUTOBAN_CHANNEL_ID=
910
DISCORD_BOT_TOKEN=
1011
DISCORD_LEVEL_UP_CHANNEL_ID=
1112
DISCORD_LOG_CHANNEL_ID=

src/discord_bot/cogs/autoban.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# standard imports
2+
import logging
3+
import os
4+
5+
# lib imports
6+
import discord
7+
from discord.ext import commands
8+
9+
# Get logger for this module
10+
logger = logging.getLogger(__name__)
11+
12+
13+
class AutoBanCog(discord.Cog):
14+
"""
15+
Discord cog that automatically bans any user who posts in the configured autoban channel.
16+
"""
17+
18+
def __init__(self, bot):
19+
self.bot = bot
20+
21+
@commands.Cog.listener()
22+
async def on_message(self, message: discord.Message):
23+
"""
24+
Listen for messages and ban the author if the message is in the autoban channel.
25+
26+
Any message sent to the channel defined by ``DISCORD_AUTOBAN_CHANNEL_ID`` will result in
27+
the author being banned from the guild. All messages from the user within the past 7 days
28+
are also deleted. Bot messages are ignored.
29+
30+
Parameters
31+
----------
32+
message : discord.Message
33+
The message object that triggered this event.
34+
"""
35+
if message.author.bot:
36+
return
37+
38+
autoban_channel_id = os.getenv('DISCORD_AUTOBAN_CHANNEL_ID')
39+
if not autoban_channel_id:
40+
return
41+
42+
if message.channel.id != int(autoban_channel_id):
43+
return
44+
45+
guild = message.guild
46+
if not guild:
47+
return
48+
49+
try:
50+
await guild.ban(
51+
user=message.author,
52+
reason="Automatic ban: posted in restricted channel.",
53+
delete_message_seconds=604800, # Delete messages from the past 7 days
54+
)
55+
logger.warning(
56+
"Auto-banned user %s (%s) for posting in channel %s (%s).",
57+
message.author,
58+
message.author.id,
59+
message.channel.name,
60+
message.channel.id,
61+
)
62+
except discord.Forbidden:
63+
logger.error(
64+
"Missing permissions to ban user %s (%s) in guild %s (%s).",
65+
message.author,
66+
message.author.id,
67+
guild.name,
68+
guild.id,
69+
)
70+
except discord.HTTPException as e:
71+
logger.error(
72+
"HTTP error while banning user %s (%s): %s",
73+
message.author,
74+
message.author.id,
75+
e,
76+
exc_info=True,
77+
)
78+
79+
80+
def setup(bot: discord.Bot):
81+
bot.add_cog(AutoBanCog(bot=bot))

0 commit comments

Comments
 (0)