简体   繁体   中英

get count of users in a voice channel? Discord.js

How can I get the count of users in a voice channel? I have a system that counts the time of users in the voice channel, I want it to work if there are more than two people in the voice channel, please tell me. Code:

exports.execute = async (oldState, newState) => {
    if ((oldState.member && oldState.member.user.bot) || (newState.member && newState.member.user.bot)) {
        return;
    }

    if (!oldState.channelID && newState.channelID) {
        Activites.set(oldState.id, Date.now());
    }

    let data;

    if (!Activites.has(oldState.id)) {
        data = Date.now();
        Activites.set(oldState.id, data);
    }
    else {
        data = Activites.get(oldState.id);
    }

    let duration = Date.now() - data;

    if (oldState.channelID && !newState.channelID) {
        Activites.delete(oldState.id);
        vt.add(`stats.${oldState.guild.id}.${oldState.id}.channels.${oldState.channelID}`, duration);
        vt.set(`stats.${oldState.guild.id}.${oldState.id}.activity`, Date.now());
    }
    else if (oldState.channelID && newState.channelID) {
        Activites.set(oldState.id, Date.now());
        vt.add(`stats.${oldState.guild.id}.${oldState.id}.channels.${oldState.channelID}`, duration);
        vt.set(`stats.${oldState.guild.id}.${oldState.id}.activity`, Date.now());
    }
};

First, you get the channel you want, then you can get the size of the members property of it.

let voiceChannel = client.guilds.cache.get("guild id").channels.cache.get("channel id");
let membersInChannel = voiceChannel.members.size;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM