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

Discord机器人/test命令报错:缺少必填关键字参数arg求解

问题解答

错误原因

你写的是前缀命令的代码(使用@bot.command()装饰器),但实际调用的是Discord的斜杠(Slash)命令,这两套命令系统完全独立,参数解析逻辑不兼容,所以会抛出缺少arg参数的错误。

正确实现斜杠命令的方法

要创建支持/test <文本>的斜杠命令,需要用discord.py v2.x版本的原生斜杠命令API,具体代码如下:

from discord import app_commands
from discord.ext import commands
import discord

# 配置机器人权限
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)

# 机器人上线时同步斜杠命令到服务器
@bot.event
async def on_ready():
    await bot.tree.sync()
    print(f"{bot.user} 已启动")

# 定义/test斜杠命令
@bot.tree.command(name="test", description="重复你输入的文本")
@app_commands.describe(arg="需要重复的内容")
async def test(interaction: discord.Interaction, arg: str):
    await interaction.response.send_message(arg)

# 运行机器人
bot.run("你的机器人令牌")

关键说明

  • 必须确保discord.py版本≥2.0,v1.x版本不支持原生斜杠命令
  • @bot.tree.command()是斜杠命令的专用装饰器,name对应命令名,description会在Discord输入框显示
  • @app_commands.describe()用来给参数添加说明,提升用户体验
  • 用interaction代替原来的ctx,这是斜杠命令的交互对象,需要通过它来回复消息
  • 首次运行时bot.tree.sync()会把命令同步到Discord,可能需要等待1-2分钟才会生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 19:13:15