Z
Z
Zakhar2020-10-14 20:17:27
Python
Zakhar, 2020-10-14 20:17:27

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

After I left the channel, it gives an error:


if after.channel.id == voice_channel:
AttributeError: 'NoneType' object has no attribute 'id'


How can this be fixed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Nevzorov, 2020-10-15
@xzartsust

after.channelbecomes 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:

In this context, exactly the same as
if voice_channel and channel_category and after.channel.id == voice_channel:

2. This piece of code is absolutely useless:
else:
    pass

And in fact, it only increases the execution time:
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)

3. The voice_channel_updateevent in discord.py does not exist by default, and if you do not call this event yourself, then you will wait for it indefinitely

Z
Zakhar, 2020-10-16
@xzartsust

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 question

Ask a Question

731 491 924 answers to any question