Commands¶
discord_http.commands module¶
- class discord_http.commands.Choice(key, value)¶
Bases:
Generic[ChoiceT]Makes it possible to access both the name and value of a choice.
Defaults to a string type
Paramaters¶
- key:
The key of the choice from your dict.
- value:
The value of your choice (the one that is shown to public)
- key: ChoiceT¶
The key of the choice (the internal value sent to the command).
- value: ChoiceT¶
The value of the choice (the label shown to the user).
- type: CommandOptionType¶
The option type of the choice, inferred from the key type.
- Parameters:
key (ChoiceT)
value (ChoiceT)
- class discord_http.commands.Cog(*args, **kwargs)¶
Bases:
objectRepresents a cog, a collection of commands, listeners, and interactions.
- class discord_http.commands.Command(command, name, description=None, guild_ids=None, guild_install=True, user_install=False, cmd_type=ApplicationCommandType.chat_input, parent=None)¶
Bases:
objectRepresents a Discord command.
- Parameters:
- type: int¶
The type of the command, either 1 for chat input, 2 for user context menu, or 3 for message context menu.
- guild_ids: list[Snowflake | int]¶
A list of guild IDs this command is in, empty if it’s a global command.
- cooldown: CooldownCache | None¶
The cooldown rule of the command, if any.
- async run(context, *args, **kwargs) BaseResponse¶
Runs the command.
- Parameters:
context (
Context) – The context of the command.*args – The arguments of the command.
**kwargs – The keyword arguments of the command.
- Return type:
- Returns:
The return type of the command, used by backend.py
- Raises:
UserMissingPermissions – User that ran the command is missing permissions.
BotMissingPermissions – Bot is missing permissions.
- autocomplete(name) Callable[[Callable], Callable]¶
Decorator to set an option as an autocomplete.
The function must at the end, return a Response.send_autocomplete() object.
Example usage
@commands.command() async def ping(ctx, options: str): return ctx.response.send_message(f"You chose {options}") @ping.autocomplete("options") async def search_autocomplete(ctx, current: str): return ctx.response.send_autocomplete({ "key": "Value shown to user", "feeling_lucky_tm": "I'm feeling lucky!" })
- class discord_http.commands.Converter(*args, **kwargs)¶
Bases:
Protocol[ConverterT]The base class of converting strings to whatever you desire.
Instead of needing to implement checks inside the command, you can use this to convert the value on runtime, both in sync and async mode.
- async convert(ctx, value) ConverterT¶
The function where you implement the logic of converting the value into whatever you need to be outputted in command.
- class discord_http.commands.Interaction(func, custom_id, *, regex=False)¶
Bases:
objectRepresents a Discord interaction, usually from a component or modal.
- match(custom_id) bool¶
Matches the custom ID with the interaction.
Will always return False if the interaction is not a regex.
- async run(context) BaseResponse¶
Runs the interaction.
- class discord_http.commands.Listener(*, name, coro)¶
Bases:
objectRepresents a Discord event listener.
- class discord_http.commands.LocaleContainer(key, name, description=None)¶
Bases:
objectRepresents a container for localized names and descriptions.
- class discord_http.commands.PartialCommand(data)¶
Bases:
PartialBaseRepresents a partial command, usually from an event.
- Parameters:
data (dict)
- discord_http.commands.Range¶
alias of
Annotated
- class discord_http.commands.SubCommand(func, *, name, description=None, guild_install=True, user_install=False, guild_ids=None, parent=None)¶
Bases:
CommandRepresents a subcommand, a command that is part of a subcommand group.
- Parameters:
- autocomplete(name) Callable[[Callable], Callable]¶
Decorator to set an option as an autocomplete.
The function must at the end, return a Response.send_autocomplete() object.
Example usage
@commands.command() async def ping(ctx, options: str): return ctx.response.send_message(f"You chose {options}") @ping.autocomplete("options") async def search_autocomplete(ctx, current: str): return ctx.response.send_autocomplete({ "key": "Value shown to user", "feeling_lucky_tm": "I'm feeling lucky!" })
- async run(context, *args, **kwargs) BaseResponse¶
Runs the command.
- Parameters:
context (
Context) – The context of the command.*args – The arguments of the command.
**kwargs – The keyword arguments of the command.
- Return type:
- Returns:
The return type of the command, used by backend.py
- Raises:
UserMissingPermissions – User that ran the command is missing permissions.
BotMissingPermissions – Bot is missing permissions.
- type: int¶
The type of the command, either 1 for chat input, 2 for user context menu, or 3 for message context menu.
- guild_ids: list[Snowflake | int]¶
A list of guild IDs this command is in, empty if it’s a global command.
- cooldown: CooldownCache | None¶
The cooldown rule of the command, if any.
- class discord_http.commands.SubGroup(*, name, description=None, guild_ids=None, guild_install=True, user_install=False, parent=None)¶
Bases:
CommandRepresents a subcommand group, a collection of subcommands.
- Parameters:
- guild_ids: list[Snowflake | int]¶
A list of guild IDs this subcommand group is in, empty if it’s a global subcommand group.
- subcommands: dict[str, SubCommand | SubGroup]¶
A mapping of subcommand names to their respective SubCommand or SubGroup objects.
- parent: SubGroup | None¶
The parent subcommand group of this subcommand group, if it is a nested subcommand group.
- cooldown: CooldownCache | None¶
The cooldown of this subcommand group, if any.
- command(name=None, *, description=None, guild_ids=None, guild_install=True, user_install=False) Callable[[Callable], SubCommand]¶
Decorator to add a subcommand to a subcommand group.
- Parameters:
name (
str|None) – Name of the command (defaults to the function name)description (
str|None) – Description of the command (defaults to the function docstring)guild_ids (
list[Snowflake|int] |None) – List of guild IDs to register the command inuser_install (
bool) – Whether the command can be installed by users or notguild_install (
bool) – Whether the command can be installed by guilds or not
- Return type:
- group(name=None, *, description=None) Callable[[Callable], SubGroup]¶
Decorator to add a subcommand group to a subcommand group.
- autocomplete(name) Callable[[Callable], Callable]¶
Decorator to set an option as an autocomplete.
The function must at the end, return a Response.send_autocomplete() object.
Example usage
@commands.command() async def ping(ctx, options: str): return ctx.response.send_message(f"You chose {options}") @ping.autocomplete("options") async def search_autocomplete(ctx, current: str): return ctx.response.send_autocomplete({ "key": "Value shown to user", "feeling_lucky_tm": "I'm feeling lucky!" })
- async run(context, *args, **kwargs) BaseResponse¶
Runs the command.
- Parameters:
context (
Context) – The context of the command.*args – The arguments of the command.
**kwargs – The keyword arguments of the command.
- Return type:
- Returns:
The return type of the command, used by backend.py
- Raises:
UserMissingPermissions – User that ran the command is missing permissions.
BotMissingPermissions – Bot is missing permissions.
Commands decorators¶
- discord_http.commands.command(name=None, *, description=None, guild_ids=None, guild_install=True, user_install=False) Callable[[Callable], Command]¶
Decorator to register a command.
- Parameters:
name (
str|None) – Name of the command (defaults to the function name)description (
str|None) – Description of the command (defaults to the function docstring)guild_ids (
list[Snowflake|int] |None) – List of guild IDs to register the command inuser_install (
bool) – Whether the command can be installed by users or notguild_install (
bool) – Whether the command can be installed by guilds or not
- Return type:
- discord_http.commands.user_command(name=None, *, guild_ids=None, guild_install=True, user_install=False) Callable[[Callable], Command]¶
Decorator to register a user command.
Example usage
@user_command() async def content(ctx, user: Union[Member, User]): return ctx.response.send_message(f"Target: {user.name}")
- Parameters:
name (
str|None) – Name of the command (defaults to the function name)guild_ids (
list[Snowflake|int] |None) – List of guild IDs to register the command inuser_install (
bool) – Whether the command can be installed by users or notguild_install (
bool) – Whether the command can be installed by guilds or not
- Return type:
- discord_http.commands.message_command(name=None, *, guild_ids=None, guild_install=True, user_install=False) Callable[[Callable], Command]¶
Decorator to register a message command.
Example usage
@message_command() async def content(ctx, msg: Message): return ctx.response.send_message(f"Content: {msg.content}")
- Parameters:
name (
str|None) – Name of the command (defaults to the function name)guild_ids (
list[Snowflake|int] |None) – List of guild IDs to register the command inuser_install (
bool) – Whether the command can be installed by users or notguild_install (
bool) – Whether the command can be installed by guilds or not
- Return type:
- discord_http.commands.group(name=None, *, description=None, guild_ids=None, guild_install=True, user_install=False) Callable[[Callable], SubGroup]¶
Decorator to register a command group.
- Parameters:
name (
str|None) – Name of the command group (defaults to the function name)description (
str|None) – Description of the command group (defaults to the function docstring)guild_ids (
list[Snowflake|int] |None) – List of guild IDs to register the command group inuser_install (
bool) – Whether the command group can be installed by users or notguild_install (
bool) – Whether the command group can be installed by guilds or not
- Return type:
- discord_http.commands.locales(translations) Callable[[Callable], Callable]¶
Decorator to set translations for a command.
_ = Reserved for the root command name and description.
Example usage:
@commands.command(name="ping") @commands.locales({ # Norwegian "no": { "_": ("ping", "Sender en 'pong' melding") "funny": ("morsomt", "Morsomt svar") } }) async def ping(ctx, funny: str): return ctx.response.send_message(f"pong {funny}")
- Parameters:
translations (
dict[Literal['id','da','de','en-GB','en-US','es-ES','fr','es-419','hr','it','lt','hu','nl','no','pl','pt-BR','ro','fi','sv-SE','vi','tr','cs','el','bg','ru','uk','hi','th','zh-CN','ja','zh-TW','ko'],dict[str,list[str] |tuple[str] |tuple[str,str]]]) – The translations for the command name, description, and options.- Return type:
- discord_http.commands.describe(**kwargs) Callable¶
Decorator to set descriptions for a command.
Example usage:
@commands.command() @commands.describe(user="User to ping") async def ping(ctx, user: Member): return ctx.response.send_message(f"Pinged {user.mention}")
- discord_http.commands.allow_contexts(*, guild=True, bot_dm=True, private_dm=True) Callable¶
Decorator to set the places you are allowed to use the command.
Can only be used if the Command has user_install set to True.
- discord_http.commands.cooldown(rate, per, *, type=None) Callable[[Callable], Callable]¶
Decorator to set a cooldown for a command.
Example usage
@commands.command() @commands.cooldown(1, 5.0) async def ping(ctx): return ctx.response.send_message("Pong!")
- Parameters:
rate (
int) – The number of times the command can be used within the cooldown periodper (
float) – The cooldown period in secondstype (
BucketType|None) – The bucket type to use for the cooldown If not set, it will be using default, which is a global cooldown
- Return type:
- discord_http.commands.choices(**kwargs) Callable¶
Decorator to set choices for a command.
Example usage:
@commands.command() @commands.choices( options={ "opt1": "Choice 1", "opt2": "Choice 2", ... } ) async def ping(ctx, options: Choice[str]): return ctx.response.send_message(f"You chose {choice.value}")
- discord_http.commands.guild_only() Callable¶
Decorator to set a command as guild only.
This is a alias to two particular functions: - commands.allow_contexts(guild=True, bot_dm=False, private_dm=False) - commands.check(…) (which checks for Context.guild to be available)
- Return type:
- discord_http.commands.default_permissions(*args) Callable¶
Decorator to set default permissions for a command.
- Return type:
- Parameters:
args (Permissions | str)
- discord_http.commands.has_permissions(*args) Callable¶
Decorator to set permissions for a command.
Example usage:
@commands.command() @commands.has_permissions("manage_messages") async def ban(ctx, user: Member): ...
- Return type:
- Parameters:
args (Permissions | str)
- discord_http.commands.bot_has_permissions(*args) Callable¶
Decorator to set permissions for a command.
Example usage:
@commands.command() @commands.bot_has_permissions("embed_links") async def cat(ctx): ...
- Return type:
- Parameters:
args (Permissions | str)
- discord_http.commands.check(predicate) Callable¶
Decorator to set a check for a command.
Example usage:
def is_owner(ctx): return ctx.author.id == 123456789 @commands.command() @commands.check(is_owner) async def foo(ctx): ...