Skip to content

Fix off-by-one errors and silent reminder deletion in reminders.py#3509

Open
oliveman-au wants to merge 1 commit intopython-discord:mainfrom
oliveman-au:fix/reminders-v3
Open

Fix off-by-one errors and silent reminder deletion in reminders.py#3509
oliveman-au wants to merge 1 commit intopython-discord:mainfrom
oliveman-au:fix/reminders-v3

Conversation

@oliveman-au
Copy link
Copy Markdown
Contributor

Found a few bugs in bot/exts/utils/reminders.py while going through the reminders codebase. Two off-by-one errors and a silent deletion issue when the bot loses channel cache.


1. Off-by-one — users could create 6 reminders despite a limit of 5

The check was using > instead of >=, so the 6th reminder always slipped through before the denial fired.

# Before
if len(active_reminders) > MAXIMUM_REMINDERS:

# After
if len(active_reminders) >= MAXIMUM_REMINDERS:

2. Off-by-one — users could delete 6 reminders at once despite a limit of 5

Same mistake in delete_reminder, which also directly contradicted the docstring saying "up to and including 5".

# Before
if len(ids) > 5:

# After
if len(ids) >= 5:

3. Reminders silently deleted with no user notification when channel cache is lost

get_channel() only checks the local cache, so after a bot restart the channel returns None and the reminder gets permanently deleted with no DM, no message, nothing to the user. Switched to get_or_fetch_channel() as suggested by wookie184, and added a DM to the user if the channel truly can't be found so they at least get their reminder content back. Also updated both call sites to await the method since it's now async.

# Before
def ensure_valid_reminder(self, reminder: dict) -> tuple[bool, discord.TextChannel]:
    channel = self.bot.get_channel(reminder["channel_id"])
    ...

# After
async def ensure_valid_reminder(self, reminder: dict) -> tuple[bool, discord.TextChannel | None]:
    channel = await get_or_fetch_channel(self.bot, reminder["channel_id"])
    ...

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.

1 participant