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

fix: revalidate cached members on GUILD_CREATE #9676

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions packages/discord.js/src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { Collection } = require('@discordjs/collection');
const { makeURLSearchParams } = require('@discordjs/rest');
const { ChannelType, GuildPremiumTier, Routes, GuildFeature } = require('discord-api-types/v10');
const { ChannelType, GuildPremiumTier, Routes, GuildFeature, GatewayIntentBits } = require('discord-api-types/v10');
const AnonymousGuild = require('./AnonymousGuild');
const GuildAuditLogs = require('./GuildAuditLogs');
const GuildPreview = require('./GuildPreview');
Expand All @@ -25,6 +25,7 @@ const RoleManager = require('../managers/RoleManager');
const StageInstanceManager = require('../managers/StageInstanceManager');
const VoiceStateManager = require('../managers/VoiceStateManager');
const DataResolver = require('../util/DataResolver');
const Events = require('../util/Events');
const Status = require('../util/Status');
const SystemChannelFlagsBitField = require('../util/SystemChannelFlagsBitField');
const { discordSort } = require('../util/Util');
Expand Down Expand Up @@ -400,7 +401,28 @@ class Guild extends AnonymousGuild {

if (data.members) {
this.members.cache.clear();
for (const guildUser of data.members) this.members._add(guildUser);
for (const member of data.members) this.members._add(member);

/*
If the GUILD_PRESENCES intent is not present the members sent by GUILD_CREATE and thus to this method,
can't be trusted, and may only contain this bot and users in voice channels. Thus we need to fetch the
members to refill the cache unless of course the GUILD_MEMBERS intent is not present, in which case,
fetching all members is not possible.
*/
if (
!this.client.options.intents.has(GatewayIntentBits.GuildPresences) &&
this.client.options.intents.has(GatewayIntentBits.GuildMembers)
) {
this.members.fetch().catch(err => {
this.client.emit(
Events.Error,
new Error(
`Error while fetching guild members to re-validate the members cache ` +
`after a guild was made available again or a reconnect occurred:\n${err}`,
),
);
});
}
}

if ('owner_id' in data) {
Expand Down