discord.js: member.roles.add() doesn't always work

Please describe the problem you are having in as much detail as possible: Sometimes it works and adds the role other times it just doesnt do anything, it does not throw any errors.

Include a reproducible code sample here, if possible:

// MY CODE 
bot.on('guildMemberAdd', member => {
        bot.channels.cache.get('766305878172762203').send('<@'+member.id+'>');
        
        let role = member.guild.roles.cache.get("741484763013185586");

        if (member.roles.cache.find(r => r.name !== "Member")) {
                member.roles.add(role);
                console.log(member.id);
        }
});

Further details:

  • discord.js version: 12.4.0 current and working better than 12.3.1.
  • Node.js version: npm@6.14.8
  • Operating system: Windows 10
  • Priority this issue should have – please be realistic and elaborate if possible: To be honest I think adding roles is a pretty big issue and should be prioritized over most things.

Relevant client options:

  • partials: none
  • gateway intents: none
  • other: none
  • I have also tested the issue on latest master, commit hash:

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 26 (13 by maintainers)

Most upvoted comments

I have a little hack, fetch member with guild.members.fetch({user: id, force: true}) and then try add/remove roles. With this i don’t have any problems

You failed to reproduce it because the issue appear to be present when x members have been reached in that discord server. I assume x to be 1000, but it could be less. The event is fired, but the role is never granted to the client. You can loop it and add the role thousands times, no role will be added. My post on stackoverflow while ago did not brought any answer either.

The creator of this issue already provided a very good reproducible example, all you need to add is 1000 members to your discord server to reproduce it.

The audits shows the bot adding the role, but when you look at the new user’s profile, no role shows up.

That suggests this is a client issue. I have had that issue myself, where the client doesn’t show bot-applied roles. However, API wise the request goes through, is fulfilled and the role accordingly added. There is nothing you (or we, on the library level) can do to influence the viewing client.

This is accordingly not a library issue, and the above mentioned “little hack” completely unrelated to the question or topic at hand.

That won’t help, but I’ve been using Discord.NET ever since and haven’t had the problem again.

Adding the role is an async operation which you have to await. If you just call the function and then immediately after do a check if the member has the role yet, that check will return false as the async network request hasn’t succeeded yet. As this seems to be an issue with understanding/working with Javascript Promises this is a question for the discord server, not the issue tracker.

Edit: I shouldn’t comment while tired lol. But remember that the role add is async, so that’s yet another reason why that if check wouldn’t work anyways. How are you checking that the member actually got the role? Within the bot and/or where? With your discord app? From my experience especially in larger servers the app doesn’t always immediately update the visual roles list of users.