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

Discord.py:如何为CommandTree斜杠命令参数设置名称与描述

给discord.py斜杠命令参数设置自定义名称与描述

在discord.py 2.0+版本中,要给CommandTree的斜杠命令参数添加自定义名称和描述,有两种简单的实现方式:

方式1:使用@app_commands.describe()装饰器

这个装饰器可以直接为每个参数添加描述,语法简单直观:

import discord
from discord import app_commands

@tree.command(name="send", description="给指定用户转账", guild=discord.Object(guildId))
@app_commands.describe(member="接收转账的目标用户")
async def send(interaction: discord.Interaction, member: discord.Member):
    # 这里写转账逻辑
    await interaction.response.send_message(f"已成功向{member.mention}转账")

方式2:使用app_commands.Parameter类

如果需要自定义参数在Discord界面上显示的名称(而不是用变量名),可以用这个类来包装参数类型,同时设置名称和描述:

import discord
from discord import app_commands

@tree.command(name="send", description="给指定用户转账", guild=discord.Object(guildId))
async def send(
    interaction: discord.Interaction,
    member: app_commands.Parameter[discord.Member],
    name="目标用户",
    description="你要转账的用户"
):
    # 转账逻辑代码
    await interaction.response.send_message(f"已成功向{member.mention}转账")

注意事项

  • 必须确保你的discord.py版本是2.0或更高,CommandTree和这些参数配置功能都是2.0版本才引入的。
  • 如果只需要添加描述,用describe装饰器更简洁;如果要修改参数显示名称,用Parameter类更合适。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 20:55:10