You need to enable JavaScript to run this app. Discord.py help命令报MissingRequiredArgument: author缺失如何修复
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py help命令报MissingRequiredArgument: author缺失如何修复

报错触发原因
  • Discord.py 的命令参数解析规则为:除了框架自动注入的上下文对象 ctx 外,函数定义中声明的其余参数均为用户调用命令时必须手动传入的必填项,缺失就会触发 MissingRequiredArgument 报错。
  • 你当前的 help 命令函数签名为 async def help(ctx, *, member, author):,存在两个核心问题:
    • 额外声明的 member、author 参数在实际逻辑中完全没有被用到,代码里后续直接用 ctx.message.author 赋值覆盖了原本的 author 变量,属于无意义的参数要求。
    • Python 语法中*的作用是将后续位置参数标记为“仅关键字参数”,配合 discord.ext.commands 的解析逻辑,* 后面的参数会被用来捕获用户调用时输入的所有剩余文本内容。你在*后同时声明两个独立参数,本身就违反了参数解析逻辑:框架会把用户输入的所有文本内容都塞给第一个*后的参数member,排在后面的author因为没有可分配的解析内容,就会被判定为缺失参数,触发对应报错。
修复方案
  • 移除函数签名里所有多余的必填参数,直接保留自动注入的 ctx 参数即可,因为现有逻辑完全不需要用户传入任何自定义参数。
  • 删掉装饰器中过时的 pass_context=True 参数,当前主流版本的 discord.py 会自动为命令注入 ctx,这个参数已经是废弃语法。
  • 在机器人初始化阶段移除框架自带的内置 help 命令,避免和你自定义的 help 命令产生冲突。

修复后的代码示例:

# 在客户端初始化完成后,先移除框架自带的内置help命令
client.remove_command('help')

@client.command()
async def help(ctx):
    # 直接从上下文对象获取调用者信息,不需要手动声明参数
    author = ctx.message.author

    embed = discord.Embed(
        colour = discord.Colour.red()
    )

    embed.set_author(name='Help Page')
    embed.add_field(name='**[.ping]**', value='*Returns Pong!*', inline=False)
    embed.add_field(name='**[.say]**', value='*Gives you the ability to make the bot say whatever you want!*', inline=False)
    embed.add_field(name='**[.8ball]**', value='*Function that acts just as an 8Ball.*', inline=False)
    embed.add_field(name='**[.ban]**', value='*Bans Users!*', inline=False)
    embed.add_field(name='**[.kick]**', value='*Kicks Users!*', inline=False)

    await ctx.send(embed=embed)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.06 17:24:05