繁体   English   中英

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

[英]Discord.py | Error with the voice channel

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

代码:

@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'

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

if None.id == 826351221145206855:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM