diff options
-rw-r--r-- | botcore/utils/interactions.py | 16 | ||||
-rw-r--r-- | docs/changelog.rst | 4 | ||||
-rw-r--r-- | pyproject.toml | 2 |
3 files changed, 19 insertions, 3 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): """ diff --git a/docs/changelog.rst b/docs/changelog.rst index 493b34e2..e35e75d0 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,10 @@ Changelog ========= +- :release:`7.4.0 <17th July 2022>` +- :feature:`106` Add an optional ``message`` attr to :obj:`botcore.utils.interactions.ViewWithUserAndRoleCheck`. On view timeout, this message has its view removed if set. + + - :release:`7.3.1 <16th July 2022>` - :bug:`104` Fix :obj:`botcore.utils.interactions.DeleteMessageButton` not working due to using wrong delete method. diff --git a/pyproject.toml b/pyproject.toml index ab406c86..6c91dde3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "bot-core" -version = "7.3.1" +version = "7.4.0" description = "Bot-Core provides the core functionality and utilities for the bots of the Python Discord community." authors = ["Python Discord <[email protected]>"] license = "MIT" |