【问题标题】:How to handle `MissingRequiredArgument` errors如何处理“MissingRequiredArgument”错误
【发布时间】: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


【解决方案1】:

最好的方法是使用错误事件。

@ok.error
async def on_command_error(error, ctx):
    await ctx.send(“You did not give me anything to repeat!”)

请注意@ok.error 是故意的,因为它只允许它为该命令起作用,而不会干扰您拥有的任何其他命令。

【讨论】:

  • 命令没有“事件”属性。所以我认为@ok.event 行不通。也许你的意思是@ok.error
  • 我做到了,我的错!
【解决方案2】:

try/except 无法处理此错误,我不知道为什么,但我有两种方法可以处理。

第一种方法:


@bot.command()
async def ok(ctx, con=None):
    if con == None: return await ctx.send('You did not give me anything to repeat!')
    # Do whatever here, if you come here it means the user gave something to repeat

'con' 默认设置为 None (con=None)。如果用户不提供任何东西,它将保持无。但是,如果用户提供了一些东西,那么它将是他所提供的。 if 语句将检测 'con' 是否等于 None,如果是,则表示用户没有返回任何内容。

第二种方法


@bot.command()
async def ok(ctx, con):
    # Do whatever here, if you come here it means the user gave something to repeat

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        print('You did not give me anything to repeat!')

我在这里使用了@bot.event,这意味着将为所有没有参数的命令处理错误(换句话说,得到MissingRequiredArgument 错误的命令) 如果您只想使用“ok”命令,请使用@ok.error 而不是@bot.event

【讨论】:

  • 另外,如果你使用@ok.error,你可以随意命名on_command_error
【解决方案3】:

我不确定其他答案是否有效,但这会

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, MissingRequiredArgument):
        await ctx.send("A parameter is missing") 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 2023-03-28
    • 2015-10-31
    相关资源
    最近更新 更多