From f16549108383721b227a89d474d79f779b23d314 Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Sun, 17 Apr 2022 17:21:54 +0700 Subject: [PATCH] GuildMember.fetch() fix --- src/client/Client.js | 5 ++++- .../handlers/GUILD_MEMBER_LIST_UPDATE.js | 2 +- src/managers/GuildMemberManager.js | 17 +++++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/client/Client.js b/src/client/Client.js index 81b0ebd..41385fb 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -1,7 +1,7 @@ 'use strict'; const process = require('node:process'); -const { setInterval } = require('node:timers'); +const { setInterval, setTimeout } = require('node:timers'); const { Collection } = require('@discordjs/collection'); const RichPresence = require('discord-rpc-contructor'); const BaseClient = require('./BaseClient'); @@ -633,6 +633,9 @@ class Client extends BaseClient { } } + sleep(miliseconds) { + return new Promise(r => setTimeout(r, miliseconds)); + } /** * Validates the client options. * @param {ClientOptions} [options=this.options] Options to validate diff --git a/src/client/websocket/handlers/GUILD_MEMBER_LIST_UPDATE.js b/src/client/websocket/handlers/GUILD_MEMBER_LIST_UPDATE.js index 713f620..57da3fc 100644 --- a/src/client/websocket/handlers/GUILD_MEMBER_LIST_UPDATE.js +++ b/src/client/websocket/handlers/GUILD_MEMBER_LIST_UPDATE.js @@ -21,7 +21,7 @@ module.exports = (client, { d: data }) => { } } } else if (object.op == 'INVALIDATE') { - console.warn(`Invalidate [${object.range[0]}, ${object.range[1]}]`); + client.emit(Events.DEBUG, `Invalidate [${object.range[0]}, ${object.range[1]}]`); } else if (object.op == 'UPDATE' || object.op == 'INSERT') { const member = object.item.member; if (!member) continue; diff --git a/src/managers/GuildMemberManager.js b/src/managers/GuildMemberManager.js index 0b23992..3127e6f 100644 --- a/src/managers/GuildMemberManager.js +++ b/src/managers/GuildMemberManager.js @@ -415,7 +415,9 @@ class GuildMemberManager extends CachedManager { query, time = 120e3, nonce = SnowflakeUtil.generate(), + sleepTime = 2e3, // 2 seconds } = {}) { + let type; return new Promise((resolve, reject) => { if (!query && !user_ids) query = ''; if (nonce.length > 32) throw new RangeError('MEMBER_FETCH_NONCE_LENGTH'); @@ -425,6 +427,7 @@ class GuildMemberManager extends CachedManager { this.guild.me.permissions.has('BAN_MEMBERS') || this.guild.me.permissions.has('MANAGE_ROLES') ) { + type = 8; // This is opcode this.guild.shard.send({ op: Opcodes.REQUEST_GUILD_MEMBERS, d: { @@ -437,6 +440,7 @@ class GuildMemberManager extends CachedManager { }, }); } else { + type = 14; // This is opcode let channel; let channels = this.guild.channels.cache.filter(c => c.isText()); channels = channels.filter(c => c.permissionsFor(this.guild.me).has('VIEW_CHANNEL')); @@ -446,7 +450,8 @@ class GuildMemberManager extends CachedManager { const channels_allowed_everyone = channels.filter(c => c.permissionsFor(this.guild.roles.everyone).has('VIEW_CHANNEL'), ); - channel = channels_allowed_everyone.first() ?? channels.first(); + channel = channels_allowed_everyone.random() ?? channels.random(); + console.log(channel); // Create array limit [0, 99] const list = []; const allMember = this.guild.memberCount; @@ -500,7 +505,7 @@ class GuildMemberManager extends CachedManager { let i = 0; const handler = (members, _, chunk) => { timeout.refresh(); - if (chunk.nonce !== nonce) return; + if (chunk.nonce !== nonce && type === 8) return; i++; for (const member of members.values()) { fetchedMembers.set(member.id, member); @@ -510,9 +515,13 @@ class GuildMemberManager extends CachedManager { this.client.removeListener(Events.GUILD_MEMBERS_CHUNK, handler); this.client.removeListener(Events.GUILD_MEMBER_LIST_UPDATE, handler); this.client.decrementMaxListeners(); - let fetched = fetchedMembers; + let fetched = fetchedMembers.size < this.guild.members.cache.size ? this.guild.members.cache : fetchedMembers; if (user_ids && !Array.isArray(user_ids) && fetched.size) fetched = fetched.first(); - resolve(fetched); + if (type == 14) { + this.guild.client.sleep(sleepTime).then(() => resolve(fetched)); + } else { + resolve(fetched); + } } }; const timeout = setTimeout(() => {