1

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)
Erik Russell
  • 793
  • 1
  • 9
  • 19
Kausar Ahmed
  • 31
  • 1
  • 7
  • `guild` is undefined. Make sure that the ID is correct and that the client is in the desired guild. – slothiful Aug 02 '19 at 20:56
  • To add to @slothiful 's comment, you may want to consider using the guild of the message that called the command. This may help: https://stackoverflow.com/questions/48273185/discord-js-guild-id-is-undefined-even-though-definition-is-there – Chase Ingebritson Aug 02 '19 at 21:01

1 Answers1

0

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);
    });