refactor(WebSocketManager)!: remove deprecated rest option (#10998)

BREAKING CHANGE: The `rest` option in the `WebSocketManager` constructor has been removed. Pass a `fetchGatewayInformation` function instead.
This commit is contained in:
Almeida
2025-07-27 11:49:29 +01:00
committed by GitHub
parent 2e9bfba5f4
commit 08a61ca8e3
9 changed files with 98 additions and 281 deletions

View File

@@ -1,17 +1,15 @@
import type { Collection } from '@discordjs/collection';
import type { REST } from '@discordjs/rest';
import { range, type Awaitable } from '@discordjs/util';
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
import {
Routes,
type APIGatewayBotInfo,
type GatewayIdentifyProperties,
type GatewayPresenceUpdateData,
type RESTGetAPIGatewayBotResult,
type GatewayIntentBits,
type GatewaySendPayload,
type GatewayDispatchPayload,
type GatewayReadyDispatchData,
import type {
APIGatewayBotInfo,
GatewayIdentifyProperties,
GatewayPresenceUpdateData,
RESTGetAPIGatewayBotResult,
GatewayIntentBits,
GatewaySendPayload,
GatewayDispatchPayload,
GatewayReadyDispatchData,
} from 'discord-api-types/v10';
import type { IShardingStrategy } from '../strategies/sharding/IShardingStrategy.js';
import type { IIdentifyThrottler } from '../throttling/IIdentifyThrottler.js';
@@ -56,6 +54,22 @@ export interface SessionInfo {
* Required options for the WebSocketManager
*/
export interface RequiredWebSocketManagerOptions {
/**
* Function for retrieving the information returned by the `/gateway/bot` endpoint.
* We recommend using a REST client that respects Discord's rate limits, such as `@discordjs/rest`.
*
* @example
* ```ts
* const rest = new REST().setToken(process.env.DISCORD_TOKEN);
* const manager = new WebSocketManager({
* token: process.env.DISCORD_TOKEN,
* fetchGatewayInformation() {
* return rest.get(Routes.gatewayBot()) as Promise<RESTGetAPIGatewayBotResult>;
* },
* });
* ```
*/
fetchGatewayInformation(): Awaitable<RESTGetAPIGatewayBotResult>;
/**
* The intents to request
*/
@@ -75,10 +89,13 @@ export interface OptionalWebSocketManagerOptions {
*
* @example
* ```ts
* const rest = new REST().setToken(process.env.DISCORD_TOKEN);
* const manager = new WebSocketManager({
* token: process.env.DISCORD_TOKEN,
* intents: 0, // for no intents
* rest,
* fetchGatewayInformation() {
* return rest.get(Routes.gatewayBot()) as Promise<RESTGetAPIGatewayBotResult>;
* },
* buildStrategy: (manager) => new WorkerShardingStrategy(manager, { shardsPerWorker: 2 }),
* });
* ```
@@ -96,21 +113,6 @@ export interface OptionalWebSocketManagerOptions {
* @defaultValue `'json'`
*/
encoding: Encoding;
/**
* Fetches the initial gateway URL used to connect to Discord. When missing, this will default to the gateway URL
* that Discord returns from the `/gateway/bot` route.
*
* @example
* ```ts
* const manager = new WebSocketManager({
* token: process.env.DISCORD_TOKEN,
* fetchGatewayInformation() {
* return rest.get(Routes.gatewayBot());
* },
* })
* ```
*/
fetchGatewayInformation(): Awaitable<RESTGetAPIGatewayBotResult>;
/**
* How long to wait for a shard to connect before giving up
*/
@@ -135,12 +137,6 @@ export interface OptionalWebSocketManagerOptions {
* How long to wait for a shard's READY packet before giving up
*/
readyTimeout: number | null;
/**
* The REST instance to use for fetching gateway information
*
* @deprecated Providing a REST instance is deprecated. Provide the `fetchGatewayInformation` function instead.
*/
rest?: REST;
/**
* Function used to retrieve session information (and attempt to resume) for a given shard
*
@@ -271,22 +267,13 @@ export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> i
}
public constructor(options: CreateWebSocketManagerOptions) {
if (!options.rest && !options.fetchGatewayInformation) {
throw new RangeError('Either a REST instance or a fetchGatewayInformation function must be provided');
if (typeof options.fetchGatewayInformation !== 'function') {
throw new TypeError('fetchGatewayInformation is required');
}
super();
this.options = {
...DefaultWebSocketManagerOptions,
fetchGatewayInformation:
options.fetchGatewayInformation ??
(async () => {
if (!options.rest) {
throw new RangeError('A REST instance must be provided if no fetchGatewayInformation function is provided');
}
return options.rest.get(Routes.gatewayBot()) as Promise<RESTGetAPIGatewayBotResult>;
}),
...options,
};
this.strategy = this.options.buildStrategy(this);