Skip to main content
Slash commands are application commands that appear in Discord’s command picker when users type / in a chat.

SlashCommand

A class that implements the protocol for a slash command.

Attributes

name
str
The name of the command.
callback
coroutine
The coroutine that is executed when the command is called.
description
Optional[str]
The description for the command.
guild_ids
Optional[List[int]]
The ids of the guilds where this command will be registered.
options
List[Option]
The parameters for this command.
parent
Optional[SlashCommandGroup]
The parent group that this command belongs to. None if there isn’t one.
nsfw
bool
Whether the command should be restricted to 18+ channels and users. Apps intending to be listed in the App Directory cannot have NSFW commands.
default_member_permissions
Permissions
The default permissions a member needs to be able to run the command.
cog
Optional[Cog]
The cog that this command belongs to. None if there isn’t one.
checks
List[Callable]
A list of predicates that verifies if the command could be executed with the given ApplicationContext as the sole parameter.
cooldown
Optional[Cooldown]
The cooldown applied when the command is invoked. None if the command doesn’t have a cooldown.
name_localizations
Dict[str, str]
The name localizations for this command. The values of this should be "locale": "name". See Discord’s locales documentation for valid locales.
description_localizations
Dict[str, str]
The description localizations for this command. The values should be "locale": "description".
integration_types
Set[IntegrationType]
The type of installation this command should be available to. Unapplicable for guild commands.
contexts
Set[InteractionContextType]
The location where this command can be used. Cannot be set if this is a guild command.

Properties

mention
str
Returns a string that allows you to mention the slash command.
is_subcommand
bool
Returns True if this command has a parent.
qualified_name
str
Retrieves the fully qualified command name. For example, in /one two three the qualified name would be one two three.
qualified_id
int
Retrieves the fully qualified command ID. This is the root parent ID.

Methods

is_on_cooldown()

Checks whether the command is currently on cooldown.
ctx
ApplicationContext
required
The invocation context to use when checking the command’s cooldown status.
Returns: bool - A boolean indicating if the command is on cooldown.

reset_cooldown()

Resets the cooldown on this command.
ctx
ApplicationContext
required
The invocation context to reset the cooldown under.

get_cooldown_retry_after()

Retrieves the amount of seconds before this command can be tried again.
ctx
ApplicationContext
required
The invocation context to retrieve the cooldown from.
Returns: float - The amount of time left on this command’s cooldown in seconds. If this is 0.0 then the command isn’t on cooldown.

copy()

Creates a copy of this command. Returns: SlashCommand - A new instance of this command.

error()

A decorator that registers a coroutine as a local error handler.
coro
coroutine
required
The coroutine to register as the local error handler.

before_invoke()

A decorator that registers a coroutine as a pre-invoke hook. A pre-invoke hook is called directly before the command is called.
coro
coroutine
required
The coroutine to register as the pre-invoke hook.

after_invoke()

A decorator that registers a coroutine as a post-invoke hook. A post-invoke hook is called directly after the command is called.
coro
coroutine
required
The coroutine to register as the post-invoke hook.

SlashCommandGroup

A class that implements the protocol for a slash command group. Command groups allow you to organize related commands together.

Attributes

name
str
The name of the command group.
description
Optional[str]
The description for the command group.
guild_ids
Optional[List[int]]
The ids of the guilds where this command will be registered.
parent
Optional[SlashCommandGroup]
The parent group that this group belongs to. None if there isn’t one.
nsfw
bool
Whether the command should be restricted to 18+ channels and users.
default_member_permissions
Permissions
The default permissions a member needs to be able to run the command.
checks
List[Callable]
A list of predicates that verifies if the command could be executed.
name_localizations
Dict[str, str]
The name localizations for this command. Values should be "locale": "name".
description_localizations
Dict[str, str]
The description localizations for this command.
integration_types
Set[IntegrationType]
The type of installation this command should be available to. Unapplicable for guild commands.
contexts
Set[InteractionContextType]
The location where this command can be used. Unapplicable for guild commands.

Methods

command()

A shortcut decorator for adding a subcommand to this slash command group.
**kwargs
dict
Keyword arguments to pass to the SlashCommand constructor.
Returns: Callable[..., SlashCommand] - A decorator that converts the provided function into a SlashCommand.

create_subgroup()

Creates a new subgroup under this slash command group.
name
str
required
The name of the group to create.
description
Optional[str]
The description of the group to create.
guild_ids
Optional[List[int]]
A list of the IDs of each guild this group should be added to, making it a guild command.
Returns: SlashCommandGroup - The slash command group that was created.

subgroup()

A shortcut decorator that initializes the provided subclass of SlashCommandGroup as a subgroup.
name
Optional[str]
The name of the group to create. This will resolve to the name of the decorated class if None is passed.
description
Optional[str]
The description of the group to create.
guild_ids
Optional[List[int]]
A list of the IDs of each guild this group should be added to.
Returns: Callable[[Type[SlashCommandGroup]], SlashCommandGroup]

walk_commands()

An iterator that recursively walks through all slash commands and groups in this group. Yields: SlashCommand | SlashCommandGroup - A nested slash command or slash command group.

slash_command()

Decorator for creating slash commands.
name
str
The name of the command. Defaults to the function name.
description
str
The description of the command. Defaults to the function’s docstring.
guild_ids
Optional[List[int]]
A list of guild IDs where this command will be registered. If not provided, the command is global.
options
List[Option]
The list of options for this command.
default_member_permissions
Optional[Permissions]
The default permissions required to use this command.
nsfw
bool
default:"False"
Whether the command is age-restricted.
contexts
Optional[Set[InteractionContextType]]
The contexts where this command can be used.
integration_types
Optional[Set[IntegrationType]]
The installation types this command is available for.
Returns: Callable[..., SlashCommand] - A decorator that converts the provided method into a SlashCommand.

Examples

import discord

bot = discord.Bot()

@bot.slash_command(name="hello", description="Say hello!")
async def hello(ctx: discord.ApplicationContext):
    await ctx.respond("Hello!")

bot.run("token")

Build docs developers (and LLMs) love