Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods for getting app command IDs/mentions from cache #6278

Open
wants to merge 2 commits into
base: V3/develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions redbot/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.
"""
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 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.
"""
# 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"</{command_name}:{command_id}>"

async def is_automod_immune(
self, to_check: Union[discord.Message, commands.Context, discord.abc.User, discord.Role]
) -> bool:
Expand Down
Loading