Natalie Faaded
Natalie Faaded

Reputation: 43

Get number of users in voice channel

I am rewriting the music portion of my friends discord bot. I am trying to figure out how to get the number of users in the voice channel of the person who executed the command. I have looked everywhere but I can't seem to find it or its usage. Right now I am using the following:

module.exports.run = async (client, message, args) => {
    
  if (!message.member.roles.cache.find(role => config["dj_role"] === string.toLowerCase(role.name))) {
    if (!message.member.hasPermission('MANAGE_GUILD' || 'MANAGE_ROLES' || 'ADMINISTRATOR')) {
        if (!message.member.id == message.guild.ownerID) {
            let count = 0;
            count += voiceChannel.members.size;
            if (count > 3){
                return message.channel.send(client.msg["rejcted_dj"].replace("[ROLE_DJ]", config["dj_role"]));
            }
        }
    }
}
  if (!message.member.voice.channel){return message.channel.send(client.msg["music_channel_undefined"])}
//play music part
}

Upvotes: 4

Views: 5821

Answers (1)

Jakye
Jakye

Reputation: 6625

You're pretty close. You already got the voice channel of the command author:

if (!message.member.voice.channel){return message.channel.send(client.msg["music_channel_undefined"])}

You can use the VoiceChannel.members.size to get how many members are in the channel.

Here's an example:

const GuildMember = new Discord.GuildMember(); // This shall be the command author.

if (GuildMember.voice.channel) {
    console.log(GuildMember.voice.channel.members.size);
};

Upvotes: 4

Related Questions