@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:
if after.channel.id == voice_channel:
maincategori = get(member.guild.categories, id = channel_category)
channel2 = await member.guild.create_voice_channel(name = f'Приватный({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
if after.channel.id == voice_channel:
AttributeError: 'NoneType' object has no attribute 'id'
after.channel
становится None когда Вы выходите из канала.if voice_channel is not None and channel_category is not None:
if after.channel.id == voice_channel:
if voice_channel and channel_category and after.channel.id == voice_channel:
else:
pass
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.17.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: def test():
...: for i in range(0, 100):
...: if i%2:
...: 2+2
...: else:
...: pass
...:
In [2]: %timeit test()
14.4 µs ± 559 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [3]: def test():
...: for i in range(0, 100):
...: if i%2:
...: 2+2
...:
In [4]: %timeit test()
13.6 µs ± 435 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
voice_channel_update
в discord.py по умолчанию не существует, и если Вы не вызываете это событие сами, то Вы будете ждать его бесконечно if after.channel is not None and member.voice.channel.id == voice_channel and member.voice.channel is not None:
global channel2
maincategory = get(member.guild.categories, id = channel_category)
channel2 = await member.guild.create_voice_channel(name = f'Привитный {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)
elif after.channel is None and len(channel2.members) == 0:
await channel2.delete()