【发布时间】:2021-02-17 04:07:41
【问题描述】:
我为任何有参数提及通道的命令添加了一个简单的错误处理程序。如果您提到了无效频道,它将发送错误消息。这是处理程序:
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.ChannelNotFound):
await ctx.send('Oops, thats not a channel!')
return
raise error
但它没有发送错误消息,而是将其放在控制台中:
Ignoring exception in on_command_error
Traceback (most recent call last):
File "C:\Users\mmein\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "pogtato.py", line 39, in on_command_error
raise error
File "C:\Users\mmein\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\mmein\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 851, in invoke
await self.prepare(ctx)
File "C:\Users\mmein\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 786, in prepare
await self._parse_arguments(ctx)
File "C:\Users\mmein\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 697, in _parse_arguments
transformed = await self.transform(ctx, param)
File "C:\Users\mmein\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 552, in transform
return await self.do_conversion(ctx, converter, argument, param)
File "C:\Users\mmein\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 505, in do_conversion
return await self._actual_conversion(ctx, converter, argument, param)
File "C:\Users\mmein\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 451, in _actual_conversion
ret = await instance.convert(ctx, argument)
File "C:\Users\mmein\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\converter.py", line 321, in convert
raise ChannelNotFound(argument)
discord.ext.commands.errors.ChannelNotFound: Channel "yes" not found.
这是一个会引发错误的命令示例:
@bot.command()
async def chantest(ctx, channel : TextChannel):
await ctx.send(channel.id)
该命令只是打印出标记频道的 ID,但是当我尝试使用该命令而不标记频道时应该会引发错误。
我不知道为什么会发生这种情况,如果您能提供帮助,请提前感谢。
【问题讨论】:
-
是的,我试图提及无效的频道“yes”。
-
您收到错误是因为您提出了错误,请参阅
raise error -
无论哪种方式都会出现同样的错误
-
@TinNguyen 不,他只提出不是
ChannelNotFound错误的错误。如果他的command抛出其中一个,他将进入 if 语句和return,所以它不可能再次抛出同样的一个(因为它无法到达那部分代码) . -
按照我的理解,我只是提出一个错误,它不是 ChannelNotFound 错误。由于删除 raise 错误似乎并没有解决任何问题,而且我还在其他有效的错误处理程序上对其进行了测试,但它破坏了它们,所以我不会更新我的帖子。
标签: python error-handling discord discord.py