如何为Discord斜杠命令的参数添加描述(Python+discord.ext)
解决Discord斜杠命令参数描述及前缀命令同步问题
一、给斜杠命令的可选参数添加描述
用@bot.tree.command定义的斜杠命令,不能复用前缀命令(@bot.command)的参数描述方式,得用discord.app_commands提供的专属工具:
方法1:使用@app_commands.describe装饰器
在命令函数上方追加该装饰器,直接指定参数的描述文本:
from discord import app_commands import datetime @client.tree.command(name="command", description="test command") @app_commands.describe(date="查询日期,格式为YYYY-MM-DD,默认取当前日期") async def scores(interaction: discord.Interaction, date: str=datetime.now(tz).strftime('%Y-%m-%d')): await interaction.response.send_message(str("\n".join(testcommand.getinfo(date))))
方法2:使用app_commands.Parameter绑定默认值与描述
直接在参数定义里用Parameter对象,同时设置默认值和描述信息:
from discord import app_commands import datetime @client.tree.command(name="command", description="test command") async def scores( interaction: discord.Interaction, date: str = app_commands.Parameter( default=datetime.now(tz).strftime('%Y-%m-%d'), description="查询日期,格式为YYYY-MM-DD,默认取当前日期" ) ): await interaction.response.send_message(str("\n".join(testcommand.getinfo(date))))
二、关于@bot.command命令的同步问题
@bot.command定义的是前缀命令,这类命令不需要同步到Discord服务器(仅斜杠命令需要同步操作),只要机器人正常上线就能通过前缀触发。如果前缀命令没反应,优先检查这几点:
- 机器人是否拥有对应频道的消息读取、发送权限
- 调用命令时是否用对了前缀(默认前缀为
!,自定义前缀需对应) - 命令函数名与实际调用的命令名是否一致(或通过
name参数指定了正确名称)
内容的提问来源于stack exchange,提问作者Schwa
相关产品推荐
相关产品推荐

