1

I have the following code setup, but it often doesn't find members on my server.
How can I solve this problem?

BOT.on('message', function(message) {
  if (!message.content.startsWith(prefix)) return;
  const args = message.content.slice(prefix.length).split(' ');
  const command = args.shift().toLowerCase();
  const chan = message.channel;

  if (command == "grabreacts" && chan.name == "the-purge") {
    var role = message.guild.roles.find(role => role.name === "2019 Purge Survivor");
    if (!role) return message.channel.send(`**${message.author.username}**, role not found`);
    //console.log(role);

    chan.fetchMessage(args)
      .then(function(value) {
        const react = value.reactions.filter(r => r.emoji == "👍").first();
        var reacts = react.count;
        react.fetchUsers()
          .then(function(users) {
            var fetchedMemebers = users.size;
            users.map(async user => {
              console.log(user.tag);
              message.guild.fetchMember(user, false)
                .then((memeber) => {
                  member.addRole(role)
                    .then((mem) => {
                      react.remove(user)
                        .catch((err) => console.log("Cannot remove user: " + user.tag + ". ex: " + ex + "!"));
                    })
                    .catch(ex => {
                      console.log(ex);
                    });
                })
                .catch((err) => {
                  console.log("Cannot find member object for: " + user.tag + "!");
                });
            });
            var assignedusers = role.members.size;
            message.reply("Reacts found: " + reacts + "; users fetched: " + fetchedMemebers + "; roles assigned: " + assignedusers);
          })
          .catch(err => message.reply("Cannot fetch users for react: '" + reacts + "', err: " + err + "!"));

      })
      .catch(err => message.reply("Cannot fetch message: '" + args + "', err: " + err + "!"));

  }
});

It works fine, up until message.guild.fetchMember(user, false), where I fall into the catch block and print many "Cannot find member object for: ******#****!" messages.

I am not getting back any actual exception blocks, and the code worked for the first 62 or 788 reacts. It seems like if the bot doesn't have the user info cached, even using a false in message.guild.fetchMember(user, boolean) doesn't work.

The user object for that member, however is defined. I need the member object to use member.addRole().

Can someone help me figure out how to get this to work, please? Or what I am doing wrong?

I am running node v11.6.0 with "discord.js": "^11.4.2" as a dependency.

Thanks!

4
  • Does this still not work even if you use message.guild.member()? Commented Jan 9, 2019 at 15:49
  • @FedericoGrandi, I get Cannot find member if I use: var member = message.guild.member(user); if (member != null) { ... }
    – anonymous
    Commented Jan 10, 2019 at 3:37
  • What happends when you try message.guild.fetchMember(user.id, false)? Perhaps the method can't figure out what to do with the complete user object (even though the docs say it should work)
    – T. Dirks
    Commented Jan 10, 2019 at 8:28
  • @T.Dirks, it works fine with the User object for 77/100 of the first fetched users. It's as if the bot is fetching from a cached list of all members and not searching via API. I'll notice if I try to @ certain people on the same server, I'll need to start typing and delete a few times before they are suggested; or I may need to go into manage server and load members that way before I can find them.
    – anonymous
    Commented Jan 11, 2019 at 3:21

1 Answer 1

2

I found that the guild object from BOT.guilds.first() has: members: 656, memberCount: 2470

So, if I do:

        BOT.guilds.first().fetchMembers().then((guild) => {
            ...
        }

That fixes my issue, as the fetchMembers() returns a Promise<Guild> that I can see in the .then() clause, and that Guild object has all members fetched inside of it.

Hope this helps others out!

Not the answer you're looking for? Browse other questions tagged or ask your own question.