Skip to content

Fix automod punishment duration always saving as 0 seconds - #279

Merged
LightSage merged 1 commit into
masterfrom
copilot/look-into-automod-save-issue
Aug 27, 2026
Merged

Fix automod punishment duration always saving as 0 seconds#279
LightSage merged 1 commit into
masterfrom
copilot/look-into-automod-save-issue

Conversation

Copilot AI commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Setting a Mute/Ban punishment duration via the automod interactive UI's "Configure Punishment Duration" modal always persisted a duration of 0, regardless of what was entered (e.g. 30m, 2h).

Root cause

ShortTime parses durations into a dateutil.relativedelta, which keeps days, hours, minutes, and seconds as distinct components (no auto-normalization into a single unit). The duration-to-seconds conversion only summed days and seconds, silently discarding hours and minutes.

# before
duration = delta.days * 86400 + delta.seconds
# "30m" -> delta.minutes=30, delta.days=0, delta.seconds=0 -> duration = 0

Changes

  • lightning/cogs/automod/ui.pyConfigurePunishmentDurationButton.callback now includes hours and minutes when computing selected_punishment_duration.
  • lightning/cogs/automod/cog.pyadd_automod_rules had the identical bug when converting punishment_duration to a stored duration; fixed with the same logic for consistency.
# after
duration = delta.days * 86400 + delta.hours * 3600 + delta.minutes * 60 + delta.seconds

Summary by Sourcery

Bug Fixes:

  • Correct automod Mute and Ban punishment duration conversion so hours and minutes are preserved instead of being saved as zero seconds.

Co-authored-by: LightSage <46062298+LightSage@users.noreply.github.com>
@sourcery-ai

sourcery-ai Bot commented Aug 27, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Fixes automod mute and ban durations by converting every component of ShortTime's relativedelta into total seconds, preventing hour- and minute-based durations from being persisted as zero.

Sequence diagram for automod punishment duration conversion

sequenceDiagram
    actor Moderator
    participant UI as ConfigurePunishmentDurationButton
    participant Rules as add_automod_rules
    participant Storage as AutomodRuleStorage

    Moderator->>UI: callback(interaction)
    UI->>UI: ShortTime.delta
    UI->>UI: selected_punishment_duration = days*86400 + hours*3600 + minutes*60 + seconds
    UI->>Rules: punishment_duration
    Rules->>Rules: punishment_duration.delta
    Rules->>Rules: duration = days*86400 + hours*3600 + minutes*60 + seconds
    Rules->>Storage: Store BAN or MUTE duration
Loading

File-Level Changes

Change Details Files
Preserve all duration components when converting parsed punishment durations to seconds.
  • Include days, hours, minutes, and seconds in the automod rule payload conversion.
  • Apply the same complete conversion in the interactive punishment-duration UI state.
lightning/cogs/automod/cog.py
lightning/cogs/automod/ui.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="lightning/cogs/automod/cog.py" line_range="299-301" />
<code_context>

         if punishment.name in ("BAN", "MUTE") and punishment_duration:
-            punishment_payload['duration'] = punishment_duration.delta.seconds
+            delta = punishment_duration.delta
+            punishment_payload['duration'] = delta.days * 86400 + delta.hours * 3600 + delta.minutes * 60 \
+                + delta.seconds

         payload = {"guild_id": ctx.guild.id,
</code_context>
<issue_to_address>
**issue (bug_risk):** `add_automod_rules` still converts `ShortTime` values containing years or months to zero seconds because the calculation ignores `delta.years` and `delta.months`. Inputs such as `1mo` or `1y` are accepted by `ShortTime`, so a Mute duration becomes an indefinite mute and a Ban duration is treated as permanent.

**Triggers:** When the slash command is given a punishment duration containing months or years, such as `1mo` or `1y`.

**Suggested fix:** Reject calendar-based units for this command, or convert the requested duration using an explicitly defined calendar-duration policy before storing seconds.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and if the conversion is wrong, BAN or MUTE rules could be saved with incorrect durations and restrict users for too long or too little. Reverting would not change rules already saved, but those configurations can be corrected or removed.

Blocking findings: lightning/cogs/automod/cog.py:301


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread lightning/cogs/automod/cog.py
@LightSage
LightSage merged commit 8fda24b into master Aug 27, 2026
4 checks passed
@LightSage
LightSage deleted the copilot/look-into-automod-save-issue branch August 27, 2026 03:19
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.

2 participants