Search code examples
discorddiscord.js

Check if member is in private voice channel or check if bot doesn’t have perms to join private voice channel and send error discord.js


Im making a music bot and i need to make an error that check if the member is in a private voice channel or if the bot doesn’t have perms for joining the private channel

I have tried this code:


interaction.guild.members.me.permissions.has(PermissionsBitField.Flags.ManageMessages)

But i don’t what permission i need for the bot to join the private voice channel so i don't know what to change the ManageMessages perm to

I have check the docs, stack even youtube cant find solution


Solution

  • You can use the joinable property

    if (!interaction.member?.voice.channel?.joinable) {
        // Cant join voice channel
    }
    

    Or if you need to check the permissions you can use the permissionsFor method

    if (!interaction.member?.voice.channel?.permissionsFor(inteaction.guild.members.me).has(PermissionsBitField.Flags.Connect)) {
        // Dont have the permissions to connect to voice channel
    }
    

    Note: the ? symbols are important because the interaction.member and member.voice.channel objects are nullable. If either are, the statement is equivalent to !null, or true. This makes sense because there is no member/channel, so there is no channel to join in the first place.