首页 > 解决方案 > 如何修复“discord.ext.commands.errors.MissingRequiredArgument:作者是缺少的必需参数。” 在不和谐.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.

标签: pythondiscord.py-rewrite

解决方案


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

当命令被调用时,您似乎期望authorand message(和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)

推荐阅读