Application Commands¶
Command Permission Decorators¶
- @discord.commands.default_permissions(**perms)[source]¶
A decorator that limits the usage of an application command to members with certain permissions.
The permissions passed in must be exactly like the properties shown under
discord.Permissions.Note
These permissions can be updated by server administrators per-guild. As such, these are only “defaults”, as the name suggests. If you want to make sure that a user always has the specified permissions regardless, you should use an internal check such as
has_permissions().Example
from discord import default_permissions @bot.slash_command() @default_permissions(manage_messages=True) async def test(ctx): await ctx.respond("You can manage messages.")
- @discord.commands.guild_only[source]¶
A decorator that limits the usage of an application command to guild contexts. The command won’t be able to be used in private message channels.
- Return type:
Example
from discord import guild_only @bot.slash_command() @guild_only() async def test(ctx): await ctx.respond("You're in a guild.")
- @discord.commands.is_nsfw[source]¶
A decorator that limits the usage of an application command to 18+ channels and users. In guilds, the command will only be able to be used in channels marked as NSFW. In DMs, users must have opted into age-restricted commands via privacy settings.
Note that apps intending to be listed in the App Directory cannot have NSFW commands.
- Return type:
Example
from discord import is_nsfw @bot.slash_command() @is_nsfw() async def test(ctx): await ctx.respond("This command is age restricted.")
Commands¶
Shortcut Decorators¶
- @discord.commands.application_command(cls=<class 'discord.commands.core.SlashCommand'>, **attrs)[source]¶
A decorator that transforms a function into an
ApplicationCommand. More specifically, usually one ofSlashCommand,UserCommand, orMessageCommand. The exact class depends on theclsparameter. By default, thedescriptionattribute is received automatically from the docstring of the function and is cleaned up with the use ofinspect.cleandoc. If the docstring isbytes, then it is decoded intostrusing utf-8 encoding. Thenameattribute also defaults to the function name unchanged.Added in version 2.0.
- Parameters:
cls (
ApplicationCommand) – The class to construct with. By default, this isSlashCommand. You usually do not change this.attrs – Keyword arguments to pass into the construction of the class denoted by
cls.
- Returns:
A decorator that converts the provided method into an
ApplicationCommand, or subclass of it.- Return type:
Callable[…,
ApplicationCommand]- Raises:
TypeError – If the function is not a coroutine or is already a command.
- @discord.commands.command(**kwargs)[source]¶
An alias for
application_command().Note
This decorator is overridden by
ext.commands.command().Added in version 2.0.
- Returns:
A decorator that converts the provided method into an
ApplicationCommand.- Return type:
Callable[…,
ApplicationCommand]
- @discord.commands.slash_command(**kwargs)[source]¶
Decorator for slash commands that invokes
application_command().Added in version 2.0.
- Returns:
A decorator that converts the provided method into a
SlashCommand.- Return type:
Callable[…,
SlashCommand]
- @discord.commands.user_command(**kwargs)[source]¶
Decorator for user commands that invokes
application_command().Added in version 2.0.
- Returns:
A decorator that converts the provided method into a
UserCommand.- Return type:
Callable[…,
UserCommand]
- @discord.commands.message_command(**kwargs)[source]¶
Decorator for message commands that invokes
application_command().Added in version 2.0.
- Returns:
A decorator that converts the provided method into a
MessageCommand.- Return type:
Callable[…,
MessageCommand]
Objects¶
- class discord.ApplicationCommand(func, **kwargs)[source]¶
- Parameters:
func (
Callable)
- is_on_cooldown(ctx)[source]¶
Checks whether the command is currently on cooldown.
Note
This uses the current time instead of the interaction time.
- Parameters:
ctx (
ApplicationContext) – The invocation context to use when checking the command’s cooldown status.- Returns:
A boolean indicating if the command is on cooldown.
- Return type:
- reset_cooldown(ctx)[source]¶
Resets the cooldown on this command.
- Parameters:
ctx (
ApplicationContext) – The invocation context to reset the cooldown under.- Return type:
- get_cooldown_retry_after(ctx)[source]¶
Retrieves the amount of seconds before this command can be tried again.
Note
This uses the current time instead of the interaction time.
- Parameters:
ctx (
ApplicationContext) – The invocation context to retrieve the cooldown from.- Returns:
The amount of time left on this command’s cooldown in seconds. If this is
0.0then the command isn’t on cooldown.- Return type:
- error(coro)[source]¶
A decorator that registers a coroutine as a local error handler.
A local error handler is an
on_command_error()event limited to a single command. However, theon_command_error()is still invoked afterwards as the catch-all.
- has_error_handler()[source]¶
Checks whether the command has an error handler registered.
- Return type:
- before_invoke(coro)[source]¶
A decorator that registers a coroutine as a pre-invoke hook. A pre-invoke hook is called directly before the command is called. This makes it a useful function to set up database connections or any type of set up required.
This pre-invoke hook takes a sole parameter, an
ApplicationContext. SeeBot.before_invoke()for more info.
- after_invoke(coro)[source]¶
A decorator that registers a coroutine as a post-invoke hook. A post-invoke hook is called directly after the command is called. This makes it a useful function to clean-up database connections or any type of clean up required.
This post-invoke hook takes a sole parameter, an
ApplicationContext. SeeBot.after_invoke()for more info.
- property full_parent_name: str¶
Retrieves the fully qualified parent command name.
This the base command name required to execute it. For example, in
/one two threethe parent name would beone two.
- defcopy
- class discord.SlashCommand(func, *args, **kwargs)[source]¶
A class that implements the protocol for a slash command.
These are not created manually, instead they are created via the decorator or functional interface.
Added in version 2.0.
- parent¶
The parent group that this command belongs to.
Noneif there isn’t one.- Type:
Optional[
SlashCommandGroup]
- guild_only¶
Whether the command should only be usable inside a guild.
Deprecated since version 2.6: Use the
contextsparameter instead.- Type:
- nsfw¶
Whether the command should be restricted to 18+ channels and users. Apps intending to be listed in the App Directory cannot have NSFW commands.
- Type:
- default_member_permissions¶
The default permissions a member needs to be able to run the command.
- Type:
- checks¶
A list of predicates that verifies if the command could be executed with the given
ApplicationContextas the sole parameter. If an exception is necessary to be thrown to signal failure, then one inherited fromApplicationCommandErrorshould be used. Note that if the checks fail thenCheckFailureexception is raised to theon_application_command_error()event.- Type:
List[Callable[[
ApplicationContext],bool]]
- cooldown¶
The cooldown applied when the command is invoked.
Noneif the command doesn’t have a cooldown.- Type:
Optional[
Cooldown]
- name_localizations¶
The name localizations for this command. The values of this should be
"locale": "name". See here for a list of valid locales.
- description_localizations¶
The description localizations for this command. The values of this should be
"locale": "description". See here for a list of valid locales.
- integration_types¶
The type of installation this command should be available to. For instance, if set to
IntegrationType.user_install, the command will only be available to users with the application installed on their account. Unapplicable for guild commands.- Type:
Set[
IntegrationType]
- contexts¶
The location where this command can be used. Cannot be set if this is a guild command.
- Type:
- Parameters:
func (
Callable)
- defcopy
- defcreate_subgroup
- @subgroup
- defwalk_commands
- class discord.SlashCommandGroup(name, description=None, guild_ids=None, parent=None, cooldown=None, max_concurrency=None, **kwargs)[source]¶
A class that implements the protocol for a slash command group.
These can be created manually, but they should be created via the decorator or functional interface.
- parent¶
The parent group that this group belongs to.
Noneif there isn’t one.- Type:
Optional[
SlashCommandGroup]
- guild_only¶
Whether the command should only be usable inside a guild.
Deprecated since version 2.6: Use the
contextsparameter instead.- Type:
- nsfw¶
Whether the command should be restricted to 18+ channels and users. Apps intending to be listed in the App Directory cannot have NSFW commands.
- Type:
- default_member_permissions¶
The default permissions a member needs to be able to run the command.
- Type:
- checks¶
A list of predicates that verifies if the command could be executed with the given
ApplicationContextas the sole parameter. If an exception is necessary to be thrown to signal failure, then one inherited fromApplicationCommandErrorshould be used. Note that if the checks fail thenCheckFailureexception is raised to theon_application_command_error()event.- Type:
List[Callable[[
ApplicationContext],bool]]
- name_localizations¶
The name localizations for this command. The values of this should be
"locale": "name". See here for a list of valid locales.
- description_localizations¶
The description localizations for this command. The values of this should be
"locale": "description". See here for a list of valid locales.
- integration_types¶
The type of installation this command should be available to. For instance, if set to
IntegrationType.user_install, the command will only be available to users with the application installed on their account. Unapplicable for guild commands.- Type:
Set[
IntegrationType]
- contexts¶
The location where this command can be used. Unapplicable for guild commands.
- Type:
- Parameters:
- create_subgroup(name, description=None, guild_ids=None, **kwargs)[source]¶
Creates a new subgroup for this SlashCommandGroup.
- Parameters:
name (
str) – 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. This will be a global command ifNoneis passed.guild_only (
bool) – Whether the command should only be usable inside a guild.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.checks (List[Callable[[
ApplicationContext],bool]]) – A list of predicates that verifies if the command could be executed with the givenApplicationContextas the sole parameter. If an exception is necessary to be thrown to signal failure, then one inherited fromApplicationCommandErrorshould be used. Note that if the checks fail thenCheckFailureexception is raised to theon_application_command_error()event.name_localizations (Dict[
str,str]) –The name localizations for this command. The values of this should be
"locale": "name". See here for a list of valid locales.description_localizations (Dict[
str,str]) –The description localizations for this command. The values of this should be
"locale": "description". See here for a list of valid locales.
- Returns:
The slash command group that was created.
- Return type:
- subgroup(name=None, description=None, guild_ids=None)[source]¶
A shortcut decorator that initializes the provided subclass of
SlashCommandGroupas a subgroup.Added in version 2.0.
- Parameters:
name (Optional[
str]) – The name of the group to create. This will resolve to the name of the decorated class ifNoneis 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, making it a guild command. This will be a global command ifNoneis passed.
- Returns:
The slash command group that was created.
- Return type:
Callable[[Type[SlashCommandGroup]], SlashCommandGroup]
- for ... in walk_commands()[source]¶
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 from the group.- Return type:
- defcopy
- class discord.UserCommand(func, *args, **kwargs)[source]¶
A class that implements the protocol for user context menu commands.
These are not created manually, instead they are created via the decorator or functional interface.
- guild_only¶
Whether the command should only be usable inside a guild.
Deprecated since version 2.6: Use the
contextsparameter instead.- Type:
- nsfw¶
Whether the command should be restricted to 18+ channels and users. Apps intending to be listed in the App Directory cannot have NSFW commands.
- Type:
- default_member_permissions¶
The default permissions a member needs to be able to run the command.
- Type:
- checks¶
A list of predicates that verifies if the command could be executed with the given
ApplicationContextas the sole parameter. If an exception is necessary to be thrown to signal failure, then one inherited fromApplicationCommandErrorshould be used. Note that if the checks fail thenCheckFailureexception is raised to theon_application_command_error()event.- Type:
List[Callable[[
ApplicationContext],bool]]
- cooldown¶
The cooldown applied when the command is invoked.
Noneif the command doesn’t have a cooldown.- Type:
Optional[
Cooldown]
- name_localizations¶
The name localizations for this command. The values of this should be
"locale": "name". See here for a list of valid locales.
- integration_types¶
The installation contexts where this command is available. Unapplicable for guild commands.
- Type:
Set[
IntegrationType]
- contexts¶
The interaction contexts where this command is available. Unapplicable for guild commands.
- Type:
- Parameters:
func (
Callable)
- defcopy
- class discord.MessageCommand(func, *args, **kwargs)[source]¶
A class that implements the protocol for message context menu commands.
These are not created manually, instead they are created via the decorator or functional interface.
- guild_only¶
Whether the command should only be usable inside a guild.
Deprecated since version 2.6: Use the
contextsparameter instead.- Type:
- nsfw¶
Whether the command should be restricted to 18+ channels and users. Apps intending to be listed in the App Directory cannot have NSFW commands.
- Type:
- default_member_permissions¶
The default permissions a member needs to be able to run the command.
- Type:
- checks¶
A list of predicates that verifies if the command could be executed with the given
ApplicationContextas the sole parameter. If an exception is necessary to be thrown to signal failure, then one inherited fromApplicationCommandErrorshould be used. Note that if the checks fail thenCheckFailureexception is raised to theon_application_command_error()event.- Type:
List[Callable[[
ApplicationContext],bool]]
- cooldown¶
The cooldown applied when the command is invoked.
Noneif the command doesn’t have a cooldown.- Type:
Optional[
Cooldown]
- name_localizations¶
The name localizations for this command. The values of this should be
"locale": "name". See here for a list of valid locales.
- integration_types¶
The installation contexts where this command is available. Unapplicable for guild commands.
- Type:
Set[
IntegrationType]
- contexts¶
The interaction contexts where this command is available. Unapplicable for guild commands.
- Type:
- Parameters:
func (
Callable)
Options¶
Objects¶
- class discord.Option(name: str, input_type: type[T] = str, *, choices: Sequence[OptionChoice[T]], description: str | None = None, channel_types: None = None, required: bool = ..., default: Any | Undefined = ..., min_value: None = None, max_value: None = None, min_length: None = None, max_length: None = None, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None, autocomplete: None = None)[source]¶
- class discord.Option(name: str, input_type: type[GuildChannel | Thread] | Literal[SlashCommandOptionType.channel] = SlashCommandOptionType.channel, *, choices: None = None, description: str | None = None, channel_types: Sequence[ChannelType] | None = None, required: bool = ..., default: Any | Undefined = ..., min_value: None = None, max_value: None = None, min_length: None = None, max_length: None = None, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None, autocomplete: None = None)
- class discord.Option(name: str, input_type: type[str] | Literal[SlashCommandOptionType.string] = str, *, description: str | None = None, choices: None = None, channel_types: None = None, required: Literal[True], default: Undefined = MISSING, min_length: int | None = None, max_length: int | None = None, min_value: None = None, max_value: None = None, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None, autocomplete: None = None)
- class discord.Option(name: str, input_type: type[str] | Literal[SlashCommandOptionType.string] = str, *, description: str | None = None, choices: None = None, channel_types: None = None, required: bool = False, default: Any, min_length: int | None = None, max_length: int | None = None, min_value: None = None, max_value: None = None, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None, autocomplete: None = None)
- class discord.Option(name: str, input_type: type[int] | Literal[SlashCommandOptionType.integer], *, description: str | None = None, choices: None = None, channel_types: None = None, required: Literal[True], default: Undefined = MISSING, min_value: int | None = None, max_value: int | None = None, min_length: None = None, max_length: None = None, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None, autocomplete: None = None)
- class discord.Option(name: str, input_type: type[int] | Literal[SlashCommandOptionType.integer], *, description: str | None = None, choices: None = None, channel_types: None = None, required: bool = False, default: Any, min_value: int | None = None, max_value: int | None = None, min_length: None = None, max_length: None = None, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None, autocomplete: None = None)
- class discord.Option(name: str, input_type: type[float] | Literal[SlashCommandOptionType.number], *, description: str | None = None, choices: None = None, channel_types: None = None, required: Literal[True], default: Undefined = MISSING, min_value: int | float | None = None, max_value: int | float | None = None, min_length: None = None, max_length: None = None, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None, autocomplete: None = None)
- class discord.Option(name: str, input_type: type[float] | Literal[SlashCommandOptionType.number], *, description: str | None = None, choices: None = None, channel_types: None = None, required: bool = False, default: Any, min_value: int | float | None = None, max_value: int | float | None = None, min_length: None = None, max_length: None = None, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None, autocomplete: None = None)
- class discord.Option(name: str, input_type: type[str | int | float] = str, *, description: str | None = None, choices: None = None, channel_types: None = None, required: Literal[True], default: Undefined = MISSING, min_value: None = None, max_value: None = None, min_length: None = None, max_length: None = None, autocomplete: ApplicationCommandOptionAutocomplete, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None)
- class discord.Option(name: str, input_type: type[str | int | float] = str, *, description: str | None = None, choices: None = None, channel_types: None = None, required: bool = False, default: Any, min_value: None = None, max_value: None = None, min_length: None = None, max_length: None = None, autocomplete: ApplicationCommandOptionAutocomplete, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None)
- class discord.Option(name: str, input_type: type[T] = str, *, description: str | None = None, choices: None = None, channel_types: None = None, required: Literal[True], default: Undefined = MISSING, min_value: None = None, max_value: None = None, min_length: None = None, max_length: None = None, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None, autocomplete: None = None)
- class discord.Option(name: str, input_type: type[T] = str, *, description: str | None = None, choices: None = None, channel_types: None = None, required: bool = False, default: Any, min_value: None = None, max_value: None = None, min_length: None = None, max_length: None = None, name_localizations: dict[str, str] | None = None, description_localizations: dict[str, str] | None = None, autocomplete: None = None)
Represents a selectable option for a slash command.
- input_type¶
The type of input that is expected for this option. This can be a
SlashCommandOptionType, an associated class, a channel type, aConverter, a converter class or anenum.Enum. If aenum.Enumis used and it has up to 25 values,choiceswill be automatically filled. If theenum.Enumhas more than 25 values,autocompletewill be implemented withdiscord.utils.basic_autocomplete()instead.- Type:
Union[Type[
str], Type[bool], Type[int], Type[float], Type[abc.GuildChannel], Type[Thread], Type[Member], Type[User], Type[Attachment], Type[Role], Type[abc.Mentionable],SlashCommandOptionType, Type[ext.commands.Converter], Type[enums.Enum], Type[Enum]]
- name¶
The name of this option visible in the UI. Inherits from the variable name if not provided as a parameter.
- Type:
- description¶
The description of this option. Must be 100 characters or fewer. If
input_typeis aenum.Enumanddescriptionis not specified,input_type’s docstring will be used.- Type:
Optional[
str]
- choices¶
The list of available choices for this option. Can be a list of values or
OptionChoiceobjects (which represent a name:value pair). If provided, the input from the user must match one of the choices in the list.- Type:
Optional[List[Union[
Any,OptionChoice]]]
- default¶
The default value for this option. If provided,
requiredwill be consideredFalse.- Type:
Optional[
Any]
- min_value¶
The minimum value that can be entered. Only applies to Options with an
input_typeofintorfloat.- Type:
Optional[
int]
- max_value¶
The maximum value that can be entered. Only applies to Options with an
input_typeofintorfloat.- Type:
Optional[
int]
- min_length¶
The minimum length of the string that can be entered. Must be between 0 and 6000 (inclusive). Only applies to Options with an
input_typeofstr.- Type:
Optional[
int]
- max_length¶
The maximum length of the string that can be entered. Must be between 1 and 6000 (inclusive). Only applies to Options with an
input_typeofstr.- Type:
Optional[
int]
- channel_types¶
A list of channel types that can be selected in this option. Only applies to Options with an
input_typeofdiscord.SlashCommandOptionType.channel. If this argument is used,input_typewill be ignored.- Type:
list[
discord.ChannelType] | None
- name_localizations¶
The name localizations for this option. The values of this should be
"locale": "name". See here for a list of valid locales.
- description_localizations¶
The description localizations for this option. The values of this should be
"locale": "description". See here for a list of valid locales.
Examples
Basic usage:
@bot.slash_command(guild_ids=[...]) async def hello( ctx: discord.ApplicationContext, name: Option(str, "Enter your name"), age: Option(int, "Enter your age", min_value=1, max_value=99, default=18), # passing the default value makes an argument optional # you also can create optional argument using: # age: Option(int, "Enter your age") = 18 ): await ctx.respond(f"Hello! Your name is {name} and you are {age} years old.")
Added in version 2.0.
- Parameters:
name (
str)input_type (
type[str|bool|int|float|GuildChannel|Thread|Member|User|Attachment|Role|Mentionable] |SlashCommandOptionType|type[TypeVar(T, bound= str | int | float)])choices (
Optional[Sequence[OptionChoice[TypeVar(T, bound= str | int | float)]]])required (
bool)default (
Any|Undefined)autocomplete (
ApplicationCommandOptionAutocomplete|None)
- class discord.ThreadOption(thread_type)[source]¶
Represents a class that can be passed as the
input_typefor anOptionclass.Added in version 2.0.
- Parameters:
thread_type (Literal["public", "private", "news"]) – The thread type to expect for this options input.
- class discord.OptionChoice(name, value=None, name_localizations=None)[source]¶
Represents a name:value pairing for a selected
Option.Added in version 2.0.
- value¶
The value of the choice. If not provided, will use the value of
name.