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

时间:2019-08-31 00:09:49

标签: python-3.x discord.py discord.py-rewrite

我在使用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)

运行该程序并将作者连接到语音通道时,一切正常,并且漫游器已连接到该通道。但是,当作者未连接到语音通道(并且未将其作为参数传递)时,错误不会出现出现,并且不会调用错误处理程序(如下):

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 终止了bot,那么我看到了以下内容:

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.

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

1 个答案:

答案 0 :(得分:2)

您似乎正在参考旧的示例或文档。 list of special Cog methods表示您应该改用cog_command_error