Search code examples
javascriptdiscorddiscord.jscommando

How do I list all users with a certain role with the role's ID? I'm using Discord.js Commando


I get an error 'TypeError: Cannot read property 'roles' of undefined'

The command I'm trying to make should list all the users with a certain role in the main server. This is a public bot too so it's in multiple servers. The code I'm currently using is from an answer to a question similar to the one I'm asking.

const guild = this.client.guilds.get('498683894489546762');

const allTrusted = guild.roles.get('498686419137724417').members.map(m=>m.user.tag).join('\n');
      const embed = new RichEmbed()
      .setDescription(allTrusteds);      
      msg.say(embed)

Solution

  • Even though the question was asked 9 months ago, I still wanted to answer.

    Assuming you've updated to djs^12 This is what you should use:

        const Discord = require('discord.js');
        msg.guild.members.fetch()
        .then(members => {
            const allTrusteds = members.filter(mmbr => mmbr.roles.cache.get('498686419137724417')).map(m => m.user.tag).join('\n')
            const embed = new Discord.MessageEmbed()
            .setDescription(allTrusteds);
            msg.say(embed);
        });