refactor(Client): remove fetchAllMembers option (#5257)

* feat(Client): remove fetchAllMembers option & logic

* Cleanup

* Missed type change
This commit is contained in:
Matt (IPv4) Cowley
2021-01-27 10:27:50 +00:00
committed by GitHub
parent 41bd6c2717
commit aaed72b723
7 changed files with 22 additions and 39 deletions

View File

@@ -463,9 +463,6 @@ class Client extends BaseClient {
if (typeof options.messageSweepInterval !== 'number' || isNaN(options.messageSweepInterval)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'messageSweepInterval', 'a number');
}
if (typeof options.fetchAllMembers !== 'boolean') {
throw new TypeError('CLIENT_INVALID_OPTION', 'fetchAllMembers', 'a boolean');
}
if (!Array.isArray(options.partials)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'partials', 'an Array');
}

View File

@@ -391,27 +391,12 @@ class WebSocketManager extends EventEmitter {
* Checks whether the client is ready to be marked as ready.
* @private
*/
async checkShardsReady() {
checkShardsReady() {
if (this.status === Status.READY) return;
if (this.shards.size !== this.totalShards || this.shards.some(s => s.status !== Status.READY)) {
return;
}
this.status = Status.NEARLY;
if (this.client.options.fetchAllMembers) {
try {
const promises = this.client.guilds.cache.map(guild => {
if (guild.available) return guild.members.fetch();
// Return empty promise if guild is unavailable
return Promise.resolve();
});
await Promise.all(promises);
} catch (err) {
this.debug(`Failed to fetch all members before ready! ${err}\n${err.stack}`);
}
}
this.triggerClientReady();
}

View File

@@ -2,18 +2,12 @@
const { Events, Status } = require('../../../util/Constants');
module.exports = async (client, { d: data }, shard) => {
module.exports = (client, { d: data }, shard) => {
let guild = client.guilds.cache.get(data.id);
if (guild) {
if (!guild.available && !data.unavailable) {
// A newly available guild
guild._patch(data);
// If the client was ready before and we had unavailable guilds, fetch them
if (client.ws.status === Status.READY && client.options.fetchAllMembers) {
await guild.members
.fetch()
.catch(err => client.emit(Events.DEBUG, `Failed to fetch all members: ${err}\n${err.stack}`));
}
}
} else {
// A new guild
@@ -25,11 +19,6 @@ module.exports = async (client, { d: data }, shard) => {
* @event Client#guildCreate
* @param {Guild} guild The created guild
*/
if (client.options.fetchAllMembers) {
await guild.members
.fetch()
.catch(err => client.emit(Events.DEBUG, `Failed to fetch all members: ${err}\n${err.stack}`));
}
client.emit(Events.GUILD_CREATE, guild);
}
}

View File

@@ -18,8 +18,6 @@ const { Error, RangeError } = require('../errors');
* sweepable (in seconds, 0 for forever)
* @property {number} [messageSweepInterval=0] How frequently to remove messages from the cache that are older than
* the message cache lifetime (in seconds, 0 for never)
* @property {boolean} [fetchAllMembers=false] Whether to cache all guild members and users upon startup, as well as
* upon joining a guild (should be avoided whenever possible)
* @property {MessageMentionOptions} [allowedMentions] Default value for {@link MessageOptions#allowedMentions}
* @property {PartialType[]} [partials] Structures allowed to be partial. This means events can be emitted even when
* they're missing all the data for a particular structure. See the "Partials" topic listed in the sidebar for some
@@ -41,7 +39,6 @@ exports.DefaultOptions = {
messageCacheMaxSize: 200,
messageCacheLifetime: 0,
messageSweepInterval: 0,
fetchAllMembers: false,
partials: [],
restWsBridgeTimeout: 5000,
restRequestTimeout: 15000,

View File

@@ -9,14 +9,30 @@ const Discord = require('../src');
console.time('magic');
const client = new Discord.Client({ fetchAllMembers: true });
const client = new Discord.Client();
client
.login(token)
.then(() => console.log('logged in'))
.catch(console.error);
client.on('ready', () => {
// Fetch all members in a new guild
client.on('guildCreate', guild => guild.members.fetch()
.catch(err => console.log(`Failed to fetch all members: ${err}\n${err.stack}`)));
// Fetch all members in a newly available guild
client.on('guildUpdate', (oldGuild, newGuild) => !oldGuild.available && newGuild.available ? guild.members.fetch()
.catch(err => console.log(`Failed to fetch all members: ${err}\n${err.stack}`)) : Promise.resolve());
client.on('ready', async () => {
// Fetch all members for initially available guilds
try {
const promises = client.guilds.cache.map(guild => guild.available ? guild.members.fetch() : Promise.resolve());
await Promise.all(promises);
} catch (err) {
console.log(`Failed to fetch all members before ready! ${err}\n${err.stack}`);
}
console.log(`ready with ${client.users.cache.size} users`);
console.timeEnd('magic');
});

View File

@@ -5,7 +5,7 @@ const ytdl = require('ytdl-core');
const auth = require('./auth.js');
const Discord = require('../src');
const client = new Discord.Client({ fetchAllMembers: false, partials: [] });
const client = new Discord.Client({ partials: [] });
client
.login(auth.token)

3
typings/index.d.ts vendored
View File

@@ -1786,7 +1786,7 @@ declare module 'discord.js' {
private destroy(): void;
private _handleSessionLimit(remaining?: number, resetAfter?: number): Promise<void>;
private handlePacket(packet?: object, shard?: WebSocketShard): boolean;
private checkShardsReady(): Promise<void>;
private checkShardsReady(): void;
private triggerClientReady(): void;
}
@@ -2427,7 +2427,6 @@ declare module 'discord.js' {
messageCacheMaxSize?: number;
messageCacheLifetime?: number;
messageSweepInterval?: number;
fetchAllMembers?: boolean;
allowedMentions?: MessageMentionOptions;
partials?: PartialTypes[];
restWsBridgeTimeout?: number;