Python 当此频道中的成员为0时,创建专用语音频道并删除此频道

Python 当此频道中的成员为0时,创建专用语音频道并删除此频道,python,discord,bots,discord.py,discord.py-rewrite,Python,Discord,Bots,Discord.py,Discord.py Rewrite,我写了一个代码,如果你去某个语音频道,就会创建一个新的私人语音频道,问题是在该语音频道没有人之后,应该将其删除,我为此写了一个代码,但它不起作用 以下是代码本身: @commands.Cog.listener() async def on_voice_state_update(self, member, before, after): cursor.execute(f'SELECT start_voice_channel FROM public."myBD" W

我写了一个代码,如果你去某个语音频道,就会创建一个新的私人语音频道,问题是在该语音频道没有人之后,应该将其删除,我为此写了一个代码,但它不起作用

以下是代码本身:

@commands.Cog.listener()
async def on_voice_state_update(self, member, before, after):   
    cursor.execute(f'SELECT start_voice_channel FROM public."myBD" WHERE guild_id = \'{member.guild.id}\';')
    v_c = cursor.fetchone()
    voice_channel = v_c[0]
    
    cursor.execute(f'SELECT categori FROM public."myBD" WHERE guild_id = \'{member.guild.id}\';')
    c_c = cursor.fetchone()
    channel_category = c_c[0]

    if voice_channel is not None and channel_category is not None:
        print(f'\n First before: {before}\n')
        print(f'\n First after: {after}\n')
        print(f'\n Second before: {before.channel.id}\n')
        print(f'\n Second after: {after.channel.id}\n')
        if after.channel.id == voice_channel:
            maincategori = get(member.guild.categories, id = channel_category)
            channel2 = await member.guild.create_voice_channel(name = f'Privat({member.display_name})', category = maincategori)
            await channel2.set_permissions(member, connect = True, mute_members = True, move_members = True, manage_channels = True)
            await member.move_to(channel2)
            def check(self, x):
                return len(channel2.members) == 0
            await self.bot.wait_for('voice_channel_update', check = check)
            await channel2.delete()
    else: 
        pass
在我离开频道后,它给出了一个错误:

如果after.channel.id==语音信道:

AttributeError:“非类型”对象没有属性“id”

刚刚检查了一下,我得到的是:

当我进入频道时:

First before: <VoiceState self_mute=False self_deaf=False self_stream=False channel=None>
Second after: <VoiceState self_mute=False self_deaf=False self_stream=False channel=<VoiceChannel id=751363819837718599 name='Voice Channel 1' position=0 bitrate=64000 user_limit=0 category_id=754670529268678669>>
Ignoring exception in on_voice_state_update
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.9/site-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "/app/cogs/commands/moderation/command/private_channel.py", line 43, in on_voice_state_update
print(f'\n Second before: {before.channel.id}\n')
AttributeError: 'NoneType' object has no attribute 'id'
先到先:
第二次之后:
正在忽略语音状态更新中的异常
回溯(最近一次呼叫最后一次):
文件“/app/.heroku/python/lib/python3.9/site packages/discord/client.py”,第312行,在运行事件中
等待coro(*args,**kwargs)
文件“/app/cogs/commands/moderation/command/private_channel.py”,第43行,处于on_voice_state_update状态
打印(f'\n前一秒:{before.channel.id}\n')
AttributeError:“非类型”对象没有属性“id”
出版时:

First before: <VoiceState self_mute=False self_deaf=False self_stream=False channel=<VoiceChannel id=751363819837718599 name='Voice Channel 1' position=0 bitrate=64000 user_limit=0 category_id=754670529268678669>>
First after: <VoiceState self_mute=False self_deaf=False self_stream=False channel=None>
Second before: 751363819837718599
Ignoring exception in on_voice_state_update
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.9/site-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "/app/cogs/commands/moderation/command/private_channel.py", line 44, in on_voice_state_update
print(f'\n Second after: {after.channel.id}\n')
AttributeError: 'NoneType' object has no attribute 'id'
先到先:
先到后:
前二名:751363819837718599
正在忽略语音状态更新中的异常
回溯(最近一次呼叫最后一次):
文件“/app/.heroku/python/lib/python3.9/site packages/discord/client.py”,第312行,在运行事件中
等待coro(*args,**kwargs)
文件“/app/cogs/commands/moderation/command/private_channel.py”,第44行,处于on_voice_state_update状态
打印(f'\n第二个之后:{after.channel.id}\n')
AttributeError:“非类型”对象没有属性“id”

如何解决这一问题?

删除频道时,必须使用
before.channel
而不是
after.channel
就像使用
after.channel
一样,它会给您非类型错误,就好像您删除了一个不可能有after的频道一样

channel = discord.utils.get(guild.voice_channels, name=str(before.channel))
if channel is None:
    return
else:
    if len(channel.members) == 0:
        await channel.delete(reason=None)

删除频道时,必须使用
before.channel
而不是
after.channel
,就像使用
after.channel
一样。它会给您带来非类型错误,就像删除一个不可能有after的频道一样

channel = discord.utils.get(guild.voice_channels, name=str(before.channel))
if channel is None:
    return
else:
    if len(channel.members) == 0:
        await channel.delete(reason=None)

是的,问题是当我进入频道时,一切都正常,但是当我出来的时候,它给出了一个关于我写的窗口的例子,我不能从if
after.channel.id==voice\u channel:
开始检查特定的频道是否初始创建了一个私有频道是的,问题是当我进入该频道时,一切都被创建好了,但是当我出来的时候,它给出了关于我写的窗口的这个,我不能从if
after.channel.id==voice\u channel:
回滚,因为它会检查特定的频道是否是创建私有频道的初始频道