Aashish_6377
Aashish_6377

Reputation: 11

How to check if members having a particular role are present in a particular voice channel in discord.js

I'm trying to find if any member having a particular role id is present in a particular voice channel. If even 1 member having the particular role id is present in that particular voice channel, then only those members can use the commands of music bot. From my research, I have came to know that voiceStateUpdate may help but the problem is I don't know how to use it in my codes(cause I am new to JavaScript). Here is the link to the documentation: https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-voiceStateUpdate . Here is a part of my code:

client.on('voiceStateUpdate', (oldMember, newMember) => {
  });
client.on('message', async message => {
    if (message.author.bot) return
    if (!message.content.startsWith(prefix)) return

    let messageArray = message.content.split(" ");
    let cmd = messageArray[0];
    const args = message.content.substring(prefix.length).split(' ');
    const searchString = args.slice(1).join(' ')
    const url = args[1] ? args[1].replace(/<(._)>/g, '$1') : ''
    const serverQueue = queue.get(message.guild.id)
   
 if() {        //the conditional statement I am trying to put here but I don't know how to do it properly
    if (message.content.startsWith(`${prefix}play`)) {
        const voiceChannel = message.member.voice.channel
        if (!voiceChannel) return message.channel.send("You need to be in a voice channel to play music")
    .......

So the main thing is that I don't know what to write their exactly in the if statement that would make my code work properly.

Upvotes: 1

Views: 457

Answers (2)

user16138548
user16138548

Reputation:

So from what I can understand, you want the command to only execute when a user with the role 'Example Role' is in the channel 'Example Channel'

For this you will need the role id and the channel id.

client.on("message", async message => {
    if (message.author.bot) return;
    if (!(message.guild)) return;

    var roleID = 'RoleID';
    var vc = message.guild.channels.cache.get("VoiceChannelID");

    if (message.content.toLowerCase() == "test") {

        canUse = false;
        vc.members.forEach((member) => {
            if (member.roles.has(roleID)) {
                canUse = True;
            }
        })
        if (!(canUse)) { // nobody in the voice channel has the role specified.
            return;
        }
        console.log("A member of the voice channel has the role specified")
    }
});

Upvotes: 0

Jakye
Jakye

Reputation: 6625

client.on("message", async message => {
    // Checking if the message author is a bot.
    if (message.author.bot) return false;
    // Checking if the message was sent in a DM channel.
    if (!message.guild) return false;

    // The role that can use the command.
    const Role = message.guild.roles.cache.get("RoleID");
    // The voice channel in which the command can be used.
    const VoiceChannel = message.guild.channels.cache.get("VoiceChannelID");

    if (message.content.toLowerCase() == "test") {
        // Checking if the GuildMember is in a VoiceChannel.
        if (!message.member.voice.channel) return message.reply("You need to be in a voice channel.");
        // Checking if the GuildMember is in the required VoiceChannel.
        if (message.member.voice.channelID !== VoiceChannel.id) return message.reply("You are in the wrong VoiceChannel.");
        // Checking if the GuildMember has the required Role.
        if (!message.member.roles.cache.has(Role.id)) return message.reply("You are not allowed to execute this command.");

        // Execute your command.
        return message.reply("Command executed");
    };
});

Upvotes: 1

Related Questions