You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无Cogs时为Discord.py Slash命令添加子命令的方法问询

未使用Cogs时给Discord.py命令添加子命令的方法

当然可以,哪怕不用Cogs,也能通过app_commands.Group来给命令添加子命令。

具体实现步骤如下:

  • 创建Group对象作为父命令的容器
  • 使用@group.command()装饰器定义各个子命令
  • 将Group注册到你的CommandTree(即代码里的tree)

示例代码:

from discord import app_commands

# 假设你已初始化好bot和tree对象
# 创建父命令Group
test_group = app_commands.Group(name="test", description="just a test")

# 定义第一个子命令:/test url
@test_group.command(name="url", description="处理指定URL")
@app_commands.describe(url="需要处理的URL")
async def test_url(interaction, url: str):
    await run_command(interaction=interaction, url=url)

# 可选:添加第二个子命令作为示例,比如/test info
@test_group.command(name="info", description="显示测试信息")
async def test_info(interaction):
    await interaction.response.send_message("这是test命令的info子命令")

# 将Group注册到命令树
tree.add_command(test_group)

使用时,用户在Discord中可以输入/test url <URL内容>触发原功能,或输入/test info调用新增的子命令。

注意:如果之前已经通过@tree.command(name="test")注册过同名命令,需要先移除它,否则会和Group产生冲突。

内容的提问来源于stack exchange,提问作者Juno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 06:52:34