From 29081905b20b72626fe5ab4f2317576bc1e212f8 Mon Sep 17 00:00:00 2001 From: Michael Oliveira <34169552+Flame442@users.noreply.github.com> Date: Thu, 28 Dec 2023 19:37:43 -0500 Subject: [PATCH 1/2] Add methods for getting app command IDs/mentions from cache --- redbot/core/bot.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/redbot/core/bot.py b/redbot/core/bot.py index bb77723b3c7..8b6fce5fab6 100644 --- a/redbot/core/bot.py +++ b/redbot/core/bot.py @@ -1787,6 +1787,52 @@ async def list_enabled_app_commands(self) -> Dict[str, Dict[str, Optional[int]]] "user": curr_user_commands, } + async def get_app_command_id( + self, + command_name: str, + command_type: discord.AppCommandType = discord.AppCommandType.chat_input, + ) -> Optional[int]: + """ + Get the cached ID for a particular app command. + + Pulls from Reds internal cache of app command IDs, which is updated when this instance ``[p]slash sync`` or ``bot.tree.sync()``s. + Does not keep track of guild specific app commands. + Returns ``None`` if the command does not exist or if Red does not know the ID for that app command. + """ + if command_type is discord.AppCommandType.chat_input: + cfg = self._config.enabled_slash_commands() + elif command_type is discord.AppCommandType.message: + cfg = self._config.enabled_message_commands() + elif command_type is discord.AppCommandType.user: + cfg = self._config.enabled_user_commands() + else: + raise TypeError("command type must be one of chat_input, message, user") + + curr_commands = await cfg + return curr_commands.get(command_name, None) + + async def get_app_command_mention( + self, + command_name: str, + command_type: discord.AppCommandType = discord.AppCommandType.chat_input, + ) -> Optional[str]: + """ + Get the string that allows you to mention a particular app command. + + Pulls from Reds internal cache of app command IDs, which is updated when this instance ``[p]slash sync`` or ``bot.tree.sync()``s. + Does not keep track of guild specific app commands. + Returns ``None`` if the command does not exist or if Red does not know the ID for that app command. + """ + # Empty string names will break later code and can't exist as commands, exit early + if not command_name: + raise ValueError("command name must be a non-empty string") + # Account for mentioning subcommands by fetching from the cache based on the base command + base_command = command_name.split(" ")[0] + command_id = await self.get_app_command_id(base_command, command_type) + if command_id is None: + return None + return f"" + async def is_automod_immune( self, to_check: Union[discord.Message, commands.Context, discord.abc.User, discord.Role] ) -> bool: From e43332aded8d2327d6fbcbac98b0645db00086e1 Mon Sep 17 00:00:00 2001 From: Michael Oliveira <34169552+Flame442@users.noreply.github.com> Date: Thu, 28 Dec 2023 19:46:07 -0500 Subject: [PATCH 2/2] Fix docs formatting --- redbot/core/bot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redbot/core/bot.py b/redbot/core/bot.py index 8b6fce5fab6..6337d824c91 100644 --- a/redbot/core/bot.py +++ b/redbot/core/bot.py @@ -1795,7 +1795,7 @@ async def get_app_command_id( """ Get the cached ID for a particular app command. - Pulls from Reds internal cache of app command IDs, which is updated when this instance ``[p]slash sync`` or ``bot.tree.sync()``s. + Pulls from Reds internal cache of app command IDs, which is updated when this instance runs ``[p]slash sync`` or ``bot.tree.sync()``. Does not keep track of guild specific app commands. Returns ``None`` if the command does not exist or if Red does not know the ID for that app command. """ @@ -1819,7 +1819,7 @@ async def get_app_command_mention( """ Get the string that allows you to mention a particular app command. - Pulls from Reds internal cache of app command IDs, which is updated when this instance ``[p]slash sync`` or ``bot.tree.sync()``s. + Pulls from Reds internal cache of app command IDs, which is updated when this instance runs ``[p]slash sync`` or ``bot.tree.sync()``. Does not keep track of guild specific app commands. Returns ``None`` if the command does not exist or if Red does not know the ID for that app command. """