【发布时间】:2020-12-19 05:31:38
【问题描述】:
我在 discord.py 的 rewrite 分支中制作了一个机器人,可以说这是我的代码:
@bot.command()
async def ok(ctx,con):
try:
await ctx.send(con)
except commands.MissingRequiredArgument:
await ctx.send('You did not give me anything to repeat!')
我要做的是处理 MissingRequiredArgument 错误,但我编写的代码仍然给出错误而不是返回 You did not give me anything to repeat! 如果有人能告诉我如何处理它,我将不胜感激。
确切的错误:
Ignoring exception in command translate:
Traceback (most recent call last):
File "C:\Users\jatinder\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\jatinder\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 790, in invoke
await self.prepare(ctx)
File "C:\Users\jatinder\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 751, in prepare
await self._parse_arguments(ctx)
File "C:\Users\jatinder\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 679, in _parse_arguments
kwargs[name] = await self.transform(ctx, param)
File "C:\Users\jatinder\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 516, in transform
raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: con is a required argument that is missing.
【问题讨论】:
-
通过对 discord.py 存储库本身上写的异常的外观,似乎 MissingRequiredArgument 只是当您没有传递所需参数时发生的异常,并且 ctx.send() 方法具有一个论点。您是否尝试过在没有任何 try/except 语句的情况下使用 await ctx.send(con) 以及如果出现错误它会说什么?
-
@lambda23 我确实尝试过,但它给出了同样的错误
-
确切的错误是什么(堆栈跟踪)?您可能还想包含 discord.py 版本,并在他们的 GitHub 存储库中检查它是否是(持续的)问题。
-
@lambda23 添加了它,我会检查他们的 github
-
原始代码的注释。如果 con 是
None,您可能只发送 Discord 消息,而不是以这种方式包装它。最好避免方法抛出错误,而不是让它抛出错误。另外,我认为con是命令的第一个参数而不是参数数组,您可以使用*args作为方法的第二个参数。这样,您将使用您的参数作为一个数组,您可以检查您的第一个参数 (args[0]) 是否为 None,然后您将发送消息。
标签: python error-handling discord discord.py discord.py-rewrite