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.
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.
Comments