Skip to content

Getting Commands

Retrieving existing commands require an already initialized Dispike object.

You can specify whether to return commands globally or per guild.

Getting commands is similar to Editing Commands except for new_command, command_id, parameters are not available.

from dispike import Dispike

bot = Dispike(...)

commands = bot.get_commands()

>>> [IncomingApplicationCommand(...), IncomingApplicationCommand(...)]

API Reference

Returns a list of DiscordCommands either globally or for a specific guild.

Parameters:

Name Type Description Default
guild_only bool

whether to target a guild. Defaults to False.

False
guild_id_passed [type]

guild id if guild_only is set to True. Defaults to None.

None

Returns:

Type Description
List[dispike.incoming.incoming_interactions.IncomingApplicationCommand]

typing.List[DiscordCommand]: Array of DiscordCommand

Exceptions:

Type Description
DiscordAPIError

any Discord returned errors.

Source code in dispike/main.py
@logger.catch(reraise=True, message="Issue with getting commands from Discord")
def get_commands(
    self, guild_only=False, guild_id_passed=None
) -> typing.List[IncomingApplicationCommand]:
    """Returns a list of ``DiscordCommands`` either globally or for a specific guild.

    Args:
        guild_only (bool, optional): whether to target a guild. Defaults to False.
        guild_id_passed ([type], optional): guild id if guild_only is set to True. Defaults to None.

    Returns:
        typing.List[DiscordCommand]: Array of DiscordCommand

    Raises:
        DiscordAPIError: any Discord returned errors.
    """
    if guild_only:
        if not guild_id_passed or not isinstance(guild_id_passed, str):
            raise TypeError(
                "You cannot have guild_only == True and NOT pass any guild id."
            )
        _url = f"/guilds/{guild_id_passed}/commands"
    else:
        _url = f"/commands"

    try:
        _send_request = self._registrator._client.get(
            _url, headers=self._registrator.request_headers
        )
        if _send_request.status_code == 200:
            return [IncomingApplicationCommand(**x) for x in _send_request.json()]

        raise DiscordAPIError(_send_request.status_code, _send_request.text)
    except DiscordAPIError:
        logger.exception("Discord API Failure.")
        raise