Add interaction check to command tree

In some cases, it's desirable for our command tree to only process a
subset of incoming interactions, such as in a multi process deployment.
This commit is contained in:
James Gayfer authored and GitHub committed 2022-03-24 22:32:56 -04:00
1 parent 2d1cbacc58
commit f26d3a7155
1 file changed
+14
+14
View File
@@ -971,6 +971,17 @@ class CommandTree(Generic[ClientT]):
await ctx_menu.on_error(interaction, e)
await self.on_error(interaction, ctx_menu, e)
async def interaction_check(self, interaction: Interaction, /) -> bool:
"""|coro|
A global check to determine if an :class:`~discord.Interaction` should
be processed by the tree.
The default implementation returns True (all interactions are processed),
but can be overridden if custom behaviour is desired.
"""
return True
async def call(self, interaction: Interaction) -> None:
"""|coro|
@@ -994,6 +1005,9 @@ class CommandTree(Generic[ClientT]):
AppCommandError
An error occurred while calling the command.
"""
if not await self.interaction_check(interaction):
return
data: ApplicationCommandInteractionData = interaction.data # type: ignore
type = data.get('type', 1)
if type != 1: