diff --git a/packages/discord.js/src/sharding/ShardingManager.js b/packages/discord.js/src/sharding/ShardingManager.js index 148bb42c4..8d3ac66cd 100644 --- a/packages/discord.js/src/sharding/ShardingManager.js +++ b/packages/discord.js/src/sharding/ShardingManager.js @@ -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.'); diff --git a/packages/discord.js/src/util/Util.js b/packages/discord.js/src/util/Util.js index 0bbd2f073..9770e0b3a 100644 --- a/packages/discord.js/src/util/Util.js +++ b/packages/discord.js/src/util/Util.js @@ -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} 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, '')}` }, }); diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index be0aeac86..ea4581abc 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -3266,6 +3266,8 @@ export class ShardingManager extends AsyncEventEmitter; public broadcastEval(fn: (client: Client) => Awaitable): Promise[]>; public broadcastEval( @@ -3288,8 +3290,10 @@ export class ShardingManager extends AsyncEventEmitter