Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`.

Expand Down
29 changes: 22 additions & 7 deletions pydis_core/utils/interactions.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -65,9 +82,7 @@ async def interaction_check(self, interaction: Interaction) -> bool:
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):
Expand Down Expand Up @@ -95,4 +110,4 @@ def __init__(

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")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <info@pythondiscord.com>"]
license = "MIT"
Expand Down