diff options
author | 2022-07-17 12:52:30 +0100 | |
---|---|---|
committer | 2022-07-17 13:08:05 +0100 | |
commit | 77f21bfe43e1b36108b23bdf1da09463e9e42f4d (patch) | |
tree | 18e1070858f71d58d823db90ae2084712a6cf42d /botcore/utils/interactions.py | |
parent | Merge pull request #104 from python-discord/fix-delete-button (diff) |
Add an optional message attr to ViewWithUserAndRoleCheckv7.4.0-beta1
On view timeout, this message has it's view removed if set.
Diffstat (limited to 'botcore/utils/interactions.py')
-rw-r--r-- | botcore/utils/interactions.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/botcore/utils/interactions.py b/botcore/utils/interactions.py index 60b8c1f7..26bd92f2 100644 --- a/botcore/utils/interactions.py +++ b/botcore/utils/interactions.py @@ -1,6 +1,7 @@ +import contextlib from typing import Optional, Sequence -from discord import ButtonStyle, Interaction, ui +from discord import ButtonStyle, Interaction, Message, NotFound, ui from botcore.utils.logging import get_logger @@ -16,6 +17,8 @@ class ViewWithUserAndRoleCheck(ui.View): allowed_roles: A sequence of role ids that are allowed to interact with the view. timeout: Timeout in seconds from last interaction with the UI before no longer accepting input. If ``None`` then there is no timeout. + message: The message to remove the view from on timeout. This can also be set with + ``view.message = await ctx.send( ... )``` , or similar, after the view is instantiated. """ def __init__( @@ -23,11 +26,13 @@ class ViewWithUserAndRoleCheck(ui.View): *, allowed_users: Sequence[int], allowed_roles: Sequence[int], - timeout: Optional[float] = 180.0 + timeout: Optional[float] = 180.0, + message: Optional[Message] = None ) -> None: super().__init__(timeout=timeout) self.allowed_users = allowed_users self.allowed_roles = allowed_roles + self.message = message async def interaction_check(self, interaction: Interaction) -> bool: """ @@ -57,6 +62,13 @@ class ViewWithUserAndRoleCheck(ui.View): await interaction.response.send_message("This is not your button to click!", ephemeral=True) return False + 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) + class DeleteMessageButton(ui.Button): """ |