Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ws option support for "buildIdentifyThrottler" #9728

Merged
merged 3 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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