1

CODE TASK: When entering the main channel, the bot creates a new private voice channel with its own rights. If you exit the channel for 10 seconds, the channel is automatically deleted, BUT if you return to this channel during these 10 seconds, the deletion request is canceled and it continues to work until it remains empty for 10 seconds.

MY PROBLEM: I don't understand how to make the undo delete function.

CODE:

import disnake
from disnake.ext import tasks, commands
from asyncio import sleep   
import asyncio

bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'), help_command=None, intents=disnake.Intents.all())

@bot.event
async def on_voice_state_update(member, before, after):
    if after.channel and after.channel.id == 'YOUR VOICE CHANNEL ID':
        category = after.channel.category
        guild = member.guild
        channel = await guild.create_voice_channel(name=f' {member.display_name}',category = category)
        await channel.set_permissions(member,connect=True,mute_members=True,move_members=True,manage_channels=True)
        await member.move_to(channel)
        def check(x, y, z):
            return len(channel.members) == 0
        await bot.wait_for('voice_state_update', check=check)
        await sleep(10)
        await channel.delete()

Bot.run('TOKEN')

I would appreciate any help

Mike
  • 17
  • 5

1 Answers1

1

You don't really have to cancel anything, you can simply wait_for user to join and put a timeout of 10s, when the timeout is thrown that means the user didn't join, you can then delete the channel.

import asyncio


@bot.event
async def on_voice_state_update(member, before, after):
    channel = after.channel or before.channel
    if channel.id != YOUR_CHANNEL_ID:
        return

    category = after.channel.category if after.channel else before.channel.category
    channel = await member.guild.create_voice_channel(
        name=f' {member.display_name}',
        category=category
    )
    await channel.set_permissions(member, connect=True, mute_members=True, move_members=True, manage_channels=True)
    await member.move_to(channel)

    def check_leave(m, b, a):
        # check for specific user to leave the freshly created channel
        return m == member and b.channel == channel and a.channel is None and len(b.channel.members) == 0

    def check_join(m, b, a):
        # check for specific user to join the freshly created channel
        return m == member and b.channel is None and a.channel == channel and len(a.channel.members) == 0

    while True:
        # wait for user to leave and for the channel to be empty
        await self.bot.wait_for('voice_state_update', check=check_leave)

        try:
            # wait for user to join, if user doesn't join in 10s a timeout error is thrown
            await self.bot.wait_for('voice_state_update', check=check_join, timeout=10.0)
        except asyncio.TimeoutError:
            await channel.delete()  # delete channel, user didn't join in time and break the loop
            break
Łukasz Kwieciński
  • 14,992
  • 4
  • 21
  • 39