herokudiscord.js

Discord js role.members not return all members


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

screenshot members discord

data in debug

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


Solution

  • 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'
      ]
    });