Skip to content

Add decorator-based subcommand syntax and type-annotation-based parameter syntax

Compare
Choose a tag to compare
@breqdev breqdev released this 02 Apr 04:41
· 430 commits to main since this release

This release makes some changes to the syntax for declaring subcommands and command parameters in order to bring it more in line with Discord.py.

Note that the old syntax still works, but moving forward, the new syntax will be the preferred method.

Subcommands

Old syntax:

@discord.command(options=[
    {
        "name": "google",
        "description": "Search with Google",
        "type": CommandOptionType.SUB_COMMAND,
        "options": [{
            "name": "query",
            "description": "Search query",
            "type": CommandOptionType.STRING,
            "required": True
        }]
    },
    {
        "name": "bing",
        "description": "Search with Bing",
        "type": CommandOptionType.SUB_COMMAND,
        "options": [{
            "name": "query",
            "description": "Search query",
            "type": CommandOptionType.STRING,
            "required": True
        }]
    }
])
def search(ctx, subcommand, *, query):
    "Search the Internet!"
    quoted = urllib.parse.quote_plus(query)
    if subcommand == "google":
        return f"https://google.com/search?q={quoted}"
    if subcommand == "bing":
        return f"https://bing.com/search?q={quoted}"

New syntax:

comic = discord.command_group("comic")


@comic.command()
def xkcd(ctx, number: int):
    return f"https://xkcd.com/{number}/"


@comic.command()
def homestuck(ctx, number: int):
    return f"https://homestuck.com/story/{number}"

Command Parameters

Old syntax:

@discord.command(options=[{
    "name": "channel",
    "description": "The channel to show information about",
    "type": CommandOptionType.CHANNEL,
    "required": True
}])
def channel_info(ctx, channel):
    return InteractionResponse("...")

New syntax:

@discord.command(annotations={"channel": "The channel to show information about"})
def channel_info(ctx, channel: Channel):
    return Response(...)

Renaming

Old names are (for now) being kept as aliases.

  • InteractionResponse -> Response
  • InteractionContext -> Context