Skip to content

Commit ca634c9

Browse files
authored
Merge pull request #1893 from python-discord/filters/autoban
Filtering: add autoban on specific reasons
2 parents 3b846ae + d55197b commit ca634c9

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

bot/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ class Channels(metaclass=YAMLGetter):
444444
incidents: int
445445
incidents_archive: int
446446
mod_alerts: int
447+
mod_meta: int
447448
nominations: int
448449
nomination_voting: int
449450
organisation: int

bot/exts/filters/filter_lists.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from bot import constants
77
from bot.api import ResponseCodeError
88
from bot.bot import Bot
9+
from bot.constants import Channels
910
from bot.converters import ValidDiscordServerInvite, ValidFilterListType
1011
from bot.log import get_logger
1112
from bot.pagination import LinePaginator
@@ -100,6 +101,12 @@ async def _add_data(
100101
)
101102
raise
102103

104+
# If it is an autoban trigger we send a warning in #mod-meta
105+
if comment and "[autoban]" in comment:
106+
await self.bot.get_channel(Channels.mod_meta).send(
107+
f":warning: Heads-up! The new filter `{content}` (`{comment}`) will automatically ban users."
108+
)
109+
103110
# Insert the item into the cache
104111
self.bot.insert_item_into_filter_list_cache(item)
105112
await ctx.message.add_reaction("✅")

bot/exts/filters/filtering.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@
4444
DAYS_BETWEEN_ALERTS = 3
4545
OFFENSIVE_MSG_DELETE_TIME = timedelta(days=Filter.offensive_msg_delete_days)
4646

47+
# Autoban
48+
LINK_PASSWORD = "https://support.discord.com/hc/en-us/articles/218410947-I-forgot-my-Password-Where-can-I-set-a-new-one"
49+
LINK_2FA = "https://support.discord.com/hc/en-us/articles/219576828-Setting-up-Two-Factor-Authentication"
50+
AUTO_BAN_REASON = (
51+
"Your account has been used to send links to a phishing website. You have been automatically banned. "
52+
"If you are not aware of sending them, that means your account has been compromised.\n\n"
53+
54+
f"Here is a guide from Discord on [how to change your password]({LINK_PASSWORD}).\n\n"
55+
56+
f"We also highly recommend that you [enable 2 factor authentication on your account]({LINK_2FA}), "
57+
"for heightened security.\n\n"
58+
59+
"Once you have changed your password, feel free to follow the instructions at the bottom of "
60+
"this message to appeal your ban."
61+
)
62+
AUTO_BAN_DURATION = timedelta(days=4)
63+
4764
FilterMatch = Union[re.Match, dict, bool, List[discord.Embed]]
4865

4966

@@ -347,6 +364,23 @@ async def _filter_message(self, msg: Message, delta: Optional[int] = None) -> No
347364
stats = self._add_stats(filter_name, match, msg.content)
348365
await self._send_log(filter_name, _filter, msg, stats, reason)
349366

367+
# If the filter reason contains `[autoban]`, we want to auto-ban the user
368+
if reason and "[autoban]" in reason.lower():
369+
# Create a new context, with the author as is the bot, and the channel as #mod-alerts.
370+
# This sends the ban confirmation directly under watchlist trigger embed, to inform
371+
# mods that the user was auto-banned for the message.
372+
context = await self.bot.get_context(msg)
373+
context.author = self.bot.get_guild(Guild.id).get_member(self.bot.user.id)
374+
context.channel = self.bot.get_channel(Channels.mod_alerts)
375+
context.command = self.bot.get_command("tempban")
376+
377+
await context.invoke(
378+
context.command,
379+
msg.author,
380+
arrow.utcnow() + AUTO_BAN_DURATION,
381+
reason=AUTO_BAN_REASON
382+
)
383+
350384
break # We don't want multiple filters to trigger
351385

352386
async def _send_log(
@@ -368,6 +402,10 @@ async def _send_log(
368402
# Allow specific filters to override ping_everyone
369403
ping_everyone = Filter.ping_everyone and _filter.get("ping_everyone", True)
370404

405+
# If we are going to autoban, we don't want to ping
406+
if reason and "[autoban]" in reason:
407+
ping_everyone = False
408+
371409
eval_msg = "using !eval " if is_eval else ""
372410
footer = f"Reason: {reason}" if reason else None
373411
message = (

bot/exts/moderation/infraction/_scheduler.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,7 @@ async def apply_infraction(
175175
dm_log_text = "\nDM: Sent"
176176

177177
end_msg = ""
178-
if infraction["actor"] == self.bot.user.id:
179-
log.trace(
180-
f"Infraction #{id_} actor is bot; including the reason in the confirmation message."
181-
)
182-
if reason:
183-
end_msg = f" (reason: {textwrap.shorten(reason, width=1500, placeholder='...')})"
184-
elif is_mod_channel(ctx.channel):
178+
if is_mod_channel(ctx.channel):
185179
log.trace(f"Fetching total infraction count for {user}.")
186180

187181
infractions = await self.bot.api_client.get(
@@ -190,6 +184,12 @@ async def apply_infraction(
190184
)
191185
total = len(infractions)
192186
end_msg = f" (#{id_} ; {total} infraction{ngettext('', 's', total)} total)"
187+
elif infraction["actor"] == self.bot.user.id:
188+
log.trace(
189+
f"Infraction #{id_} actor is bot; including the reason in the confirmation message."
190+
)
191+
if reason:
192+
end_msg = f" (reason: {textwrap.shorten(reason, width=1500, placeholder='...')})"
193193

194194
purge = infraction.get("purge", "")
195195

config-default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ guild:
207207
incidents_archive: 720668923636351037
208208
mod_alerts: 473092532147060736
209209
mods: &MODS 305126844661760000
210+
mod_meta: 775412552795947058
210211
nominations: 822920136150745168
211212
nomination_voting: 822853512709931008
212213
organisation: &ORGANISATION 551789653284356126

0 commit comments

Comments
 (0)