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';
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

View File

@ -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;

View File

@ -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,10 +515,14 @@ 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();
if (type == 14) {
this.guild.client.sleep(sleepTime).then(() => resolve(fetched));
} else {
resolve(fetched);
}
}
};
const timeout = setTimeout(() => {
this.client.removeListener(Events.GUILD_MEMBERS_CHUNK, handler);