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)
- 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:
- command¶
The function that is called when the command is invoked.
- type: int¶
The type of the command, either 1 for chat input, 2 for user context menu, or 3 for message context menu.
- name¶
The name of the command.
- parent¶
The parent subcommand group of this command, if it is a subcommand.
- guild_install¶
Whether this command should be installed in guilds or not.
- user_install¶
Whether this command should be installed for users or not.
- guild_ids: list[Snowflake | int]¶
A list of guild IDs this command is in, empty if it’s a global command.
- description¶
The description of the command, only applicable for chat input commands.
- options¶
The options of the command, only applicable for chat input commands.
- property cooldown: CooldownCache | None¶
Returns the cooldown rule of the command if available.
- 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:
BaseResponse- 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.
- to_dict() dict¶
Converts the Discord command to a dict.
- Return type:
- Returns:
The dict of the command.
- 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): await ctx.send(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.
- class discord_http.commands.Listener(*, name, coro)¶
Bases:
objectRepresents a Discord event listener.
- name¶
The name of the event to listen to, e.g. “on_message_create”.
- coro¶
The coroutine function that is called when the event is triggered.
- 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.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:
- name¶
The name of the subcommand group.
- description¶
The description of the subcommand group.
- guild_ids: list[Snowflake | int]¶
A list of guild IDs this subcommand group is in, empty if it’s a global subcommand group.
- type: int¶
The type of the command, either 1 for chat input, 2 for user context menu, or 3 for message context menu.
- subcommands: dict[str, SubCommand | SubGroup]¶
A mapping of subcommand names to their respective SubCommand or SubGroup objects.
- guild_install¶
Whether this subcommand group should be installed in guilds or not.
- user_install¶
Whether this subcommand group should be installed for users or not.
- parent: SubGroup | None¶
The parent subcommand group of this subcommand group, if it is a nested subcommand group.
- 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): await ctx.send(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!" })
- property cooldown: CooldownCache | None¶
Returns the cooldown rule of the command if available.
- 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:
BaseResponse- 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]): await ctx.send(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): await ctx.send(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): await ctx.send(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): await ctx.send(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): await ctx.send("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]): await ctx.send(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): ...