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

如何为nextcord斜杠命令的参数添加描述?

解决Nextcord斜杠命令参数添加描述的问题

要给Nextcord的斜杠命令参数添加描述,你需要使用nextcord.SlashOption(部分旧版本可能为nextcord.Option)来为每个参数指定描述信息,而非直接使用普通类型注解。以下是两种可行的实现方式:

方式一:在参数注解中直接使用SlashOption

import nextcord
from nextcord import Interaction, SlashOption
from typing import Optional

@__Bot__.slash_command(description="Retrieve a Quran verse.")
async def hello(
    interaction: Interaction,
    start: str = SlashOption(description="起始章节,例如:1"),
    verse: str = SlashOption(description="起始经文,例如:1"),
    end: Optional[int] = SlashOption(description="结束经文(可选)", required=False)
):
    VerseCommand = [(start, verse, end)]
    # 你的业务逻辑代码

方式二:在slash_command装饰器中定义options列表

import nextcord
from nextcord import Interaction
from typing import Optional

@__Bot__.slash_command(
    description="Retrieve a Quran verse.",
    options=[
        nextcord.SlashOption(
            name="start",
            description="起始章节,例如:1",
            required=True
        ),
        nextcord.SlashOption(
            name="verse",
            description="起始经文,例如:1",
            required=True
        ),
        nextcord.SlashOption(
            name="end",
            description="结束经文(可选)",
            required=False
        )
    ]
)
async def hello(interaction: Interaction, start: str, verse: str, end: Optional[int]):
    VerseCommand = [(start, verse, end)]
    # 你的业务逻辑代码

注意事项

  1. 你之前导入的Options是错误的,需替换为SlashOption(或旧版本的Option)。
  2. 修改命令后必须重新同步斜杠命令到Discord,可在机器人启动时调用__Bot__.sync_commands()完成同步,否则Discord端不会更新参数描述。
  3. 对于可选参数,需同时设置required=False和使用Optional类型注解。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 00:22:33