diff options
-rw-r--r-- | docs/changelog.rst | 4 | ||||
-rw-r--r-- | pydis_core/utils/interactions.py | 29 | ||||
-rw-r--r-- | pyproject.toml | 2 |
3 files changed, 27 insertions, 8 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst index ebb203f5..610b2d37 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,10 @@ Changelog ========= +- :release:`9.1.1 <14th November 2022>` +- :bug:`162` Handle not being able to delete the interaction message on button press/timeout. + + - :release:`9.1.0 <13th November 2022>` - :feature:`158` Bump Discord.py to :literal-url:`2.1.0 <https://github.com/Rapptz/discord.py/releases/tag/v2.1.0>`. diff --git a/pydis_core/utils/interactions.py b/pydis_core/utils/interactions.py index 3e4acffe..a6746e1e 100644 --- a/pydis_core/utils/interactions.py +++ b/pydis_core/utils/interactions.py @@ -1,13 +1,30 @@ -import contextlib -from typing import Optional, Sequence +from typing import Literal, Optional, Sequence -from discord import ButtonStyle, Interaction, Message, NotFound, ui +from discord import ButtonStyle, HTTPException, Interaction, Message, NotFound, ui from pydis_core.utils.logging import get_logger log = get_logger(__name__) +async def _handle_modify_message(message: Message, action: Literal["edit", "delete"]) -> None: + """Remove the view from, or delete the given message depending on the specified action.""" + try: + if action == "edit": + await message.edit(view=None) + elif action == "delete": + await message.delete() + except HTTPException as e: + # Cover the case where this message has been deleted by external means, + # or the message is now in an archived/locked thread. + if e.code == 50083: + log.debug(f"Could not {action} message {message.id} due to it being in an archived thread.") + elif isinstance(e, NotFound): + log.info(f"Could not find message {message.id} when attempting to {action} it.") + else: + log.error(f"Could not {action} message {message.id} due to Discord HTTP error:\n{str(e)}") + + class ViewWithUserAndRoleCheck(ui.View): """ A view that allows the original invoker and moderators to interact with it. @@ -65,9 +82,7 @@ class ViewWithUserAndRoleCheck(ui.View): async def on_timeout(self) -> None: """Remove the view from ``self.message`` if set.""" if self.message: - with contextlib.suppress(NotFound): - # Cover the case where this message has already been deleted by external means - await self.message.edit(view=None) + await _handle_modify_message(self.message, "edit") class DeleteMessageButton(ui.Button): @@ -95,4 +110,4 @@ class DeleteMessageButton(ui.Button): async def callback(self, interaction: Interaction) -> None: """Delete the original message on button click.""" - await interaction.message.delete() + await _handle_modify_message(interaction.message, "delete") diff --git a/pyproject.toml b/pyproject.toml index 5386ec1e..97d79457 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pydis_core" -version = "9.1.0" +version = "9.1.1" description = "PyDis core provides core functionality and utility to the bots of the Python Discord community." authors = ["Python Discord <[email protected]>"] license = "MIT" |