I have created a bot that allows, among other things, to send a message to those who belong to a certain role. The bot is hosted on Heroku (free version, with a mandatory restart every day) When I try to retrieve the members of the role in question I have the impression to retrieve only the members who are connected since the bot restart (or who have been online). I have 27 members of a role but the bot retrieves only 3
Here is my code :
client.on("messageCreate", message =>{
message.guild.roles.fetch(roleMembersId).then(role => {
role.members.forEach(member => {
console.log("user " member.user.username)
})
})
})
I have the same behavior if I use interactions or directly the client
Is there a solution to this problem? I did not find anything like this when I searched
CodePudding user response:
Perhaps you're missing the GUILD_PRESENCES
intent.
For discord.js v14:
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildPresences
]
})
For lower versions you can try and use:
const Discord = require("discord.js");
const client = new Discord.Client({
intents: [
'GUILDS','GUILD_MESSAGES','GUILD_PRESENCES'
]
});
CodePudding user response:
Role#members
only returns cached members, as you can see in the description, and source. You can fetch all members (and cache them) first, before using it
await message.guild.members.fetch()
const members = message.guild.roles.cache.get(roleId).members
members.forEach(m => console.log(`user ${m.user.username}`))