From 19b242ac10aa9b32c1a45a9178c97481d62a9400 Mon Sep 17 00:00:00 2001 From: Scott Bucher <47844014+scottbucher@users.noreply.github.com> Date: Wed, 28 Jul 2021 21:52:51 -0400 Subject: [PATCH] feat(FetchRecommendedShardsOptions): account for large bot sharding (#6184) Co-authored-by: Justin --- src/util/Util.js | 12 +++++++++--- typings/index.d.ts | 7 ++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/util/Util.js b/src/util/Util.js index 2e06fef4c..12858d0a0 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -254,13 +254,19 @@ class Util extends null { return text.replace(/\|\|/g, '\\|\\|'); } + /** + * @typedef {Object} FetchRecommendedShardsOptions + * @property {number} [guildsPerShard=1000] Number of guilds assigned per shard + * @property {number} [multipleOf=1] The multiple the shard count should round up to. (16 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 the recommended shard count * @returns {Promise} The recommended number of shards */ - static fetchRecommendedShards(token, guildsPerShard = 1000) { + static fetchRecommendedShards(token, { guildsPerShard = 1000, multipleOf = 1 } = {}) { if (!token) throw new DiscordError('TOKEN_MISSING'); const defaults = Options.createDefault(); return fetch(`${defaults.http.api}/v${defaults.http.version}${Endpoints.botGateway}`, { @@ -272,7 +278,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)) / multipleOf) * multipleOf); } /** diff --git a/typings/index.d.ts b/typings/index.d.ts index bf5b8b9a9..759f237d1 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1535,6 +1535,11 @@ export class ShardingManager extends EventEmitter { public once(event: 'shardCreate', listener: (shard: Shard) => Awaited): this; } +export interface FetchRecommendedShardsOptions { + guildsPerShard?: number; + multipleOf?: number; +} + export class SnowflakeUtil extends null { private constructor(); public static deconstruct(snowflake: Snowflake): DeconstructedSnowflake; @@ -1795,7 +1800,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;