Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

channel.purge() can fail early if a message is deleted during execution #9830

Open
3 tasks done
Theroxenes opened this issue May 15, 2024 · 0 comments
Open
3 tasks done
Labels
unconfirmed bug A bug report that needs triaging

Comments

@Theroxenes
Copy link

Theroxenes commented May 15, 2024

Summary

If the purge method uses single-delete mode it can fail the entire method on missing messages

Reproduction Steps

  1. Initiate a purge of messages with bulk=False or have it automatically fall back to the single delete strategy.
  2. Manually delete one of the messages marked for purge before the method finishes.

Minimal Reproducible Code

import datetime
from discord.ext import commands

# Post several messages, call this command, then manually delete a message while it is running.
@commands.command()
async def purge_test(ctx, minutes: int=10)
    after_time = datetime.datetime.now() - datetime.timedelta(minutes=minutes)
    def purge_check(message):
        return (message.author.id == ctx.author.id) and (message.channel.id == ctx.channel.id)

    # bulk is set to False here to force the behavior but this can occur with bulk=True
    return await ctx.channel.purge(limit=None, reason='test', after=after_time, bulk=False)

Expected Results

Purge to continue gracefully after encountering a missing message, or defer errors raised until the rest of the purge is completed.

Actual Results

discord.errors.NotFound is raised and the purge terminates early, even if the error is handled.

Traceback:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10008): Unknown Message
Traceback (most recent call last):
  File "/home/thero/sinbot/venv/lib/python3.10/site-packages/discord/ext/commands/core.py", line 235, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/thero/sinbot/cogs/messagelogging.py", line 517, in clear
    to_log = (await channel.purge(limit=None, check=purge_check, after=earliest_time, reason=f"Clear command invoked by {ctx.author.display_name}."))
  File "/home/thero/sinbot/venv/lib/python3.10/site-packages/discord/channel.py", line 531, in purge
    return await discord.abc._purge_helper(
  File "/home/thero/sinbot/venv/lib/python3.10/site-packages/discord/abc.py", line 152, in _purge_helper
    await strategy(to_delete, reason=reason)
  File "/home/thero/sinbot/venv/lib/python3.10/site-packages/discord/abc.py", line 124, in _single_delete_strategy
    await m.delete()
  File "/home/thero/sinbot/venv/lib/python3.10/site-packages/discord/message.py", line 859, in delete
    await self._state.http.delete_message(self.channel.id, self.id)
  File "/home/thero/sinbot/venv/lib/python3.10/site-packages/discord/http.py", line 741, in request
    raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/thero/sinbot/venv/lib/python3.10/site-packages/discord/ext/commands/bot.py", line 1350, in invoke
    await ctx.command.invoke(ctx)
  File "/home/thero/sinbot/venv/lib/python3.10/site-packages/discord/ext/commands/core.py", line 1029, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
  File "/home/thero/sinbot/venv/lib/python3.10/site-packages/discord/ext/commands/core.py", line 244, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInv
okeError: Command raised an exception: NotFound: 404 Not Found (error code: 10008): Unknown Message

Intents

members, presences, message_content

System Information

  • Python v3.10.12-final
  • discord.py v2.3.2-final
  • aiohttp v3.9.5
  • system info: Linux 5.15.0-101-generic fix typo #111-Ubuntu SMP Tue Mar 5 20:16:58 UTC 2024

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

This was encountered in the wild without forcing bulk=False in the purge method, I believe it was triggered because it was iterating channels to clear a user's messages entirely and they had a single message in a channel, which was deleted separately while the bot was running its cleanup command.

This particular case can be error-handled in a cleanup command easily, but I believe it is possible for this to happen in much more annoying ways such as a purge running into a message > 14 days old that is deleted before it can complete.

If you are trying to collect the results of a purge and this happens I cannot think of a good way to handle it, as some messages will be deleted but never get returned.

@Theroxenes Theroxenes added the unconfirmed bug A bug report that needs triaging label May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unconfirmed bug A bug report that needs triaging
Projects
None yet
Development

No branches or pull requests

1 participant