Search code examples
pythondiscorddiscord.py

Voice channel locking and unlocking


I have voice channel lock and unlock commands, but i dont want just anyone to use these commands.

I want to have it to where:

  1. The person who first joins the vc can use the commands
  2. Once that person leaves, it will either go the the next person who joined or reset it for someone else to use it(if that makes sense)

For example, if i joined a vc and left and someone else joins, they will be able to use those commands and i will be no longer able to because i wasnt the first one who joined.

also if anyone can, for channel.overwrites_for() how would i add a role to this?

code so far:

from discord import ui
from discord.ext import commands
from discord import app_commands


class VC(commands.GroupCog):
    def __init__(self, client:commands.Bot):
        self.client = client

    @app_commands.command(name='lock', description='Locks the VC you are currently in')
    async def lock(self, interaction:discord.Interaction):
        channel = interaction.user.voice.channel
        overwrite = channel.overwrites_for(interaction.guild.default_role)
        overwrite.connect = False
        await channel.set_permissions(interaction.guild.default_role, overwrite=overwrite)
        await interaction.response.send_message(content='VC has been locked, only Moderators and Admins can join.')

    @app_commands.command(name='unlock', description='unlocks a VC you are currently in')
    async def unlock(self, interaction:discord.Interaction):
        channel = interaction.user.voice.channel
        overwrite = channel.overwrites_for(interaction.guild.default_role)
        overwrite.connect = True
        await channel.set_permissions(interaction.guild.default_role, overwrite=overwrite)
        await interaction.response.send_message(content='VC has ben unlocked, members can now join the VC.')

async def setup(client:commands.Bot) -> None:
    await client.add_cog(VC(client))


Solution

  • Supposing you have some way of storing for information, such as a dictionary for all the values of each server, you could create something of this sort:

    Main Code:

    data = {}
    
    
    @client.event
    async def on_voice_state_update(member, before, after):
    
        before = before.channel
        after = after.channel
    
        if after: # sees if user joined a voice channel
    
            if not after.members:
    
                # this will execute when the first person joins the channel
    
                data[after.id] = member.id
    
        if before: # sees if user was previously in another channel
    
            if before.id in data: 
    
                if len(before.members) == 1:
    
                    # this will execute when everyone leaves the channel
    
                    del data[before.id]
    
                elif data[before.id] == member.id:
    
                    data[before.id] == <ID OF NEW AUTHOR>
    

    Cog Code:

    @app_commands.command(name='lock', description='Locks the VC you are currently in')
    async def lock(self, interaction:discord.Interaction):
    
        if interaction.user.voice is None:
    
            await interaction.response.send_message(content='You are not in a voice channel.')
    
        channel = interaction.user.voice.channel
    
        if channel.id in data and data[channel.id] == interaction.user.id:
    
            overwrite = channel.overwrites_for(interaction.guild.default_role)
            overwrite.connect = False
    
            await channel.set_permissions(interaction.guild.default_role, overwrite=overwrite)
            await interaction.response.send_message(content='VC has been locked, only Moderators and Admins can join.')
    
        else:
    
            await interaction.response.send_message(content='You are not the author of this channel.')
    
    @app_commands.command(name='unlock', description='unlocks a VC you are currently in')
    async def unlock(self, interaction:discord.Interaction):
    
        if interaction.user.voice is None:
    
            await interaction.response.send_message(content='You are not in a voice channel.')
    
        channel = interaction.user.voice.channel
    
        if channel.id in data and data[channel.id] == interaction.user.id:
    
            overwrite = channel.overwrites_for(interaction.guild.default_role)
            overwrite.connect = True
    
            await channel.set_permissions(interaction.guild.default_role, overwrite=overwrite)
            await interaction.response.send_message(content='VC has ben unlocked, members can now join the VC.')
    
        else:
    
            await interaction.response.send_message(content='You are not the author of this channel.')
    

    Also, for your other question, channel.overwrites_for() works for members, as well as roles.