Commands

discord_http.commands module

class discord_http.commands.Choice(key: ChoiceT, value: str | int | float)[source]

Bases: Generic[ChoiceT]

Makes it possible to access both the name and value of a choice.

Defaults to a string type

Paramaters

key: str

The key of the choice from your dict.

value: Union[int, str, float]

The value of your choice (the one that is shown to public)

class discord_http.commands.Cog(*args, **kwargs)[source]

Bases: object

async cog_load() None[source]

Called before the cog is loaded

async cog_unload() None[source]

Called before the cog is unloaded

class discord_http.commands.Command(command: Callable, name: str, description: str | None = None, guild_ids: list[Snowflake | int] | None = None, guild_install: bool = True, user_install: bool = False, type: ApplicationCommandType = ApplicationCommandType.chat_input)[source]

Bases: object

autocomplete(name: str)[source]

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!"
    })
Parameters:

name (str) – Name of the option to set as an autocomplete.

property cooldown: CooldownCache | None

Returns the cooldown rule of the command if available

Type:

Optional[CooldownCache]

property mention: str

Returns a mentionable string for the command

Type:

str

mention_sub(suffix: str) str[source]

Returns a mentionable string for a subcommand.

Parameters:

suffix (str) – The subcommand name.

Returns:

The mentionable string.

Return type:

str

async run(context: Context, *args, **kwargs) BaseResponse[source]

Runs the command.

Parameters:

context (Context) – The context of the command.

Returns:

The return type of the command, used by backend.py (Quart)

Return type:

BaseResponse

Raises:
async run_autocomplete(context: Context, name: str, current: str) dict[source]

Runs the autocomplete

Parameters:
  • context (Context) – Context object for the command

  • name (str) – Name of the option

  • current (str) – Current value of the option

Returns:

The return type of the command, used by backend.py (Quart)

Return type:

dict

Raises:

TypeError – Autocomplete must return an AutocompleteResponse object

to_dict() dict[source]

Converts the Discord command to a dict.

Returns:

The dict of the command.

Return type:

dict

class discord_http.commands.Interaction(func: Callable, custom_id: str, *, regex: bool = False)[source]

Bases: object

match(custom_id: str) bool[source]

Matches the custom ID with the interaction. Will always return False if the interaction is not a regex.

Parameters:

custom_id (str) – The custom ID to match.

Returns:

Whether the custom ID matched or not.

Return type:

bool

async run(context: Context) BaseResponse[source]

Runs the interaction.

Parameters:

context (Context) – The context of the interaction.

Returns:

The return type of the interaction, used by backend.py (Quart)

Return type:

BaseResponse

Raises:

TypeError – Interaction must be a Response object

class discord_http.commands.Listener(name: str, coro: Callable)[source]

Bases: object

async run(*args, **kwargs)[source]

Runs the listener

class discord_http.commands.PartialCommand(data: dict)[source]

Bases: PartialBase

class discord_http.commands.Range(opt_type: CommandOptionType, min: int | float | str | None, max: int | float | str | None)[source]

Bases: object

Makes it possible to create a range rule for command arguments

When used in a command, it will only return the value if it’s within the range.

Example usage:

Range[str, 1, 10]        # (min and max length of the string)
Range[int, 1, 10]        # (min and max value of the integer)
Range[float, 1.0, 10.0]  # (min and max value of the float)
Parameters:
  • opt_type (CommandOptionType) – The type of the range

  • min (Union[int, float, str]) – The minimum value of the range

  • max (Union[int, float, str]) – The maximum value of the range

class discord_http.commands.SubGroup(*, name: str, description: str | None = None, guild_ids: list[Snowflake | int] | None = None, guild_install: bool = True, user_install: bool = False)[source]

Bases: Command

add_group(name: str) SubGroup[source]

Adds a subcommand group to a subcommand group

Parameters:

name (str) – Name of the subcommand group

Returns:

The subcommand group

Return type:

SubGroup

command(name: str | None = None, *, description: str | None = None, guild_ids: list[Snowflake | int] | None = None, guild_install: bool = True, user_install: bool = False)[source]

Decorator to add a subcommand to a subcommand group

Parameters:
  • name (Optional[str]) – Name of the command (defaults to the function name)

  • description (Optional[str]) – Description of the command (defaults to the function docstring)

  • guild_ids (Optional[list[Union[Snowflake, int]]]) – List of guild IDs to register the command in

  • user_install (bool) – Whether the command can be installed by users or not

  • guild_install (bool) – Whether the command can be installed by guilds or not

group(name: str | None = None, *, description: str | None = None)[source]

Decorator to add a subcommand group to a subcommand group

Parameters:

name (Optional[str]) – Name of the subcommand group (defaults to the function name)

property options: list[dict]

Returns the options of the subcommand group

Type:

list[dict]

Commands decorators

discord_http.commands.command(name: str | None = None, *, description: str | None = None, guild_ids: list[Snowflake | int] | None = None, guild_install: bool = True, user_install: bool = False)[source]

Decorator to register a command.

Parameters:
  • name (Optional[str]) – Name of the command (defaults to the function name)

  • description (Optional[str]) – Description of the command (defaults to the function docstring)

  • guild_ids (Optional[list[Union[Snowflake, int]]]) – List of guild IDs to register the command in

  • user_install (bool) – Whether the command can be installed by users or not

  • guild_install (bool) – Whether the command can be installed by guilds or not

discord_http.commands.user_command(name: str | None = None, *, guild_ids: list[Snowflake | int] | None = None, guild_install: bool = True, user_install: bool = False)[source]

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 (Optional[str]) – Name of the command (defaults to the function name)

  • guild_ids (Optional[list[Union[Snowflake, int]]]) – List of guild IDs to register the command in

  • user_install (bool) – Whether the command can be installed by users or not

  • guild_install (bool) – Whether the command can be installed by guilds or not

discord_http.commands.message_command(name: str | None = None, *, guild_ids: list[Snowflake | int] | None = None, guild_install: bool = True, user_install: bool = False)[source]

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 (Optional[str]) – Name of the command (defaults to the function name)

  • guild_ids (Optional[list[Union[Snowflake, int]]]) – List of guild IDs to register the command in

  • user_install (bool) – Whether the command can be installed by users or not

  • guild_install (bool) – Whether the command can be installed by guilds or not

discord_http.commands.group(name: str | None = None, *, description: str | None = None, guild_ids: list[Snowflake | int] | None = None, guild_install: bool = True, user_install: bool = False)[source]

Decorator to register a command group.

Parameters:
  • name (Optional[str]) – Name of the command group (defaults to the function name)

  • description (Optional[str]) – Description of the command group (defaults to the function docstring)

  • guild_ids (Optional[list[Union[Snowflake, int]]]) – List of guild IDs to register the command group in

  • user_install (bool) – Whether the command group can be installed by users or not

  • guild_install (bool) – Whether the command group can be installed by guilds or not

discord_http.commands.locales(translations: Dict[Literal['id', 'da', 'de', 'en-GB', 'en-US', 'es-ES', 'fr', '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]]])[source]

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[LocaleTypes, Dict[str, Union[tuple[str], tuple[str, str]]]]) – The translations for the command name, description, and options.

discord_http.commands.describe(**kwargs)[source]

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: bool = True, bot_dm: bool = True, private_dm: bool = True)[source]

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.

Parameters:
  • guild (bool) – Weather the command can be used in guilds.

  • bot_dm (bool) – Weather the command can be used in bot DMs.

  • private_dm (bool) – Weather the command can be used in private DMs.

discord_http.commands.cooldown(rate: int, per: float, *, type: BucketType | None = None)[source]

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 period

  • per (float) – The cooldown period in seconds

  • key (Optional[BucketType]) – The bucket type to use for the cooldown If not set, it will be using default, which is a global cooldown

discord_http.commands.choices(**kwargs: dict[str | int | float, str | int | float])[source]

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()[source]

Decorator to set a command as guild only.

This is a alias to commands.allow_contexts(guild=True, bot_dm=False, private_dm=False)

discord_http.commands.is_nsfw()[source]

Decorator to set a command as NSFW.

discord_http.commands.default_permissions(*args: Permissions | str)[source]

Decorator to set default permissions for a command.

discord_http.commands.has_permissions(*args: Permissions | str)[source]

Decorator to set permissions for a command.

Example usage:

@commands.command()
@commands.has_permissions("manage_messages")
async def ban(ctx, user: Member):
    ...
discord_http.commands.bot_has_permissions(*args: Permissions | str)[source]

Decorator to set permissions for a command.

Example usage:

@commands.command()
@commands.bot_has_permissions("embed_links")
async def cat(ctx):
    ...
discord_http.commands.check(predicate: Callable | Coroutine)[source]

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):
    ...
discord_http.commands.interaction(custom_id: str, *, regex: bool = False)[source]

Decorator to register an interaction.

This supports the usage of regex to match multiple custom IDs.

Parameters:
  • custom_id (str) – The custom ID of the interaction. (can be partial, aka. regex)

  • regex (bool) – Whether the custom_id is a regex or not

discord_http.commands.listener(name: str | None = None)[source]

Decorator to register a listener.

Parameters:

name (Optional[str]) – Name of the listener (defaults to the function name)

Raises:

TypeError

  • If name was not a string - If the listener was not a coroutine function