创建私人频道并在不和谐中删除它们

发布于 2025-02-12 07:51:31 字数 2569 浏览 0 评论 0原文

我想制作一个系统,当用户进入私人创建频道时,创建文本通道和语音频道时,创建者可以赋予并剥夺进入私人的权利,并且所有者离开私人,然后在一分钟内删除通道以及数据库中有关它们的数据。我使用mongodb。

我几乎完成了所有代码,但是删除频道的代码不起作用,即检查频道ID。我还想确保如果创作者返回,则取消了30的计时器,但是还有一些更方便的方法来创建此计时器。

@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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凯凯我们等你回来 2025-02-19 07:51:32
  1. 您在异步代码中使用正常睡眠。使用异步睡眠。
  2. 尝试使用discord.ext.tasks创建计时器。而不是睡觉。

和通道删除。这应该有效:

@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})
  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})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文