Get percentage of online members on Discord Server

Viewed 246

I am able to display users online and offline when a certain command is used. I would like to add another field to my embed with the percentage of members online (e.g 73% of the members are online). I understand the logic behind it, I am just having trouble with the syntax. I haven't been able to find anything online that would work with my code. Thank you in advance!

module.exports = {
    users: function(message) {
        let guild = client.guilds.cache.get('727640959634112553');
        const Embed = new Discord.MessageEmbed();
        Embed.setTitle(`Server Activity`);
        Embed.addField("Online Members", message.guild.members.cache.filter(member => member.presence.status !== "offline").size);
        Embed.addField("Offline Members", message.guild.members.cache.filter(member => member.presence.status == "offline").size);
        return message.channel.send(Embed);
    }
}
1 Answers

Since you already have the 2 required variables, the number of online members and the number of offline members (which should always be higher or equal to the number of online members), we can use the following formula:

(A * 100) / B

Where A is the number of online members, and B is the number of total members.


const percentageDiff = (A, B) => {return Math.floor((A * 100) / B)};

message.channel.send(`${percentageDiff(message.guild.members.cache.filter(member => member.presence.status != "offline").size, message.guild.memberCount)}% of the members are online.`);
Related