2

So i'm making a command that outputs every user in a role

module.exports = {
    name: 'in-role',
    description: 'Find all users in a specified role.',
    aliases: ['inrole'],
    async execute(message, args, Discord) {
        const role = message.guild.roles.cache.get(args[0])

        if (role) {
            const roleMembers = role.members.map(member => member.toString())
            const inRoleEmbed = new Discord.MessageEmbed()
                .setTitle(role.name)
                .setDescription(roleMembers.join('\n'))
                .setColor(role.hexColor);

            message.channel.send(inRoleEmbed).catch(error => {
                message.channel.send('There was an error executing the command.')
                console.log(error)

            })
        }

    }

};

heres the code, it's only caching the top/first 2 members in the guild. Wondering if there is a way to make it get every member and then output all of the users in that role instead of only looking at top 2 members.

Niko Dev
  • 25
  • 5
  • This question seems to be a duplicate of the question: https://stackoverflow.com/questions/64559390/none-of-my-discord-js-guildmember-events-are-emitting-my-user-caches-are-basica – Tyler2P Nov 30 '20 at 09:24

1 Answers1

1

If you're only getting 2 members in your cache, it seems you haven't yet enabled privileged intents. Find out how to do that.

However, even after enabling intents, you still probably won't have every member of your guild cached, which is when you should use GuildMemberManager#fetch()

// make sure you make your `execute` function async!

await guild.members.fetch();
const role = message.guild.roles.cache.get(args[0]);

...
Lioness100
  • 8,260
  • 6
  • 18
  • 49