如何在 discord.py 中修复 "discord.ext.commands.errors.MissingRequiredArgument: author is a required argument that is missing."

How to fix "discord.ext.commands.errors.MissingRequiredArgument: author is a required argument that is missing." in discord.py

我正在尝试为我的机器人创建一个允许用户报告错误的命令,但是当我尝试 运行 代码时我总是收到错误消息。

@client.command()
async def bug(ctx, author, message, m):
    await ctx.send("Greetings! What would you like to report?")
    return m.author == message.author and m.channel == message.channel
    msg = await client.wait_for('message', check=bug)
    bugReport = msg[2:]
    await ctx.send("Thank you for your help. This bug has been reported to the server admins.")
    channel = client.get_channel(private)
    await channel.send(bugReport + " was reported by " + author)

该程序应该在收到错误消息之前询问用户他们想要报告什么,然后切换到错误报告渠道来报告问题,但相反,我得到的只是错误:

discord.ext.commands.errors.MissingRequiredArgument: author is a required argument that is missing.

错误提示缺少必需的参数 author

您上面的代码中有 3 个 author 参数:

  1. 函数中def bug(ctx, author, message, m)
  2. 参数中m.author
  3. 参数中message.author

在你的代码中添加一些if author is not None:语句来检查author参数是否存在,如果不需要则默认设置为None

def bug(ctx, author=None, message, m):
    # Some code here...
    if author is not None:
        await channel.send(bugReport + " was reported by " + author)
    else:
        await channel.send(bugReport)  # without author

P.S。 return 语句后:

return m.author == message.author and m.channel == message.channel

bug函数的其余部分将不会执行(无用)。

您正在重复使用名称 bug 来指代命令本身和 wait_for 的检查。看起来您正在尝试在函数中定义检查,但您缺少定义行。

您似乎希望在调用命令时将 authormessage(以及 m,无论是什么)传递给协程。相反,它们被捆绑到一个 Context 对象中,这是第一个参数。

下面,我已经修改了您的代码,因此它可以从初始调用中获取 report,或者请求它。

@client.command()
async def bug(ctx, *, report):
    def check(message):
        return ctx.author == message.author and ctx.channel == message.channel

    if not report:
        await ctx.send("Greetings! What would you like to report?")
        msg = await client.wait_for('message', check=check)
        report = msg.content[2:]  # This doesn't look right to me, will remove 2 characters
    await ctx.send("Thank you for your help. This bug has been reported to the server admins.")
    channel = client.get_channel(private)
    await channel.send(report + " was reported by " + author)