From 576075f190351219a015f3e0dd4573fa479fa474 Mon Sep 17 00:00:00 2001 From: LJNeon Date: Tue, 26 Nov 2019 15:57:46 -0700 Subject: [PATCH 1/7] Changed Guild#fetchAllMembers to return a Promise --- lib/gateway/Shard.js | 22 ++++++++++++++++++++++ lib/structures/Guild.js | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/gateway/Shard.js b/lib/gateway/Shard.js index 41f4f47d6..d3337ebbd 100644 --- a/lib/gateway/Shard.js +++ b/lib/gateway/Shard.js @@ -144,6 +144,15 @@ class Shard extends EventEmitter { this.connecting = false; this.ready = false; this.preReady = false; + if(this.requestMembersPromise !== undefined) { + for(const guildID in this.requestMembersPromise) { + if(!this.requestMembersPromise.hasOwnProperty(guildID)) + continue; + clearTimeout(this.requestMembersPromise[guildID].timeout); + this.requestMembersPromise[guildID].res(); + } + } + this.requestMembersPromise = {}; this.getAllUsersCount = {}; this.getAllUsersQueue = []; this.getAllUsersLength = 1; @@ -1134,6 +1143,11 @@ class Shard extends EventEmitter { if(this.getAllUsersCount.hasOwnProperty(guild.id)) { if(this.getAllUsersCount[guild.id] <= 1) { delete this.getAllUsersCount[guild.id]; + if(this.requestMembersPromise.hasOwnProperty(guild.id)) { + clearTimeout(this.requestMembersPromise[guild.id].timeout); + this.requestMembersPromise[guild.id].res(); + delete this.requestMembersPromise[guild.id]; + } this.checkReady(); } else { --this.getAllUsersCount[guild.id]; @@ -1495,6 +1509,14 @@ class Shard extends EventEmitter { this.getAllUsersLength = 1 + guildID.length + 3; } else if(this.ready) { this.requestGuildMembers([guildID]); + + return new Promise(res => this.requestMembersPromise[guildID] = { + res, + timeout: setTimeout(() => { + delete this.requestMembersPromise[guildID]; + res(); + }, this.client.options.requestTimeout) + }); } else { this.getAllUsersQueue.push(guildID); this.getAllUsersLength += guildID.length + 3; diff --git a/lib/structures/Guild.js b/lib/structures/Guild.js index af0381356..41c3ee27e 100644 --- a/lib/structures/Guild.js +++ b/lib/structures/Guild.js @@ -149,9 +149,10 @@ class Guild extends Base { /** * Request all guild members from Discord + * @returns {Promise} Resolves once all guild members are fetched */ fetchAllMembers() { - this.shard.getGuildMembers(this.id, Math.ceil(this.memberCount / 1000)); // TODO Promise with chunk timeout + return this.shard.getGuildMembers(this.id, Math.ceil(this.memberCount / 1000)); } get iconURL() { From 8f0999026662d409bc2572ff8d220e1fba9e32f3 Mon Sep 17 00:00:00 2001 From: LJNeon Date: Tue, 26 Nov 2019 16:13:12 -0700 Subject: [PATCH 2/7] Updated typings --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 489286a64..9f12d0c42 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1205,7 +1205,7 @@ declare namespace Eris { iconURL?: string; explicitContentFilter: number; constructor(data: BaseData, client: Client); - fetchAllMembers(): void; + fetchAllMembers(): Promise; dynamicIconURL(format: string, size: number): string; createChannel(name: string): Promise; createChannel(name: string, type: 0, reason?: string, options?: CreateChannelOptions | string): Promise; From 004c2eac3707c8bce257fd52ec84c92e9aa2fa87 Mon Sep 17 00:00:00 2001 From: LJNeon Date: Tue, 26 Nov 2019 16:30:10 -0700 Subject: [PATCH 3/7] fixed linting errors --- lib/gateway/Shard.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/gateway/Shard.js b/lib/gateway/Shard.js index d3337ebbd..291d6e2a1 100644 --- a/lib/gateway/Shard.js +++ b/lib/gateway/Shard.js @@ -146,8 +146,9 @@ class Shard extends EventEmitter { this.preReady = false; if(this.requestMembersPromise !== undefined) { for(const guildID in this.requestMembersPromise) { - if(!this.requestMembersPromise.hasOwnProperty(guildID)) + if(!this.requestMembersPromise.hasOwnProperty(guildID)) { continue; + } clearTimeout(this.requestMembersPromise[guildID].timeout); this.requestMembersPromise[guildID].res(); } @@ -1510,8 +1511,8 @@ class Shard extends EventEmitter { } else if(this.ready) { this.requestGuildMembers([guildID]); - return new Promise(res => this.requestMembersPromise[guildID] = { - res, + return new Promise((res) => this.requestMembersPromise[guildID] = { + res: res, timeout: setTimeout(() => { delete this.requestMembersPromise[guildID]; res(); From fc9f619dc84db23ada97551df2de9203351af457 Mon Sep 17 00:00:00 2001 From: LJNeon Date: Tue, 26 Nov 2019 17:24:49 -0700 Subject: [PATCH 4/7] timeout option --- index.d.ts | 2 +- lib/gateway/Shard.js | 4 ++-- lib/structures/Guild.js | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 9f12d0c42..7ca20f7ce 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1205,7 +1205,7 @@ declare namespace Eris { iconURL?: string; explicitContentFilter: number; constructor(data: BaseData, client: Client); - fetchAllMembers(): Promise; + fetchAllMembers(timeout?: number): Promise; dynamicIconURL(format: string, size: number): string; createChannel(name: string): Promise; createChannel(name: string, type: 0, reason?: string, options?: CreateChannelOptions | string): Promise; diff --git a/lib/gateway/Shard.js b/lib/gateway/Shard.js index 291d6e2a1..57e0fc4a1 100644 --- a/lib/gateway/Shard.js +++ b/lib/gateway/Shard.js @@ -1502,7 +1502,7 @@ class Shard extends EventEmitter { } } - getGuildMembers(guildID, chunkCount) { + getGuildMembers(guildID, chunkCount, timeout) { this.getAllUsersCount[guildID] = chunkCount; if(this.getAllUsersLength + 3 + guildID.length > 4048) { // 4096 - "{\"op\":8,\"d\":{\"guild_id\":[],\"query\":\"\",\"limit\":0}}".length + 1 for lazy comma offset this.requestGuildMembers(this.getAllUsersQueue); @@ -1516,7 +1516,7 @@ class Shard extends EventEmitter { timeout: setTimeout(() => { delete this.requestMembersPromise[guildID]; res(); - }, this.client.options.requestTimeout) + }, timeout || this.client.options.requestTimeout) }); } else { this.getAllUsersQueue.push(guildID); diff --git a/lib/structures/Guild.js b/lib/structures/Guild.js index 41c3ee27e..466a81ca1 100644 --- a/lib/structures/Guild.js +++ b/lib/structures/Guild.js @@ -149,10 +149,11 @@ class Guild extends Base { /** * Request all guild members from Discord + * @arg {Number} [timeout] A number of milliseconds to resolve early at. Defaults to the client request timeout. * @returns {Promise} Resolves once all guild members are fetched */ - fetchAllMembers() { - return this.shard.getGuildMembers(this.id, Math.ceil(this.memberCount / 1000)); + fetchAllMembers(timeout) { + return this.shard.getGuildMembers(this.id, Math.ceil(this.memberCount / 1000), timeout); } get iconURL() { From cd706f5a20137654bda59c7039b8acae4610f287 Mon Sep 17 00:00:00 2001 From: abal Date: Wed, 27 Nov 2019 19:00:54 -0800 Subject: [PATCH 5/7] Update Guild.js --- lib/structures/Guild.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/structures/Guild.js b/lib/structures/Guild.js index 466a81ca1..2394428cb 100644 --- a/lib/structures/Guild.js +++ b/lib/structures/Guild.js @@ -149,7 +149,7 @@ class Guild extends Base { /** * Request all guild members from Discord - * @arg {Number} [timeout] A number of milliseconds to resolve early at. Defaults to the client request timeout. + * @arg {Number} [timeout] The number of milliseconds to wait before resolving early. Defaults to the `requestTimeout` client option * @returns {Promise} Resolves once all guild members are fetched */ fetchAllMembers(timeout) { From 2c74a14e0664155fef6598ef7512e66088599a93 Mon Sep 17 00:00:00 2001 From: LJNeon Date: Wed, 27 Nov 2019 21:14:19 -0700 Subject: [PATCH 6/7] Return total members received --- index.d.ts | 2 +- lib/gateway/Shard.js | 8 ++++++-- lib/structures/Guild.js | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 7ca20f7ce..11f769e7a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1205,7 +1205,7 @@ declare namespace Eris { iconURL?: string; explicitContentFilter: number; constructor(data: BaseData, client: Client); - fetchAllMembers(timeout?: number): Promise; + fetchAllMembers(timeout?: number): Promise; dynamicIconURL(format: string, size: number): string; createChannel(name: string): Promise; createChannel(name: string, type: 0, reason?: string, options?: CreateChannelOptions | string): Promise; diff --git a/lib/gateway/Shard.js b/lib/gateway/Shard.js index 57e0fc4a1..b8f43448f 100644 --- a/lib/gateway/Shard.js +++ b/lib/gateway/Shard.js @@ -1146,12 +1146,15 @@ class Shard extends EventEmitter { delete this.getAllUsersCount[guild.id]; if(this.requestMembersPromise.hasOwnProperty(guild.id)) { clearTimeout(this.requestMembersPromise[guild.id].timeout); - this.requestMembersPromise[guild.id].res(); + this.requestMembersPromise[guild.id].res(this.requestMembersPromise[guild.id].received + packet.d.members.length); delete this.requestMembersPromise[guild.id]; } this.checkReady(); } else { --this.getAllUsersCount[guild.id]; + if(this.requestMembersPromise.hasOwnProperty(guild.id)) { + this.requestMembersPromise[guild.id].received += packet.d.members.length; + } } } @@ -1513,9 +1516,10 @@ class Shard extends EventEmitter { return new Promise((res) => this.requestMembersPromise[guildID] = { res: res, + received: 0, timeout: setTimeout(() => { + res(this.requestMembersPromise[guildID].received); delete this.requestMembersPromise[guildID]; - res(); }, timeout || this.client.options.requestTimeout) }); } else { diff --git a/lib/structures/Guild.js b/lib/structures/Guild.js index 2394428cb..2e0bf7f05 100644 --- a/lib/structures/Guild.js +++ b/lib/structures/Guild.js @@ -150,7 +150,7 @@ class Guild extends Base { /** * Request all guild members from Discord * @arg {Number} [timeout] The number of milliseconds to wait before resolving early. Defaults to the `requestTimeout` client option - * @returns {Promise} Resolves once all guild members are fetched + * @returns {Promise} Resolves with the total amount of fetched members. */ fetchAllMembers(timeout) { return this.shard.getGuildMembers(this.id, Math.ceil(this.memberCount / 1000), timeout); From 7bb49ea9ebb238569617cabda950bb635934616e Mon Sep 17 00:00:00 2001 From: LJNeon Date: Sat, 30 Nov 2019 19:03:16 -0700 Subject: [PATCH 7/7] fix wording + add missing return value --- lib/gateway/Shard.js | 2 +- lib/structures/Guild.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gateway/Shard.js b/lib/gateway/Shard.js index b8f43448f..463c04ae5 100644 --- a/lib/gateway/Shard.js +++ b/lib/gateway/Shard.js @@ -150,7 +150,7 @@ class Shard extends EventEmitter { continue; } clearTimeout(this.requestMembersPromise[guildID].timeout); - this.requestMembersPromise[guildID].res(); + this.requestMembersPromise[guildID].res(this.requestMembersPromise[guildID].received); } } this.requestMembersPromise = {}; diff --git a/lib/structures/Guild.js b/lib/structures/Guild.js index 2e0bf7f05..ec38db7e0 100644 --- a/lib/structures/Guild.js +++ b/lib/structures/Guild.js @@ -150,7 +150,7 @@ class Guild extends Base { /** * Request all guild members from Discord * @arg {Number} [timeout] The number of milliseconds to wait before resolving early. Defaults to the `requestTimeout` client option - * @returns {Promise} Resolves with the total amount of fetched members. + * @returns {Promise} Resolves with the total number of fetched members. */ fetchAllMembers(timeout) { return this.shard.getGuildMembers(this.id, Math.ceil(this.memberCount / 1000), timeout);