我正在尝试为我的机器人创建一个命令,该命令允许用户进行错误报告,但是在尝试运行代码时,我总是遇到错误。
@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.
答案 0 :(得分:0)
该错误表明缺少必需的参数author
。
上面的代码中有3个author
参数:
def bug(ctx, author, message, m)
m.author
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
函数的其余部分将不会执行(无用)。
答案 1 :(得分:0)
您正在重用名称bug
来同时引用命令本身和对wait_for
的检查。看起来您正在尝试在函数中定义检查,但缺少定义行。
您似乎希望在调用命令时将author
和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)