From 6307f813854ed9dc76d2c4351bd69dd0490d0928 Mon Sep 17 00:00:00 2001 From: Saya <36309350+Deivu@users.noreply.github.com> Date: Sat, 22 Jul 2023 16:57:24 +0800 Subject: [PATCH] feat: add ws option support for "buildIdentifyThrottler" (#9728) * feat: add option support for "buildIdentifyThrottler" * docs: add documentation for buildIdentifyThrottler * docs: fix example code --- .../src/client/websocket/WebSocketManager.js | 1 + packages/discord.js/src/util/Options.js | 19 +++++++++++++++++++ packages/discord.js/typings/index.d.ts | 8 +++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/discord.js/src/client/websocket/WebSocketManager.js b/packages/discord.js/src/client/websocket/WebSocketManager.js index e3a2ec749778..f62610b78ffb 100644 --- a/packages/discord.js/src/client/websocket/WebSocketManager.js +++ b/packages/discord.js/src/client/websocket/WebSocketManager.js @@ -155,6 +155,7 @@ class WebSocketManager extends EventEmitter { }, compression: zlib ? CompressionMethod.ZlibStream : null, }; + if (ws.buildIdentifyThrottler) wsOptions.buildIdentifyThrottler = ws.buildIdentifyThrottler; if (ws.buildStrategy) wsOptions.buildStrategy = ws.buildStrategy; this._ws = new WSWebSocketManager(wsOptions); this.attachEvents(); diff --git a/packages/discord.js/src/util/Options.js b/packages/discord.js/src/util/Options.js index 9e31308e7590..429354622e22 100644 --- a/packages/discord.js/src/util/Options.js +++ b/packages/discord.js/src/util/Options.js @@ -67,6 +67,19 @@ const { version } = require('../../package.json'); * @returns {IShardingStrategy} The strategy to use for sharding */ +/** + * A function to change the concurrency handling for shard identifies of this manager + * ```js + * async (manager) => { + * const gateway = await manager.fetchGatewayInformation(); + * return new SimpleIdentifyThrottler(gateway.session_start_limit.max_concurrency); + * } + * ``` + * @typedef {Function} IdentifyThrottlerFunction + * @param {WSWebSocketManager} manager The WebSocketManager that is going to initiate the sharding + * @returns {Awaitable} The identify throttler that this ws manager will use + */ + /** * WebSocket options (these are left as snake_case to match the API) * @typedef {Object} WebsocketOptions @@ -75,6 +88,7 @@ const { version } = require('../../package.json'); * @property {number} [version=10] The Discord gateway version to use Changing this can break the library; * only set this if you know what you are doing * @property {BuildStrategyFunction} [buildStrategy] Builds the strategy to use for sharding + * @property {IdentifyThrottlerFunction} [buildIdentifyThrottler] Builds the identify throttler to use for sharding */ /** @@ -214,3 +228,8 @@ module.exports = Options; * @external IShardingStrategy * @see {@link https://discord.js.org/docs/packages/ws/stable/IShardingStrategy:Interface} */ + +/** + * @external IIdentifyThrottler + * @see {@link https://discord.js.org/docs/packages/ws/stable/IIdentifyThrottler:Interface} + */ diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index bd34b1d75c82..766ff2deb2be 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -40,7 +40,12 @@ import { import { Awaitable, JSONEncodable } from '@discordjs/util'; import { Collection } from '@discordjs/collection'; import { BaseImageURLOptions, ImageURLOptions, RawFile, REST, RESTOptions } from '@discordjs/rest'; -import { WebSocketManager as WSWebSocketManager, IShardingStrategy, SessionInfo } from '@discordjs/ws'; +import { + WebSocketManager as WSWebSocketManager, + IShardingStrategy, + IIdentifyThrottler, + SessionInfo, +} from '@discordjs/ws'; import { APIActionRowComponent, APIApplicationCommandInteractionData, @@ -6388,6 +6393,7 @@ export interface WebSocketOptions { large_threshold?: number; version?: number; buildStrategy?(manager: WSWebSocketManager): IShardingStrategy; + buildIdentifyThrottler?(manager: WSWebSocketManager): Awaitable; } export interface WidgetActivity {