不和谐.py |语音通道错误

时间:2021-07-20 04:48:37

标签: python discord discord.py

此代码的目的是当参与者加入某个语音频道时,创建他的个人语音频道。当语音通道为空时,自动删除。该代码完美地执行其任务,但我在控制台中收到错误消息。我该如何解决这个问题?

代码:

@Bot.event
async def on_voice_state_update(member,before,after):
    if after.channel.id == 826351221145206855:
        for guild in Bot.guilds:
            maincategory = discord.utils.get(guild.categories, id=816323445213626368)
            channel2 = await guild.create_voice_channel(name=f'HALL {member.display_name}',category = maincategory)
            await channel2.set_permissions(member,connect=True,mute_members=True,move_members=True,manage_channels=True)
            await member.move_to(channel2)
            def check(x, y, z):
                return len(channel2.members) == 0
            await Bot.wait_for('voice_state_update', check=check)
            await channel2.delete()

控制台错误:

Ignoring exception in on_voice_state_update
Traceback (most recent call last):
  File "C:\Users\Маки\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "d:\import discord.py", line 242, in on_voice_state_update
    if after.channel.id == 826351221145206855:
AttributeError: 'NoneType' object has no attribute 'id'

1 个答案:

答案 0 :(得分:0)

如果成员离开语音频道,将触发 voice_state_update 事件并且 after.channel 将是 None,因为用户不再处于任何语音频道中。因此,您的第一个 if 语句评估为

if None.id == 826351221145206855:

并产生您看到的错误。一个干净的修复是将行修改为

if after.channel and after.channel.id == 826351221145206855:

这样,您的机器人将首先检查用户是否在语音频道中,如果是这种情况,则仅尝试查看 id。这利用了 if None 评估为 Falseif "actual channel" 评估为 True 的 Python 机制,并且不会干扰您的其余代码,因为没有新操作用户离开任何语音频道时需要