GuildMember.fetch() fix

This commit is contained in:
March 7th 2022-04-17 17:21:54 +07:00
parent 44a7f4cb60
commit f165491083
3 changed files with 18 additions and 6 deletions

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const process = require('node:process'); const process = require('node:process');
const { setInterval } = require('node:timers'); const { setInterval, setTimeout } = require('node:timers');
const { Collection } = require('@discordjs/collection'); const { Collection } = require('@discordjs/collection');
const RichPresence = require('discord-rpc-contructor'); const RichPresence = require('discord-rpc-contructor');
const BaseClient = require('./BaseClient'); 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. * Validates the client options.
* @param {ClientOptions} [options=this.options] Options to validate * @param {ClientOptions} [options=this.options] Options to validate

View File

@ -21,7 +21,7 @@ module.exports = (client, { d: data }) => {
} }
} }
} else if (object.op == 'INVALIDATE') { } 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') { } else if (object.op == 'UPDATE' || object.op == 'INSERT') {
const member = object.item.member; const member = object.item.member;
if (!member) continue; if (!member) continue;

View File

@ -415,7 +415,9 @@ class GuildMemberManager extends CachedManager {
query, query,
time = 120e3, time = 120e3,
nonce = SnowflakeUtil.generate(), nonce = SnowflakeUtil.generate(),
sleepTime = 2e3, // 2 seconds
} = {}) { } = {}) {
let type;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!query && !user_ids) query = ''; if (!query && !user_ids) query = '';
if (nonce.length > 32) throw new RangeError('MEMBER_FETCH_NONCE_LENGTH'); 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('BAN_MEMBERS') ||
this.guild.me.permissions.has('MANAGE_ROLES') this.guild.me.permissions.has('MANAGE_ROLES')
) { ) {
type = 8; // This is opcode
this.guild.shard.send({ this.guild.shard.send({
op: Opcodes.REQUEST_GUILD_MEMBERS, op: Opcodes.REQUEST_GUILD_MEMBERS,
d: { d: {
@ -437,6 +440,7 @@ class GuildMemberManager extends CachedManager {
}, },
}); });
} else { } else {
type = 14; // This is opcode
let channel; let channel;
let channels = this.guild.channels.cache.filter(c => c.isText()); let channels = this.guild.channels.cache.filter(c => c.isText());
channels = channels.filter(c => c.permissionsFor(this.guild.me).has('VIEW_CHANNEL')); 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 => const channels_allowed_everyone = channels.filter(c =>
c.permissionsFor(this.guild.roles.everyone).has('VIEW_CHANNEL'), 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] // Create array limit [0, 99]
const list = []; const list = [];
const allMember = this.guild.memberCount; const allMember = this.guild.memberCount;
@ -500,7 +505,7 @@ class GuildMemberManager extends CachedManager {
let i = 0; let i = 0;
const handler = (members, _, chunk) => { const handler = (members, _, chunk) => {
timeout.refresh(); timeout.refresh();
if (chunk.nonce !== nonce) return; if (chunk.nonce !== nonce && type === 8) return;
i++; i++;
for (const member of members.values()) { for (const member of members.values()) {
fetchedMembers.set(member.id, member); 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_MEMBERS_CHUNK, handler);
this.client.removeListener(Events.GUILD_MEMBER_LIST_UPDATE, handler); this.client.removeListener(Events.GUILD_MEMBER_LIST_UPDATE, handler);
this.client.decrementMaxListeners(); 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(); 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(() => { const timeout = setTimeout(() => {