📅  最后修改于: 2023-12-03 15:14:42.432000             🧑  作者: Mango
Discord.py is a Python library that allows you to interact with the Discord API. One of its key features is the ability to create commands for your bot using the commands module. The group command specifically allows you to group together related commands under a single parent command.
To use the commands.group decorator, you must first create an instance of Bot or AutoShardedBot from the discord.ext.commands module. You can then create a command group by adding the @commands.group
decorator to a function that will act as the parent command. Any functions with the @parent_cmd.group()
decorator will now be a subcommand of the parent command.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.command()
async def parent_cmd(ctx):
await ctx.send('This is the parent command')
@parent_cmd.group()
async def sub_cmd(ctx):
await ctx.send('This is the subcommand')
@sub_cmd.command()
async def sub_sub_cmd(ctx):
await ctx.send('This is the sub-subcommand')
In the above example, the sub_cmd
and sub_sub_cmd
functions are both subcommands of the parent_cmd
function.
To display this information in a markdown format:
## Discord.py Commands.Group Overview
Discord.py is a Python library that allows you to interact with the Discord API. One of its key features is the ability to create commands for your bot using the commands module. The group command specifically allows you to group together related commands under a single parent command.
### Creating a Command Group
To use the `commands.group` decorator, you must first create an instance of Bot or AutoShardedBot from the `discord.ext.commands` module. You can then create a command group by adding the `@commands.group` decorator to a function that will act as the parent command. Any functions with the `@parent_cmd.group()` decorator will now be a subcommand of the parent command.
```python
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.command()
async def parent_cmd(ctx):
await ctx.send('This is the parent command')
@parent_cmd.group()
async def sub_cmd(ctx):
await ctx.send('This is the subcommand')
@sub_cmd.command()
async def sub_sub_cmd(ctx):
await ctx.send('This is the sub-subcommand')
In the above example, the sub_cmd
and sub_sub_cmd
functions are both subcommands of the parent_cmd
function.