Discord:停止在Python Cons中打印异常错误

2024-04-25 23:42:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我肯定我错过了一些非常简单的东西,但我就是找不到。我为我发出的命令做了一个错误句柄,它会响应用户。问题是我希望我的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

Tags: inpyliblocalawaitusersappdataext
1条回答
网友
1楼 · 发布于 2024-04-25 23:42:26

是的,该行将错误传播到处理程序之外,由命令调用逻辑处理。对于大多数错误,该逻辑只会将错误打印到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

相关问题 更多 >