创建私人语音频道,并在该频道的成员为0时删除此频道

时间:2020-10-14 19:01:26

标签: 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" 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 == voice_channel:

AttributeError:'NoneType'对象没有属性'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'

什么时候出来:

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'

如何解决?

1 个答案:

答案 0 :(得分:0)

删除频道时,您必须使用before.channel而不是after.channel,就像您使用after.channel一样,它会给您带来NoneType错误,就像您删除频道一样,之后。

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)