Discord.py 在程序终止之前不调用错误处理程序,也不打印回溯

Discord.py not calling error handler, and not printing traceback until the program is terminated

我在 discord.py 的错误处理方面遇到了一些问题。我设置了以下命令:

class InvalidVoiceChannel(VoiceConnectionError):
    """Exception for cases of invalid Voice Channels."""

@commands.command(name='connect', aliases=['join'])
async def connect_(self, ctx, *, channel: discord.VoiceChannel = None):
    if not channel:
        try:
            channel = ctx.author.voice.channel
        except AttributeError:
            raise InvalidVoiceChannel(
                'No channel to join. Please either specify a valid channel'
                ' or join one.'
            )

    vc = ctx.voice_client

    if vc:
        if vc.channel.id == channel.id:
            return
        try:
            await vc.move_to(channel)
        except asyncio.TimeoutError:
            raise VoiceConnectionError(
                f'Moving to channel: <{channel}> timed out.'
            )
    else:
        try:
            await channel.connect()
        except asyncio.TimeoutError:
            raise VoiceConnectionError(
                f'Connecting to channel: <{channel}> timed out.'
            )

    )
    await ctx.send(f'Connected to: **{channel}**', delete_after=20)

当运行并且作者连接到语音频道时,一切正常,机器人连接到频道。然而,当作者没有连接到语音通道(并且没有作为参数传递)时,错误不会 appear 被引发,并且错误处理程序(下面)是未调用:

async def __error(self, ctx, error):
    if isinstance(error, commands.NoPrivateMessage):
        try:
            return await ctx.send(
                'This command can not be used in private messages.'
            )
        except discord.HTTPException:
            pass
    elif isinstance(error, InvalidVoiceChannel):
        await ctx.send(
            'Error connecting to voice channel. Please make sure you are'
            ' in a valid channel or provide me with one.'
        )
    else:
        raise error

一旦我按下 Ctrl+C 终止机器人,然后我看到以下打印:

Ignoring exception in command connect:                                                               
Traceback (most recent call last):
  File "/home/danth/Code/discord-sounds/sounds/cogs/music.py", line 269, in connect_
    channel = ctx.author.voice.channel
AttributeError: 'NoneType' object has no attribute 'channel'
During handling of the above exception, another exception occurred:
  Traceback (most recent call last):
    File "/home/danth/Code/.virtualenvs/discord-sounds/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 863, in invoke
      await ctx.command.invoke(ctx)
    File "/home/danth/Code/.virtualenvs/discord-sounds/lib/python3.7/site-packages/discord/ext/commands/core.py", line 728, in invoke
      await injected(*ctx.args, **ctx.kwargs)
    File "/home/danth/Code/.virtualenvs/discord-sounds/lib/python3.7/site-packages/discord/ext/commands/core.py", line 79, in wrapped
      ret = await coro(*args, **kwargs)
  File "/home/danth/Code/discord-sounds/sounds/cogs/music.py", line 272, in connect_
    'No channel to join. Please either specify a valid channel'
cogs.music.InvalidVoiceChannel: No channel to join. Please either specify a valid channel or join one.

为什么不调用错误处理程序,为什么不立即出现回溯?

您可能指的是旧示例或文档。 list of special Cog methods implies you should use cog_command_error 代替。