Discord:在Python控制台中打印停止异常错误

问题描述 投票:0回答:1

我确信我缺少一些非常简单的东西,但是我找不到它。我为我做出的命令做了一个错误处理,它会响应用户。问题是我希望我的bot python脚本的控制台干净,因此我正在寻找一种方法来消除在控制台中打印的大错误。即使我处理了它,它仍然会在控制台中打印异常,这是我要阻止的异常。非常感谢您提供的所有帮助。

代码:

    @bal.error
    async def bal_error(self, ctx, error):
        if isinstance(error, discord.ext.commands.BadArgument):
            await ctx.send('Balance: Please specify a user. Syntax (!bal {mention})')
        raise error #I am pretty sure there is no need for this.

我试图避免将其打印到控制台的错误:

Ignoring exception in on_command_error
Traceback (most recent call last):
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 270, in _run
_event
    await coro(*args, **kwargs)
  File "C:\Users\gunzb\Desktop\AfterClap Bot\bot.py", line 37, in on_command_error
    raise error
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 86
3, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 7
21, in invoke
    await self.prepare(ctx)
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 6
85, in prepare
    await self._parse_arguments(ctx)
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 6
08, in _parse_arguments
    kwargs[name] = await self.transform(ctx, param)
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 4
55, in transform
    return await self.do_conversion(ctx, converter, argument, param)
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 4
08, in do_conversion
    return await self._actual_conversion(ctx, converter, argument, param)
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 3
54, in _actual_conversion
    ret = await instance.convert(ctx, argument)
  File "C:\Users\gunzb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\converter.py", l
ine 132, in convert
    raise BadArgument('Member "{}" not found'.format(argument))
discord.ext.commands.errors.BadArgument: Member "n" not found
python discord.py discord.py-rewrite
1个回答
0
投票

是的,该行将错误传播到处理程序之外,该错误由命令调用逻辑处理。对于大多数错误,该逻辑只会将错误打印到sys.stderr,然后将其忽略。

我建议仅在代码未处理的情况下重新引发错误:

@bal.error
async def bal_error(self, ctx, error):
    if isinstance(error, discord.ext.commands.BadArgument):
        await ctx.send('Balance: Please specify a user. Syntax (!bal {mention})')
    else:
        raise error # Only called for other errors
© www.soinside.com 2019 - 2024. All rights reserved.