How can i make my inrole command cache every member in the guild

Niko Dev

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.

Lioness100

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]);

...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I unban every member from a guild

Discord.js how can I create an invite for every guild my bot is in?

How can I make every member in python array starts with zero?

How can I implement a !leaveguild <guildID> command (that leaves the specified guild) into my Discord (discord.js) bot?

how can I get current guild ID in on_member_join

How to remove a role from every member of the guild

Discord.js How to make a command to get every member in my voice channel?

How can i make my caroutine run in every x seconds

How to lock every channel of a guild with a command?

Discord.py | How can I make the bot tag the owner of the guild on joining the guild?

How can I override flags in my Makefile by modifying the make command

How can I make my HTML command box work with JavaScript?

How can i make an "uptime" command for my discord bot? (PYTHON)

How to get member count of all members in every guild the bot is in

How can I make GNU screen run a command or script every time I create a new window?

How can I make my for loop run through every number in my list test_cases?

How can I show every voice channel ID/name of a guild with DiscordJS?

Discord.js bot help needed - how can I make my bot auto do a task every 6 hours without sending it a command?

How can i make it so that a member has to ping another member in a ,user command using discord.js v14

LNK2001 in "How can I make a WNDPROC or DLGPROC a member of my C++ class?"

How can I run a gcloud SDK command to invalidate a Cloud CDN cache remotely from my backend server?

Discord.py: How can I make a command that i can only do with my user ID?

How can I make my PowerShell script wait for the first command to finish and then run the second command

How can I get a discord.Guild object that is 'complete' (i.e. has Member.activity and Member.voice per member)? (discord.py)

How can I make my mouse auto-click every 5 seconds?

How can I make sure ASDF loads a dynamic foreign library every time my system is loaded?

How can I make a variable "refresh" itself every time my JavaScript file has code pulled from it?

How can I make a click listener for every Item I have in my card view inside my Recycler View

How I can get guild.id