mirror of
https://github.com/discordjs/discord.js.git
synced 2026-06-03 01:20:07 +00:00
feat(ShardingManager): allow setting custom api url
This commit is contained in:
@@ -7,6 +7,7 @@ const { setTimeout: sleep } = require('node:timers/promises');
|
||||
const { Collection } = require('@discordjs/collection');
|
||||
const { range } = require('@discordjs/util');
|
||||
const { AsyncEventEmitter } = require('@vladfrangu/async_event_emitter');
|
||||
const { APIVersion, RouteBases } = require('discord-api-types/v10');
|
||||
const { DiscordjsError, DiscordjsTypeError, DiscordjsRangeError, ErrorCodes } = require('../errors/index.js');
|
||||
const { fetchRecommendedShardCount } = require('../util/Util.js');
|
||||
const { Shard } = require('./Shard.js');
|
||||
@@ -43,6 +44,8 @@ class ShardingManager extends AsyncEventEmitter {
|
||||
* @property {string[]} [shardArgs=[]] Arguments to pass to the shard script when spawning
|
||||
* @property {string[]} [execArgv=[]] Arguments to pass to the shard script executable when spawning
|
||||
* @property {string} [token] Token to use for automatic shard count and passing to shards
|
||||
* @property {string} [api='https://discord.com/api'] The base API URL
|
||||
* @property {string} [version='10'] The API version to use
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -164,6 +167,20 @@ class ShardingManager extends AsyncEventEmitter {
|
||||
*/
|
||||
this.token = _options.token?.replace(/^bot\s*/i, '') ?? null;
|
||||
|
||||
/**
|
||||
* The base API URL
|
||||
*
|
||||
* @type {string}
|
||||
*/
|
||||
this.api = _options.api ?? RouteBases.api;
|
||||
|
||||
/**
|
||||
* The API version to use
|
||||
*
|
||||
* @type {?string}
|
||||
*/
|
||||
this.version = _options.version ?? APIVersion;
|
||||
|
||||
/**
|
||||
* A collection of shards that this manager has spawned
|
||||
*
|
||||
@@ -217,7 +234,10 @@ class ShardingManager extends AsyncEventEmitter {
|
||||
let shardAmount = amount;
|
||||
if (shardAmount === 'auto') {
|
||||
// eslint-disable-next-line require-atomic-updates
|
||||
shardAmount = await fetchRecommendedShardCount(this.token);
|
||||
shardAmount = await fetchRecommendedShardCount(this.token, {
|
||||
api: this.api,
|
||||
version: this.version,
|
||||
});
|
||||
} else {
|
||||
if (typeof shardAmount !== 'number' || Number.isNaN(shardAmount)) {
|
||||
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'Amount of shards', 'a number.');
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
const { parse } = require('node:path');
|
||||
const { Collection } = require('@discordjs/collection');
|
||||
const { lazy } = require('@discordjs/util');
|
||||
const { ChannelType, RouteBases, Routes } = require('discord-api-types/v10');
|
||||
const { APIVersion, ChannelType, Routes, RouteBases } = require('discord-api-types/v10');
|
||||
const { fetch } = require('undici');
|
||||
const { Colors } = require('./Colors.js');
|
||||
// eslint-disable-next-line import-x/order
|
||||
@@ -67,6 +67,8 @@ function flatten(obj, ...props) {
|
||||
* @typedef {Object} FetchRecommendedShardCountOptions
|
||||
* @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)
|
||||
* @property {string} [api='https://discord.com/api'] The base API URL
|
||||
* @property {string} [version='10'] The API version to use
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -76,9 +78,12 @@ function flatten(obj, ...props) {
|
||||
* @param {FetchRecommendedShardCountOptions} [options] Options for fetching the recommended shard count
|
||||
* @returns {Promise<number>} The recommended number of shards
|
||||
*/
|
||||
async function fetchRecommendedShardCount(token, { guildsPerShard = 1_000, multipleOf = 1 } = {}) {
|
||||
async function fetchRecommendedShardCount(
|
||||
token,
|
||||
{ guildsPerShard = 1_000, multipleOf = 1, api = RouteBases.api, version = APIVersion } = {},
|
||||
) {
|
||||
if (!token) throw new DiscordjsError(ErrorCodes.TokenMissing);
|
||||
const response = await fetch(RouteBases.api + Routes.gatewayBot(), {
|
||||
const response = await fetch(`${api}/v${version}${Routes.gatewayBot()}`, {
|
||||
method: 'GET',
|
||||
headers: { Authorization: `Bot ${token.replace(/^bot\s*/i, '')}` },
|
||||
});
|
||||
|
||||
6
packages/discord.js/typings/index.d.ts
vendored
6
packages/discord.js/typings/index.d.ts
vendored
@@ -3266,6 +3266,8 @@ export class ShardingManager extends AsyncEventEmitter<ShardingManagerEventTypes
|
||||
public token: string | null;
|
||||
public totalShards: number | 'auto';
|
||||
public shardList: number[] | 'auto';
|
||||
public api: string;
|
||||
public version: string;
|
||||
public broadcast(message: unknown): Promise<Shard[]>;
|
||||
public broadcastEval<Result>(fn: (client: Client) => Awaitable<Result>): Promise<Serialized<Result>[]>;
|
||||
public broadcastEval<Result, Context>(
|
||||
@@ -3288,8 +3290,10 @@ export class ShardingManager extends AsyncEventEmitter<ShardingManagerEventTypes
|
||||
}
|
||||
|
||||
export interface FetchRecommendedShardCountOptions {
|
||||
api?: string;
|
||||
guildsPerShard?: number;
|
||||
multipleOf?: number;
|
||||
version?: string;
|
||||
}
|
||||
|
||||
export {
|
||||
@@ -7150,6 +7154,7 @@ export interface SetRolePositionOptions {
|
||||
export type ShardingManagerMode = 'process' | 'worker';
|
||||
|
||||
export interface ShardingManagerOptions {
|
||||
api?: string;
|
||||
execArgv?: readonly string[];
|
||||
mode?: ShardingManagerMode;
|
||||
respawn?: boolean;
|
||||
@@ -7158,6 +7163,7 @@ export interface ShardingManagerOptions {
|
||||
silent?: boolean;
|
||||
token?: string;
|
||||
totalShards?: number | 'auto';
|
||||
version?: string;
|
||||
}
|
||||
|
||||
export interface ShowModalOptions {
|
||||
|
||||
Reference in New Issue
Block a user