Skip to content

Commit 3d53d05

Browse files
Merge pull request #162 from python-discord/handle-thread-close-on-interactin
Handle not being able to delete the interaction message on button press.
2 parents a0f08ba + fe29777 commit 3d53d05

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
Changelog
55
=========
66

7+
- :release:`9.1.1 <14th November 2022>`
8+
- :bug:`162` Handle not being able to delete the interaction message on button press/timeout.
9+
10+
711
- :release:`9.1.0 <13th November 2022>`
812
- :feature:`158` Bump Discord.py to :literal-url:`2.1.0 <https://github.com/Rapptz/discord.py/releases/tag/v2.1.0>`.
913

pydis_core/utils/interactions.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
import contextlib
2-
from typing import Optional, Sequence
1+
from typing import Literal, Optional, Sequence
32

4-
from discord import ButtonStyle, Interaction, Message, NotFound, ui
3+
from discord import ButtonStyle, HTTPException, Interaction, Message, NotFound, ui
54

65
from pydis_core.utils.logging import get_logger
76

87
log = get_logger(__name__)
98

109

10+
async def _handle_modify_message(message: Message, action: Literal["edit", "delete"]) -> None:
11+
"""Remove the view from, or delete the given message depending on the specified action."""
12+
try:
13+
if action == "edit":
14+
await message.edit(view=None)
15+
elif action == "delete":
16+
await message.delete()
17+
except HTTPException as e:
18+
# Cover the case where this message has been deleted by external means,
19+
# or the message is now in an archived/locked thread.
20+
if e.code == 50083:
21+
log.debug(f"Could not {action} message {message.id} due to it being in an archived thread.")
22+
elif isinstance(e, NotFound):
23+
log.info(f"Could not find message {message.id} when attempting to {action} it.")
24+
else:
25+
log.error(f"Could not {action} message {message.id} due to Discord HTTP error:\n{str(e)}")
26+
27+
1128
class ViewWithUserAndRoleCheck(ui.View):
1229
"""
1330
A view that allows the original invoker and moderators to interact with it.
@@ -65,9 +82,7 @@ async def interaction_check(self, interaction: Interaction) -> bool:
6582
async def on_timeout(self) -> None:
6683
"""Remove the view from ``self.message`` if set."""
6784
if self.message:
68-
with contextlib.suppress(NotFound):
69-
# Cover the case where this message has already been deleted by external means
70-
await self.message.edit(view=None)
85+
await _handle_modify_message(self.message, "edit")
7186

7287

7388
class DeleteMessageButton(ui.Button):
@@ -95,4 +110,4 @@ def __init__(
95110

96111
async def callback(self, interaction: Interaction) -> None:
97112
"""Delete the original message on button click."""
98-
await interaction.message.delete()
113+
await _handle_modify_message(interaction.message, "delete")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydis_core"
3-
version = "9.1.0"
3+
version = "9.1.1"
44
description = "PyDis core provides core functionality and utility to the bots of the Python Discord community."
55
authors = ["Python Discord <info@pythondiscord.com>"]
66
license = "MIT"

0 commit comments

Comments
 (0)