Answer the question
In order to leave comments, you need to log in
Creating private voice channels?
Hello, I wrote a code that if you enter a certain voice channel, it will create a new private voice channel, the problem is that after there is no one in the voice channel, it must be deleted, I wrote a code for this, but it does not work.
Here is the code itself:
@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'
Answer the question
In order to leave comments, you need to log in
after.channel
becomes None when you exit the channel.
A couple of notes:
1. Are you Egyptian? If not, what are these pyramids for?
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
event in discord.py does not exist by default, and if you do not call this event yourself, then you will wait for it indefinitely
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()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question