From d9f07499a036495cbfa5ca0510c1dca10432738d Mon Sep 17 00:00:00 2001 From: Scott Bucher <47844014+scottbucher@users.noreply.github.com> Date: Sun, 25 Jul 2021 22:00:47 -0400 Subject: [PATCH 1/6] feat(FetchRecommendedShardsOptions): account for large bot sharding --- src/util/Util.js | 13 ++++++++++--- typings/index.d.ts | 7 ++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/util/Util.js b/src/util/Util.js index 2e06fef4c543..514702962d44 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -254,15 +254,22 @@ class Util extends null { return text.replace(/\|\|/g, '\\|\\|'); } + /** + * @typedef {Object} FetchRecommendedShardsOptions + * @property {number} [guildsPerShard=1000] Number of guilds per shard + * @property {boolean} [largeBotSharding=false] Whether or not to round for large bot sharding + */ + /** * Gets the recommended shard count from Discord. * @param {string} token Discord auth token - * @param {number} [guildsPerShard=1000] Number of guilds per shard + * @param {FetchRecommendedShardsOptions} [options] Options for fetching recommended shard count * @returns {Promise} The recommended number of shards */ - static fetchRecommendedShards(token, guildsPerShard = 1000) { + static fetchRecommendedShards(token, { guildsPerShard = 1000, largeBotSharding = false } = {}) { if (!token) throw new DiscordError('TOKEN_MISSING'); const defaults = Options.createDefault(); + const multiple = largeBotSharding ? 16 : 1; return fetch(`${defaults.http.api}/v${defaults.http.version}${Endpoints.botGateway}`, { method: 'GET', headers: { Authorization: `Bot ${token.replace(/^Bot\s*/i, '')}` }, @@ -272,7 +279,7 @@ class Util extends null { if (res.status === 401) throw new DiscordError('TOKEN_INVALID'); throw res; }) - .then(data => data.shards * (1000 / guildsPerShard)); + .then(data => Math.ceil(data.shards * (1000 / guildsPerShard), multiple) * multiple); } /** diff --git a/typings/index.d.ts b/typings/index.d.ts index 5b85087b1f4f..1d179d3d7a6b 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1531,6 +1531,11 @@ export class ShardingManager extends EventEmitter { public once(event: 'shardCreate', listener: (shard: Shard) => Awaited): this; } +export interface FetchRecommendedShardsOptions { + guildsPerShard?: number; + largeBotSharding?: boolean; +} + export class SnowflakeUtil extends null { private constructor(); public static deconstruct(snowflake: Snowflake): DeconstructedSnowflake; @@ -1791,7 +1796,7 @@ export class Util extends null { public static escapeStrikethrough(text: string): string; public static escapeSpoiler(text: string): string; public static cleanCodeBlockContent(text: string): string; - public static fetchRecommendedShards(token: string, guildsPerShard?: number): Promise; + public static fetchRecommendedShards(token: string, options?: FetchRecommendedShardsOptions): Promise; public static flatten(obj: unknown, ...props: Record[]): unknown; public static idToBinary(num: Snowflake): string; public static makeError(obj: MakeErrorOptions): Error; From 03837ce582da5b99dc5dd77f03dbcbfbdac1be14 Mon Sep 17 00:00:00 2001 From: Scott Bucher <47844014+scottbucher@users.noreply.github.com> Date: Mon, 26 Jul 2021 07:45:49 -0400 Subject: [PATCH 2/6] Update src/util/Util.js Co-authored-by: Justin --- src/util/Util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/Util.js b/src/util/Util.js index 514702962d44..109938098134 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -256,7 +256,7 @@ class Util extends null { /** * @typedef {Object} FetchRecommendedShardsOptions - * @property {number} [guildsPerShard=1000] Number of guilds per shard + * @property {number} [guildsPerShard=1000] Number of guilds assigned per shard * @property {boolean} [largeBotSharding=false] Whether or not to round for large bot sharding */ From 9847623095b0c7ae30acbacec4183156620d1e65 Mon Sep 17 00:00:00 2001 From: Scott Bucher <47844014+scottbucher@users.noreply.github.com> Date: Mon, 26 Jul 2021 07:46:12 -0400 Subject: [PATCH 3/6] Update src/util/Util.js Co-authored-by: Justin --- src/util/Util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/Util.js b/src/util/Util.js index 109938098134..5f13e3061d58 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -263,7 +263,7 @@ class Util extends null { /** * Gets the recommended shard count from Discord. * @param {string} token Discord auth token - * @param {FetchRecommendedShardsOptions} [options] Options for fetching recommended shard count + * @param {FetchRecommendedShardsOptions} [options] Options for fetching the recommended shard count * @returns {Promise} The recommended number of shards */ static fetchRecommendedShards(token, { guildsPerShard = 1000, largeBotSharding = false } = {}) { From 8ceda8235c0c36f711cf911169f7a695763aea11 Mon Sep 17 00:00:00 2001 From: Scott Bucher <47844014+scottbucher@users.noreply.github.com> Date: Mon, 26 Jul 2021 08:26:27 -0400 Subject: [PATCH 4/6] Update Util.js --- src/util/Util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/Util.js b/src/util/Util.js index 5f13e3061d58..e15cc8001682 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -279,7 +279,7 @@ class Util extends null { if (res.status === 401) throw new DiscordError('TOKEN_INVALID'); throw res; }) - .then(data => Math.ceil(data.shards * (1000 / guildsPerShard), multiple) * multiple); + .then(data => Math.ceil((data.shards * (1000 / guildsPerShard)) / multiple) * multiple); } /** From ec1848f311b1c90fbb159f0700e2678c3fa1579e Mon Sep 17 00:00:00 2001 From: Scott Bucher <47844014+scottbucher@users.noreply.github.com> Date: Tue, 27 Jul 2021 20:24:33 -0400 Subject: [PATCH 5/6] feat(FetchRecommendedShardsOptions): replace boolean with multiple of --- src/util/Util.js | 7 +++---- typings/index.d.ts | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/util/Util.js b/src/util/Util.js index e15cc8001682..6e35f4bb58e3 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -257,7 +257,7 @@ class Util extends null { /** * @typedef {Object} FetchRecommendedShardsOptions * @property {number} [guildsPerShard=1000] Number of guilds assigned per shard - * @property {boolean} [largeBotSharding=false] Whether or not to round for large bot sharding + * @property {boolean} [multipleOf=1] The multiple the shard count should round up to. (16 for large bot sharding) */ /** @@ -266,10 +266,9 @@ class Util extends null { * @param {FetchRecommendedShardsOptions} [options] Options for fetching the recommended shard count * @returns {Promise} The recommended number of shards */ - static fetchRecommendedShards(token, { guildsPerShard = 1000, largeBotSharding = false } = {}) { + static fetchRecommendedShards(token, { guildsPerShard = 1000, multipleOf = 1 } = {}) { if (!token) throw new DiscordError('TOKEN_MISSING'); const defaults = Options.createDefault(); - const multiple = largeBotSharding ? 16 : 1; return fetch(`${defaults.http.api}/v${defaults.http.version}${Endpoints.botGateway}`, { method: 'GET', headers: { Authorization: `Bot ${token.replace(/^Bot\s*/i, '')}` }, @@ -279,7 +278,7 @@ class Util extends null { if (res.status === 401) throw new DiscordError('TOKEN_INVALID'); throw res; }) - .then(data => Math.ceil((data.shards * (1000 / guildsPerShard)) / multiple) * multiple); + .then(data => Math.ceil((data.shards * (1000 / guildsPerShard)) / multipleOf) * multipleOf); } /** diff --git a/typings/index.d.ts b/typings/index.d.ts index 1d179d3d7a6b..e26ef147957e 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1533,7 +1533,7 @@ export class ShardingManager extends EventEmitter { export interface FetchRecommendedShardsOptions { guildsPerShard?: number; - largeBotSharding?: boolean; + multipleOf?: number; } export class SnowflakeUtil extends null { From aae521766a36592d65f4b3f33ae5ae5984fe1732 Mon Sep 17 00:00:00 2001 From: Scott Bucher <47844014+scottbucher@users.noreply.github.com> Date: Tue, 27 Jul 2021 20:26:03 -0400 Subject: [PATCH 6/6] Update datatype of multipleOf --- src/util/Util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/Util.js b/src/util/Util.js index 6e35f4bb58e3..12858d0a09a0 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -257,7 +257,7 @@ class Util extends null { /** * @typedef {Object} FetchRecommendedShardsOptions * @property {number} [guildsPerShard=1000] Number of guilds assigned per shard - * @property {boolean} [multipleOf=1] The multiple the shard count should round up to. (16 for large bot sharding) + * @property {number} [multipleOf=1] The multiple the shard count should round up to. (16 for large bot sharding) */ /**