mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 20:43:30 +01:00
feat(FetchRecommendedShardsOptions): account for large bot sharding (#6184)
Co-authored-by: Justin <justinleeong@gmail.com>
This commit is contained in:
@@ -254,13 +254,19 @@ class Util extends null {
|
|||||||
return text.replace(/\|\|/g, '\\|\\|');
|
return text.replace(/\|\|/g, '\\|\\|');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} FetchRecommendedShardsOptions
|
||||||
|
* @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)
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the recommended shard count from Discord.
|
* Gets the recommended shard count from Discord.
|
||||||
* @param {string} token Discord auth token
|
* @param {string} token Discord auth token
|
||||||
* @param {number} [guildsPerShard=1000] Number of guilds per shard
|
* @param {FetchRecommendedShardsOptions} [options] Options for fetching the recommended shard count
|
||||||
* @returns {Promise<number>} The recommended number of shards
|
* @returns {Promise<number>} The recommended number of shards
|
||||||
*/
|
*/
|
||||||
static fetchRecommendedShards(token, guildsPerShard = 1000) {
|
static fetchRecommendedShards(token, { guildsPerShard = 1000, multipleOf = 1 } = {}) {
|
||||||
if (!token) throw new DiscordError('TOKEN_MISSING');
|
if (!token) throw new DiscordError('TOKEN_MISSING');
|
||||||
const defaults = Options.createDefault();
|
const defaults = Options.createDefault();
|
||||||
return fetch(`${defaults.http.api}/v${defaults.http.version}${Endpoints.botGateway}`, {
|
return fetch(`${defaults.http.api}/v${defaults.http.version}${Endpoints.botGateway}`, {
|
||||||
@@ -272,7 +278,7 @@ class Util extends null {
|
|||||||
if (res.status === 401) throw new DiscordError('TOKEN_INVALID');
|
if (res.status === 401) throw new DiscordError('TOKEN_INVALID');
|
||||||
throw res;
|
throw res;
|
||||||
})
|
})
|
||||||
.then(data => data.shards * (1000 / guildsPerShard));
|
.then(data => Math.ceil((data.shards * (1000 / guildsPerShard)) / multipleOf) * multipleOf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
7
typings/index.d.ts
vendored
7
typings/index.d.ts
vendored
@@ -1535,6 +1535,11 @@ export class ShardingManager extends EventEmitter {
|
|||||||
public once(event: 'shardCreate', listener: (shard: Shard) => Awaited<void>): this;
|
public once(event: 'shardCreate', listener: (shard: Shard) => Awaited<void>): this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface FetchRecommendedShardsOptions {
|
||||||
|
guildsPerShard?: number;
|
||||||
|
multipleOf?: number;
|
||||||
|
}
|
||||||
|
|
||||||
export class SnowflakeUtil extends null {
|
export class SnowflakeUtil extends null {
|
||||||
private constructor();
|
private constructor();
|
||||||
public static deconstruct(snowflake: Snowflake): DeconstructedSnowflake;
|
public static deconstruct(snowflake: Snowflake): DeconstructedSnowflake;
|
||||||
@@ -1795,7 +1800,7 @@ export class Util extends null {
|
|||||||
public static escapeStrikethrough(text: string): string;
|
public static escapeStrikethrough(text: string): string;
|
||||||
public static escapeSpoiler(text: string): string;
|
public static escapeSpoiler(text: string): string;
|
||||||
public static cleanCodeBlockContent(text: string): string;
|
public static cleanCodeBlockContent(text: string): string;
|
||||||
public static fetchRecommendedShards(token: string, guildsPerShard?: number): Promise<number>;
|
public static fetchRecommendedShards(token: string, options?: FetchRecommendedShardsOptions): Promise<number>;
|
||||||
public static flatten(obj: unknown, ...props: Record<string, boolean | string>[]): unknown;
|
public static flatten(obj: unknown, ...props: Record<string, boolean | string>[]): unknown;
|
||||||
public static idToBinary(num: Snowflake): string;
|
public static idToBinary(num: Snowflake): string;
|
||||||
public static makeError(obj: MakeErrorOptions): Error;
|
public static makeError(obj: MakeErrorOptions): Error;
|
||||||
|
|||||||
Reference in New Issue
Block a user