Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing presence updates after service restart #10721

Open
junajan opened this issue Jan 21, 2025 · 1 comment
Open

Missing presence updates after service restart #10721

junajan opened this issue Jan 21, 2025 · 1 comment

Comments

@junajan
Copy link

junajan commented Jan 21, 2025

Which package is this bug report for?

discord.js

Issue description

Hello and thank you for this project.

Using discord.js we created a bot connected to server which has over 1.2M members.
In this server we try to listen to all presence updates:

import { Client, Partials } from 'discord.js';

const bot = new Client({
  intents: 257,
  partials: [
    Partials.Channel,
    Partials.GuildMember,
    Partials.GuildScheduledEvent,
    Partials.Message,
    Partials.Reaction,
    Partials.ThreadMember,
    Partials.User,
  ],
});

bot.on('presenceUpdate', handlePresenceUpdate);

bot.on('raw', async (event: { t: string; d: any }) => {
  if (event.t === 'PRESENCE_UPDATE') {
    await handlePresenceUpdateRaw();
  }
});

@ApmMonitor('handlePresenceUpdate', 0.01) // only 1% of calls is recorded via APM
function handlePresenceUpdate() {
}

@ApmMonitor('handlePresenceUpdateRawViaDiscordJS', 0.01) // only 1% of calls is recorded via APM
function handlePresenceUpdateRaw() {
}

It looks like discord.js does not emit all presenceUpdate events at the beginning, even when all partials are turned on because when we start/restart the service, the amount of handlePresenceUpdateRawViaDiscordJS calls is much higher than handlePresenceUpdate calls.

For comparison see handlePresenceUpdateRawViaDiscordJS calls before/after restart:
Image

And handlePresenceUpdate calls during the same period:
Image

Is it possible that discord.js tries to cache some additional resource first before it emits presenceUpdate event?

Code sample

Versions

  • Node.js v20.17.0
  • discord.js 14.15.3
  • os Alpine Linux v3.20

Issue priority

Medium (should be fixed soon)

Which partials do you have configured?

User, Channel, GuildMember, Message, Reaction, GuildScheduledEvent, ThreadMember

Which gateway intents are you subscribing to?

GuildPresences

I have tested this issue on a development release

No response

@Qjuh
Copy link
Contributor

Qjuh commented Feb 16, 2025

  • the raw gateway event PRESENCE_UPDATE emits when either the presence or the user itself is updated. Discord.js separates those two into emitting presenceUpdate only for actual changes in presence, userUpdate if the user changed but presence remained as is
  • as you can see in https://github.com/discordjs/discord.js/blob/v14/packages/discord.js/src/client/actions/PresenceUpdate.js there are a few checks done before presenceUpdate gets emitted. But with User and GuildMember partial the only check that would prevent the event being emitted is the check for presence not being changed compared to the one in cache
  • Since on login all presences for online members are sent in the initial GUILD_CREATE payload which discord.js waits for before emitting READY (and before emitting other events) the presences would already be in cache to be compared against.

So it would be helpful if you were able to get information about the payload contained in the raw events that don’t emit the djs event to find out if this is intended behavior or an actual bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants