From 8f04fd436f70e591e21a54345885c13137089c2c Mon Sep 17 00:00:00 2001 From: Flame442 <34169552+Flame442@users.noreply.github.com> Date: Mon, 20 Jan 2020 14:09:17 -0800 Subject: [PATCH] Catches `discord.NotFound` in `utils.mod.mass_purge` (#3414) * Catches `discord.NotFound` in `mass_purge` * Create 3378.bugfix.rst --- changelog.d/3378.bugfix.rst | 1 + redbot/core/utils/mod.py | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 changelog.d/3378.bugfix.rst diff --git a/changelog.d/3378.bugfix.rst b/changelog.d/3378.bugfix.rst new file mode 100644 index 000000000..caff4e2ef --- /dev/null +++ b/changelog.d/3378.bugfix.rst @@ -0,0 +1 @@ +Fixed an error when ``redbot.core.utils.mod.mass_purge`` is passed ``COUNT % 100 == 1`` messages AND the last message in the list does not exist. diff --git a/redbot/core/utils/mod.py b/redbot/core/utils/mod.py index 030eaa3ba..2b5b1df10 100644 --- a/redbot/core/utils/mod.py +++ b/redbot/core/utils/mod.py @@ -38,12 +38,13 @@ async def mass_purge(messages: List[discord.Message], channel: discord.TextChann """ while messages: - if len(messages) > 1: + # discord.NotFound can be raised when `len(messages) == 1` and the message does not exist. + # As a result of this obscure behavior, this error needs to be caught just in case. + try: await channel.delete_messages(messages[:100]) - messages = messages[100:] - else: - await messages[0].delete() - messages = [] + except discord.errors.HTTPException: + pass + messages = messages[100:] await asyncio.sleep(1.5)