Subcommands allow you to group related commands under a single base command. This results in a structure like /base group command in Discord.
Note: Using subcommands or groups makes the base command itself unusable as a standalone command.
There are three ways to define them:
- Decorator Pattern: Define the base command, then use
@base_function.subcommand(...) for additional commands. - Repeat Definition: Define each subcommand as its own
@slash_command with group_name and sub_cmd_name parameters. This is useful for splitting commands across multiple files. - Class Definition: Use the
SlashCommand and .group() objects explicitly.
# Method 1: Decorator Pattern
@slash_command(
name="base",
description="My command base",
group_name="group",
group_description="My command group",
sub_cmd_name="command",
sub_cmd_description="My command",
)
async def my_command_function(ctx: SlashContext):
await ctx.send("Hello World")
@my_command_function.subcommand(
group_name="group",
group_description="My command group",
sub_cmd_name="second_command",
sub_cmd_description="My second command",
)
async def my_second_command_function(ctx: SlashContext):
await ctx.send("Hello World")
# Method 2: Repeat Definition (Good for multiple files)
@slash_command(
name="base",
description="My command base",
group_name="group",
group_description="My command group",
sub_cmd_name="second_command",
sub_cmd_description="My second command",
)
async def my_second_command_function(ctx: SlashContext):
await ctx.send("Hello World")
# Method 3: Class Definition
from interactions import SlashCommand
base = SlashCommand(name="base", description="My command base")
group = base.group(name="group", description="My command group")
@group.subcommand(sub_cmd_name="second_command", sub_cmd_description="My second command")
async def my_second_command_function(ctx: SlashContext):
await ctx.send("Hello World")