Skip to content
This repository was archived by the owner on Aug 28, 2019. It is now read-only.

Commit f780b0a

Browse files
committed
Add setting to fallback to global command if guild command isn't found
1 parent 6e2fcd4 commit f780b0a

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

discord/app_commands/tree.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,16 @@ class CommandTree(Generic[ClientT]):
113113
-----------
114114
client: :class:`~discord.Client`
115115
The client instance to get application command information from.
116+
fallback_to_global: :class:`bool`
117+
If a guild-specific command is not found when invoked, then try falling back into
118+
a global command in the tree. For example, if the tree locally has a ``/ping`` command
119+
under the global namespace but the guild has a guild-specific ``/ping``, instead of failing
120+
to find the guild-specific ``/ping`` command it will fall back to the global ``/ping`` command.
121+
This has the potential to raise more :exc:`~discord.app_commands.CommandSignatureMismatch` errors
122+
than usual. Defaults to ``True``.
116123
"""
117124

118-
def __init__(self, client: ClientT):
125+
def __init__(self, client: ClientT, *, fallback_to_global: bool = True):
119126
self.client: ClientT = client
120127
self._http = client.http
121128
self._state = client._connection
@@ -124,6 +131,7 @@ def __init__(self, client: ClientT):
124131
raise ClientException('This client already has an associated command tree.')
125132

126133
self._state._command_tree = self
134+
self.fallback_to_global: bool = fallback_to_global
127135
self._guild_commands: Dict[int, Dict[str, Union[Command, Group]]] = {}
128136
self._global_commands: Dict[str, Union[Command, Group]] = {}
129137
# (name, guild_id, command_type): Command
@@ -935,7 +943,11 @@ async def wrapper():
935943
def _get_context_menu(self, data: ApplicationCommandInteractionData) -> Optional[ContextMenu]:
936944
name = data['name']
937945
guild_id = _get_as_snowflake(data, 'guild_id')
938-
return self._context_menus.get((name, guild_id, data.get('type', 1)))
946+
t = data.get('type', 1)
947+
cmd = self._context_menus.get((name, guild_id, t))
948+
if cmd is None and self.fallback_to_global:
949+
return self._context_menus.get((name, None, t))
950+
return cmd
939951

940952
def _get_app_command_options(
941953
self, data: ApplicationCommandInteractionData
@@ -948,9 +960,11 @@ def _get_app_command_options(
948960
try:
949961
guild_commands = self._guild_commands[command_guild_id]
950962
except KeyError:
951-
command = None
963+
command = None if not self.fallback_to_global else self._global_commands.get(name)
952964
else:
953965
command = guild_commands.get(name)
966+
if command is None and self.fallback_to_global:
967+
command = self._global_commands.get(name)
954968
else:
955969
command = self._global_commands.get(name)
956970

0 commit comments

Comments
 (0)