【发布时间】:2021-01-29 05:58:59
【问题描述】:
我写了一个代码,如果你去某个语音频道会创建一个新的私人语音频道,问题是语音频道中没有人之后,它应该被删除,我为此写了一个代码,但它不起作用。
这是代码本身:
@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'
如何解决这个问题?
【问题讨论】:
标签: python discord bots discord.py discord.py-rewrite