diff options
author | 2024-02-17 10:56:10 +0100 | |
---|---|---|
committer | 2024-03-29 23:41:19 +0100 | |
commit | 4072c8830c9d7f303d0d39b680e21997895ad9b7 (patch) | |
tree | fb004da9f9e4b703da74fda92bdc83cf472cd711 | |
parent | cap timeout duration upon edit (diff) |
apply timeouts on members who left the server
If timeout was edited to a longer duration, members could evade being timeout upon rejoining, so we reapply them if that's the case
-rw-r--r-- | bot/exts/moderation/infraction/infractions.py | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/bot/exts/moderation/infraction/infractions.py b/bot/exts/moderation/infraction/infractions.py index 3b2f2810a..f2dbd858d 100644 --- a/bot/exts/moderation/infraction/infractions.py +++ b/bot/exts/moderation/infraction/infractions.py @@ -1,6 +1,6 @@ import textwrap import typing as t -from datetime import timedelta +from datetime import UTC, timedelta import arrow import discord @@ -659,6 +659,32 @@ class Infractions(InfractionScheduler, commands.Cog): await ctx.send(str(error.errors[0])) error.handled = True + @commands.Cog.listener() + async def on_member_join(self, member: Member) -> None: + """ + + Apply active timeout infractions for returning members. + + This is needed for users who might have had their infraction edited in our database but not in Discord itself. + """ + active_timeouts = await self.bot.api_client.get( + endpoint="bot/infractions", + params={"active": "true", "type": "timeout", "user__id": member.id} + ) + + if active_timeouts: + timeout_infraction = active_timeouts[0] + expiry = arrow.get(timeout_infraction["expires_at"], tzinfo=UTC).datetime.replace(second=0, microsecond=0) + + if member.is_timed_out() and expiry == member.timed_out_until.replace(second=0, microsecond=0): + return + + reason = f"Applying active timeout for returning member: {timeout_infraction['id']}" + + async def action() -> None: + await member.edit(timed_out_until=expiry, reason=reason) + await self.reapply_infraction(timeout_infraction, action) + async def setup(bot: Bot) -> None: """Load the Infractions cog.""" |