Get all users with specific role using discord.js

Viewed 74

I'm getting RangeError [BITFIELD_INVALID]: Invalid bitfield flag or number: undefined. with this code. Can someone help me with what's undefined here?

I'm using discord.js v13.6.0.

require('dotenv').config();
const token = process.env.bot_token;

const { Client, Intents } = require("discord.js")
const client = new Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
    Intents.NON_PRIVILEGED,
    Intents.GUILD_MEMBERS
  ]
});

const prefix = '!getUserByRole';

client.on("messageCreate", (message) =>{
    if(!message.content.startsWith(prefix) || message.author.bot || message.author.id) return;
    const args = message.content.slice(prefix.length).split(/ +/);
    const roleId = args[1];
    message.guild.members.fetch().then((members)=>{
        members.filter(mmbr => mmbr.roles.cache.get(roleId)).map(m => {
            console.log(m.user.tag, m.user.id);
        });
    })
})

client.login(token);
1 Answers

In discord.js v13 Intents.ALL, Intents.NON_PRIVILEGED, and Intents.PRIVILEGED have all been removed to discourage bad practices. It means you need to remove Intents.NON_PRIVILEGED.

Also, there is no Intents.GUILD_MEMBERS, it should be Intents.FLAGS.GUILD_MEMBERS. So you'll need to update your code like this:

const client = new Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
    Intents.FLAGS.GUILD_MEMBERS,
  ],
});
Related