aboutsummaryrefslogtreecommitdiff
path: root/discord/ui
diff options
context:
space:
mode:
authorRapptz <[email protected]>2021-05-27 23:49:01 -0400
committerRapptz <[email protected]>2021-05-28 00:53:28 -0400
commit3453992ce687fa8eb20457c00144546b58de806e (patch)
tree07a3fc26872770b1b8c28cfe491ff7909247f4ba /discord/ui
parentAdd Role.is_assignable() (diff)
downloaddiscord.py-3453992ce687fa8eb20457c00144546b58de806e.tar.xz
discord.py-3453992ce687fa8eb20457c00144546b58de806e.zip
Add View.interaction_check for interaction pre-conditions
Diffstat (limited to 'discord/ui')
-rw-r--r--discord/ui/view.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/discord/ui/view.py b/discord/ui/view.py
index 547d8280..773d1899 100644
--- a/discord/ui/view.py
+++ b/discord/ui/view.py
@@ -179,7 +179,43 @@ class View:
item._view = self
self.children.append(item)
+ async def interaction_check(self, interaction: Interaction) -> bool:
+ """|coro|
+
+ A callback that is called when an interaction happens within the view
+ that checks whether the view should process item callbacks for the interaction.
+
+ This is useful to override if for example you want to ensure that the
+ interaction author is a given user.
+
+ The default implementation of this returns ``True``.
+
+ .. note::
+
+ If an exception occurs within the body then the interaction
+ check is considered failed.
+
+ Parameters
+ -----------
+ interaction: :class:`~discord.Interaction`
+ The interaction that occurred.
+
+ Returns
+ ---------
+ :class:`bool`
+ Whether the view children's callbacks should be called.
+ """
+ return True
+
async def _scheduled_task(self, state: Any, item: Item, interaction: Interaction):
+ try:
+ allow = await self.interaction_check(interaction)
+ except Exception:
+ allow = False
+
+ if not allow:
+ return
+
await item.callback(interaction)
if not interaction.response._responded:
await interaction.response.defer()