Skip to content

Commit

Permalink
feat: add ws option support for "buildIdentifyThrottler" (#9728)
Browse files Browse the repository at this point in the history
* feat: add option support for "buildIdentifyThrottler"

* docs: add documentation for buildIdentifyThrottler

* docs: fix example code
  • Loading branch information
Deivu authored Jul 22, 2023
1 parent d26e022 commit 6307f81
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 19 additions & 0 deletions packages/discord.js/src/util/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<IIdentifyThrottler>} The identify throttler that this ws manager will use
*/

/**
* WebSocket options (these are left as snake_case to match the API)
* @typedef {Object} WebsocketOptions
Expand All @@ -75,6 +88,7 @@ const { version } = require('../../package.json');
* @property {number} [version=10] The Discord gateway version to use <warn>Changing this can break the library;
* only set this if you know what you are doing</warn>
* @property {BuildStrategyFunction} [buildStrategy] Builds the strategy to use for sharding
* @property {IdentifyThrottlerFunction} [buildIdentifyThrottler] Builds the identify throttler to use for sharding
*/

/**
Expand Down Expand Up @@ -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}
*/
8 changes: 7 additions & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -6388,6 +6393,7 @@ export interface WebSocketOptions {
large_threshold?: number;
version?: number;
buildStrategy?(manager: WSWebSocketManager): IShardingStrategy;
buildIdentifyThrottler?(manager: WSWebSocketManager): Awaitable<IIdentifyThrottler>;
}

export interface WidgetActivity {
Expand Down

0 comments on commit 6307f81

Please sign in to comment.