Home > Software design >  Discord JS v13 Online user counter
Discord JS v13 Online user counter

Time:03-17

Im using this code below to update a message with server statistic. First it works correct, but later "new users" appear(you can see it at this screenshot)

setInterval(async () => {
    await guildID.members.fetch().then(async Users => {
    //some code//
    embed.setField('Activity', `
        ${emojiget.get(emojisstatus[0])} | ${Users.filter(member => member.presence?.status == "online").size}
        ${emojiget.get(emojisstatus[1])} | ${Users.filter(member => member.presence?.status == "idle").size}
        ${emojiget.get(emojisstatus[2])} | ${Users.filter(member => member.presence?.status == "dnd").size}
        ${emojiget.get(emojisstatus[3])} | ${Users.filter(member => !member.presence).size}
        Всего : ${Users.size}
    `)
    })
    await channel.messages.edit('940364414262071377', {embeds: [embed]})
}, 120000)

CodePudding user response:

From the official documentation, it seems there is no more the attribute presence in the User class.

Make it sense also for you? or I'm missing something?

CodePudding user response:

First, the guildID.members is wrong syntax You need to fetch the guild or if you are sharding it will automatically fetched so you just get it from the cache

  • Fetch: const guild = await client.guilds.fetch('Guild ID'); -> return Guild class so from the guild fetched we can fetch all members: guild.members.fetch() -> return a Collection<Snowflake, GuildMember>

Second, there is no setField() method for discord.js messageEmbed class, replace it with setFields() to set multiple fields in an embed(max 15). Example:

const { messageEmbed } = require('discord.js');
const embed = new messageEmbed()
                    .setFields(
                        [
                            {
                                name: 'Field name',
                                value: 'Field value',
                                inline: false,
                            },
                        ]
                    );

Last, GuildMember has a presence property even if they are offline, so get the offline members from presence.status: Users.filter(member => member.presence.status == "offline").size

From all of those now we have the final code:

setInterval(async () => {
    await guildID.members.fetch().then(async Users => {
        // some code//
        embed.setFields(['Activity', `
        ${emojiget.get(emojisstatus[0])} | ${Users.filter(member => member.presence.status == 'online').size}
        ${emojiget.get(emojisstatus[1])} | ${Users.filter(member => member.presence.status == 'idle').size}
        ${emojiget.get(emojisstatus[2])} | ${Users.filter(member => member.presence.status == 'dnd').size}
        ${emojiget.get(emojisstatus[3])} | ${Users.filter(member => member.presence.status === 'offline').size}
        Всего : ${Users.size}
    `]);
    });
    await channel.messages.edit('940364414262071377', { embeds: [embed] });
}, 120000);

Hope this helps you, comment on this answer if you still have any issue about it

  • Related