Discord.js: guildMemberAdd isn't being emitted reliably on already cached users

Created on 30 Oct 2018  ·  13Comments  ·  Source: discordjs/discord.js

Please describe the problem you are having in as much detail as possible:
Discord.js fails to emit the guildMemberAdd event on a user who's just recently been kicked or left the guild on their own will and rejoined shortly after.

I rely on this event heavily to have a guild-specific bot assign a role on joining, which can't be achieved reliably if the event isn't emitted reliably.

From my understanding discord.js fails to free the cached user immediately after the member has been removed which honestly shouldn't block the whole event to not fire at all.

Include a reproducible code sample here, if possible:


this.on('guildMemberAdd', member => {
    if (member.guild.id == "427200060980330508") {

        const memberRole = member.guild.roles.find(role => role.name == "Member 🍍");

        member.addRole(memberRole).then(() => { 
            console.log(Util.getCurrentTime() + " [ SERVER JOIN ] " + member.user.tag + " just got the member role assigned!");
        }).catch(console.log);
    }
});

Further details:

  • discord.js version: 11.4.2
  • Node.js version: 8.11.4
  • Operating system: Ubuntu Server 18.04 LTS
  • Priority this issue should have – please be realistic and elaborate if possible:
    Low to Medium-Low
  • [x] I found this issue while running code on a __user account__
  • [ ] I have also tested the issue on latest master, commit hash:
unverified gateway bug

Most helpful comment

Likely caused by recent changes on discords end regarding intents (and not related to OP)

:gear: Events for privileged intents are not sent if not enabled:
• Discords FAQ on this change: https://dis.gd/gwupdate
• your bot is verified? https://dis.gd/contact (and tell them which privileged intents you need and why)
• your bot is __not__ verified? toggle the switches in https://discord.com/developers/applications (image)
• you do __not__ need to provide intents in client options, discord sends all events if no intents are provided.
• If you are on v11 and experience issues: update to v12, we no longer support v11.

Symptoms you might be experiencing with discord.js:
• member caches are empty (or only have very few entries)
• user cache is empty (or has only very few entries)
• fetching members times out
• login times out if you try to fetch all members on startup
• The client events "guildMemberAdd", "guildMemberRemove", "guildMemberUpdate" do not emit
Guild#memberCount returns count as of ready

All 13 comments

On commit ab3a43919890f0a45812fd91788ea911a106f937, I can't get this to fail. Have you tested this on master?

const Discord = require('discord.js');
const client = new Discord.Client();
client.on('guildMemberAdd', m => m.roles.add(m.guild.roles.find(r => r.name === 'Member')));
client.login('TOKEN');

First things first:
The idea of the reproducible sample would be an isolation of the fail case on a boilerplate bot with no modifications so everyone can run the file and reproduce the fail case.

You assume the fail case to be the event not reliably emitting, so I suggest you test that case as bare bones as possible, removing all the role adds and whatnot.

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('guildMemberAdd', console.log);

client.on('ready', () => {
    console.log('ready');
});

client.login(token);

Run this and I can assure you you will get a GuildMember object logged every single time someone joins, no matter if they shortly left or not.
The issue is not reproducible on 11.4.2 with above sample.
Whatever is causing this anomaly in your case is very likely not a library issue.

Side note:
Unwanted side effects of a join role:

  1. Bypasses verification levels
  2. Invalidates "Prune Members"
  3. Rate limits might become a problem
  4. The server depends on the bot being online at all times (API outages etc.)

Sorry to burst your bubble almostSouji, however I experience the very same behavior I described in full detail in my original posting with the code you provided. Member rejoining after a short period of time don't trigger the guildMemberAdd event.

Sorry to burst your bubble

This makes you sound very unprofessional when trying to convey an actual issue with the library,
also after testing with 5 rejoins (two different users, on last two attempts I restarted bot in-between joins) I can not get this to fail, it always logs.

This seems to be an issue with either your internet, or your code. Most likely the latter.

I'm sorry that I sounded like that, that wasn't my intention. However, the latter can be ruled out as I used his provided code example which in return failed as well. I'm running this on a root server with dedicated hardware and redundant networking failsafes, so neither is it my network. Any other ideas?

Which timeframe are we talking from leave to rejoin?
I could not reproduce with anything between immediate to 5m

This issue seems inactive so I'm going to close it, please reopen if you can still reproduce it and have any more information.

I'm running into the same issue on master (commit hash 7ec0bd9). All events aside from guildMemberAdd and guildMemberRemove are emitting correctly. This is also occurring with the minimal example supplied by almostSouji above:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('guildMemberAdd', console.log);

client.on('ready', () => {
    console.log('ready');
});

client.login(token);

I am using Discord.js version 12.4.1 and Node.js version 15.0.1, and testing on Windows 10 version 1903.

The issue occurs even when the bot has administrator privileges, regardless of whether the user was kicked or left by themselves.

Note that the code worked fine on both my other bot and this one until I changed this one's name and profile picture in the Discord Developer Portal. As soon as I made the changes, without changing any code on either bot, both bots stopped emitting the events. The other bot is not hosted on this computer.

According to the Discord Status website, the API is fully functional at the time of testing.

Things I've tried:

  • Permanent and temporary invite links, with and without limited uses.
  • Kicking and leaving the server.
  • Creating a new application in the Discord Developer Portal, and using that token instead.
  • Deleting package-lock.json and node_modules and running npm install.
  • Creating a new server and doing the same tests.
  • Restarting the process multiple times and closing/opening my console multiple times (obvious, but I'm including it just in case).

Thoroughly confused by this one. It sounds to me like it might be a Discord issue and not a Discord.js issue, but it seems relevant here so I figured I would post it in case anybody smarter than me knows what's up.

Likely caused by recent changes on discords end regarding intents (and not related to OP)

:gear: Events for privileged intents are not sent if not enabled:
• Discords FAQ on this change: https://dis.gd/gwupdate
• your bot is verified? https://dis.gd/contact (and tell them which privileged intents you need and why)
• your bot is __not__ verified? toggle the switches in https://discord.com/developers/applications (image)
• you do __not__ need to provide intents in client options, discord sends all events if no intents are provided.
• If you are on v11 and experience issues: update to v12, we no longer support v11.

Symptoms you might be experiencing with discord.js:
• member caches are empty (or only have very few entries)
• user cache is empty (or has only very few entries)
• fetching members times out
• login times out if you try to fetch all members on startup
• The client events "guildMemberAdd", "guildMemberRemove", "guildMemberUpdate" do not emit
Guild#memberCount returns count as of ready

Thank you.

Likely caused by recent changes on discords end regarding intents (and not related to OP)

Kind of annoying that they just made breaking changes like that, but okay. Thanks for saving me hours of confusion

Not to say anything but those changes were announced months ago

@almostSouji THANK YOU

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Brawaru picture Brawaru  ·  3Comments

Lombra picture Lombra  ·  3Comments

shukriadams picture shukriadams  ·  3Comments

Dmitry221060 picture Dmitry221060  ·  3Comments

DatMayo picture DatMayo  ·  3Comments