Skip to content

Delete automated infraction messages after a period of time#3339

Merged
jb3 merged 3 commits intomainfrom
jb3/infraction-message-tidy-up
Jun 23, 2025
Merged

Delete automated infraction messages after a period of time#3339
jb3 merged 3 commits intomainfrom
jb3/infraction-message-tidy-up

Conversation

@jb3
Copy link
Copy Markdown
Member

@jb3 jb3 commented Jun 23, 2025

This PR automatically deletes messages generated by the bot as part of dealing out automated infractions (e.g. spam filters). These have a habit of filling up channels which are frequently abused but have low traffic otherwise (some channels appear as just a wall of automated infractions).

As of now, the timer is set to 8 hours, though we can increase this if we feel we need the longer persistence (though obviously the full record and log is still preserved in the #mod-alerts channel).

Deletion times are persisted to Redis to ensure we try to delete stuff after the bot restarts.

Closes #2317.

@jb3 jb3 requested a review from mbaruh as a code owner June 23, 2025 03:51
@jb3 jb3 requested review from ChrisLovering and Copilot June 23, 2025 03:54
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR ensures that automated infraction messages sent by the bot are cleaned up after a configurable delay, persisting deletion schedules across restarts via Redis.

  • Adds an AUTOMATED_TIDY_UP_HOURS constant and Redis-backed cache for message deletion times.
  • Introduces a separate tidy_up_scheduler alongside existing expiration scheduling.
  • Implements _delete_infraction_message for safe removal and cache cleanup, and integrates scheduling in cog_load and apply_infraction.
Comments suppressed due to low confidence (1)

bot/exts/moderation/infraction/_scheduler.py:330

  • Add tests to cover the scheduling and execution of automated message deletions, including verifying that entries are persisted to and removed from Redis as expected across restarts.
            self.tidy_up_scheduler.schedule_at(

Comment thread bot/exts/moderation/infraction/_scheduler.py
Comment thread bot/exts/moderation/infraction/_scheduler.py Outdated
@jb3 jb3 requested a review from jchristgit June 23, 2025 04:43
Comment thread bot/exts/moderation/infraction/_scheduler.py
channel = await get_or_fetch_channel(self.bot, channel_id)
if channel is None:
log.warning(f"Channel {channel_id} not found for infraction message deletion.")
return
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also delete it from Redis in this case? On one hand, it's nice to be able to try later if there is a transient issue with fetching the channel. On the other hand, it's wasteful to keep trying if the channel has been deleted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider setting a TTL in Redis for these records. That way if a channel truly is gone, stale records won't pile up.

@vivekashok1221 vivekashok1221 self-requested a review June 23, 2025 05:26
@MarkKoz
Copy link
Copy Markdown
Contributor

MarkKoz commented Jun 23, 2025

How does this interact with threads that get closed? Can the bot still delete messages?

@jb3
Copy link
Copy Markdown
Member Author

jb3 commented Jun 23, 2025

Bot should always have permissions to delete messages anywhere in the server. I will handle discord.Forbidden though and ensure we log it out (but still clear the record, this should all be best effort).

@jb3 jb3 force-pushed the jb3/infraction-message-tidy-up branch from 8bef06b to b50ba64 Compare June 23, 2025 05:51
@jb3 jb3 force-pushed the jb3/infraction-message-tidy-up branch from b50ba64 to c4e62e4 Compare June 23, 2025 05:52
except discord.NotFound:
log.warning(f"Channel or message {message_id} not found in channel {channel_id}.")
except discord.Forbidden:
log.warning(f"Bot lacks permissions to delete message {message_id} in channel {channel_id}.")
Copy link
Copy Markdown
Member

@ChrisLovering ChrisLovering Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if this should always be a warning, since trying to delete messages in closed threads will also raise Forbidden iirc. No point in sending that specific case to sentry

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I hadn't realised that manage messages does not override this. I'll drop it to an info log.

@jb3 jb3 merged commit 4b04acb into main Jun 23, 2025
5 checks passed
@jb3 jb3 deleted the jb3/infraction-message-tidy-up branch June 23, 2025 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Delete 'Applied mute to …' message

4 participants