From 77f21bfe43e1b36108b23bdf1da09463e9e42f4d Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Sun, 17 Jul 2022 12:52:30 +0100 Subject: Add an optional message attr to ViewWithUserAndRoleCheck On view timeout, this message has it's view removed if set. --- botcore/utils/interactions.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'botcore/utils') 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): """ -- cgit v1.2.3