2

I'm using discord.js version 12, the problem is that the bot is only selecting 2 users out of the entire server (The author of the message and the bot) and I want it to be able to pick anybody from the server.

Here's the code that I've tried:

client.on('message', msg=>{
  if(msg.content.toLowerCase() === "!sru"){
    const randomUser = msg.guild.members.cache.random();
    msg.channel.send(`${randomUser.user.username}#${randomUser.user.discriminator}`);
  }
})

Thanks in advance.

1 Answers1

1
client.on('message', msg=>{
  if(msg.content.toLowerCase() === "!sru"){
    const randomUser = msg.guild.members.cache.random();
   msg.channel.send(`${randomUser.user.username}#${randomUser.user.discriminator}`);
  }
})

Make sure you have your SERVER MEMBERS INTENT in the developer portal activated.

Applications -> Bot -> Server Members Intent

Jannik Schmidtke
  • 1,257
  • 2
  • 5
  • 15
  • Thanks, but this didn't work. The bot still only selects 2 people. – AnimatingCoder Jan 12 '21 at 10:46
  • 1
    @AnimatingCoder maybe you deleted not everything from your old code or something like this. The code in my answer works without problems. – Jannik Schmidtke Jan 12 '21 at 10:49
  • 1
    @AnimatingCoder remove your old code and replace it with my answers code. – Jannik Schmidtke Jan 12 '21 at 10:50
  • 2
    This is happening because the other users are not cached, try and get the users to send messages in order to cache them. Sometimes it takes a little while in a new server – Joe Moore Jan 12 '21 at 12:30
  • 1
    After you've enabled the intents in the developer portal you also need to use them when configuring the client. This [question](https://stackoverflow.com/questions/64559390) should help you. – Jakye Jan 12 '21 at 14:26
  • @Jakye Yes, that worked. Apparently I forgot to change `const client = new Discord.Client();` to `const client = new Discord.Client({ ws: { intents: new Discord.Intents(Discord.Intents.ALL) }});`. Thanks for the help. – AnimatingCoder Jan 13 '21 at 07:40