fix: GuildMemberManager#fetchByMemberSafety

This commit is contained in:
Elysia 2024-01-30 16:02:25 +07:00
parent f99260266a
commit 08d52aa677

View File

@ -503,13 +503,17 @@ class GuildMemberManager extends CachedManager {
fetchByMemberSafety(timeout = 15_000) { fetchByMemberSafety(timeout = 15_000) {
return new Promise(resolve => { return new Promise(resolve => {
const nonce = SnowflakeUtil.generate(); const nonce = SnowflakeUtil.generate();
const fetchedMembers = new Collection();
let timeout_ = setTimeout(() => { let timeout_ = setTimeout(() => {
this.client.removeListener(Events.GUILD_MEMBER_LIST_UPDATE, handler); this.client.removeListener(Events.GUILD_MEMBERS_CHUNK, handler);
resolve(this.guild.members.cache); resolve(fetchedMembers);
}, timeout).unref(); }, timeout).unref();
const handler = (members, guild, raw) => { const handler = (members, guild, chunk) => {
if (guild.id == this.guild.id && raw.nonce == nonce) { if (guild.id == this.guild.id && chunk.nonce == nonce) {
if (members.size > 0) { if (members.size > 0) {
for (const member of members.values()) {
fetchedMembers.set(member.id, member);
}
this.client.ws.broadcast({ this.client.ws.broadcast({
op: Opcodes.SEARCH_RECENT_MEMBERS, op: Opcodes.SEARCH_RECENT_MEMBERS,
d: { d: {
@ -521,12 +525,12 @@ class GuildMemberManager extends CachedManager {
}); });
} else { } else {
clearTimeout(timeout_); clearTimeout(timeout_);
this.client.removeListener(Events.GUILD_MEMBER_LIST_UPDATE, handler); this.client.removeListener(Events.GUILD_MEMBERS_CHUNK, handler);
resolve(this.guild.members.cache); resolve(fetchedMembers);
} }
} }
}; };
this.client.on('guildMembersChunk', handler); this.client.on(Events.GUILD_MEMBERS_CHUNK, handler);
this.client.ws.broadcast({ this.client.ws.broadcast({
op: Opcodes.SEARCH_RECENT_MEMBERS, op: Opcodes.SEARCH_RECENT_MEMBERS,
d: { d: {