Home > Blockchain >  Creating private channels and deleting them in the discord
Creating private channels and deleting them in the discord

Time:07-02

I want to make a system where when a user enters the private creation channel, a text channel and a voice channel are created, in the text the creator can give and take away the rights to enter the private, and if the owner leaves the private, then in a minute the channels should be deleted, as well as data about them from the database. I use MongoDB.

I have done almost all the code, but the code with the removal of channels does not work, namely, checking for the channel id. I also want to make sure that the timer for 30 is canceled if the creator returns, but there is some more convenient way to create this timer.

@bot.event
async def on_voice_state_update(member, before, after):
       if after.channel is not None:
        if after.channel.id == 992120556256247808:
            guild = bot.get_guild(642681537284014080)
            category = discord.utils.get(guild.categories, name='Приват')
            v_channel = await guild.create_voice_channel(name=f'Приват ({member.display_name})', category=category)
            t_channel = await guild.create_text_channel(name=f'Выдача прав ({member.display_name})', category=category)

            await v_channel.set_permissions(member, connect=True, speak=True, view_channel=True, stream=True, kick_members=True, mute_members=True, priority_speaker=True)
            role = discord.utils.get(guild.roles, id=642681537284014080)
            await v_channel.set_permissions(role, view_channel=False)
            await t_channel.set_permissions(role, view_channel=False)

            private_post = {
                '_id': member.id,
                'text_id':t_channel.id,
                'voice_id':v_channel.id,
            }
            private.insert_one(private_post)
            await member.move_to(v_channel)

            if before.channel == v_channel: #This one and the lines below it don't work
                await bot.get_channel(private.find_one({'_id':member.id})['text_id']).send(f'`[PRIVATE]`: {member.mention}, Your private will be deleted in 1 minute!')
                time.sleep(60000)
                await t_channel.delete()
                await v_channel.delete()
                roles.delete_one({'_id': member.id})

@bot.command()
async def perm(ctx, member: discord.Member):
    if ctx.channel.id == private.find_one({'text_id': ctx.channel.id})['text_id']:
        v_channel = bot.get_channel(private.find_one({'text_id': ctx.channel.id})['voice_id'])
        await v_channel.set_permissions(member, connect=True, speak=True, view_channel=True, stream=True)

@bot.command()
async def unperm(ctx, member: discord.Member):
    if ctx.channel.id == private.find_one({'text_id': ctx.channel.id})['text_id']:
        v_channel = bot.get_channel(private.find_one({'text_id': ctx.channel.id})['voice_id'])
        await v_channel.set_permissions(member, connect=False, speak=False, view_channel=False, stream=False)
        if member in v_channel.members:
            await member.move_to(None)

CodePudding user response:

  1. you're using normal sleep in async code. use async sleep.
  2. try using discord.ext.tasks to create timer. instead of sleep.

and the channel deletion. This should work :

@bot.event
async def on_voice_state_update(member, before, after):
    if after.channel is not None:
        if after.channel.id == 992120556256247808:
            guild = bot.get_guild(642681537284014080)
            category = discord.utils.get(guild.categories, name='Приват')
            v_channel = await guild.create_voice_channel(name=f'Приват ({member.display_name})', category=category)
            t_channel = await guild.create_text_channel(name=f'Выдача прав ({member.display_name})', category=category)

            await v_channel.set_permissions(member, connect=True, speak=True, view_channel=True, stream=True, kick_members=True, mute_members=True, priority_speaker=True)
            role = discord.utils.get(guild.roles, id=642681537284014080)
            await v_channel.set_permissions(role, view_channel=False)
            await t_channel.set_permissions(role, view_channel=False)

            private_post = {
                '_id': member.id,
                'text_id': t_channel.id,
                'voice_id': v_channel.id,
            }
            private.insert_one(private_post)
            await member.move_to(v_channel)

    if before.channel is not None:
        channels = private.find_one({'_id':member.id})
        t_channel = bot.get_channel(channels['text_id'])
        v_channel = bot.get_channel(channels['voice_id'])
        if before.channel.id != v_channel.id:
            return
        await t_channel.send(f'`[PRIVATE]`: {member.mention}, Your private will be deleted in 1 minute!')
        await asyncio.sleep(60000)
        await t_channel.delete()
        await v_channel.delete()
        roles.delete_one({'_id': member.id})
  • Related