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 2022-03-24 19:32:56 -07:00 committed by GitHub
parent 2d1cbacc58
commit f26d3a7155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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: