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(FetchRecommendedShardsOptions): account for large bot sharding #6184

Merged
merged 6 commits into from Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/util/Util.js
Expand Up @@ -254,15 +254,22 @@ class Util extends null {
return text.replace(/\|\|/g, '\\|\\|');
}

/**
* @typedef {Object} FetchRecommendedShardsOptions
* @property {number} [guildsPerShard=1000] Number of guilds per shard
scottbucher marked this conversation as resolved.
Show resolved Hide resolved
* @property {boolean} [largeBotSharding=false] Whether or not to round for large bot sharding
*/

/**
* Gets the recommended shard count from Discord.
* @param {string} token Discord auth token
* @param {number} [guildsPerShard=1000] Number of guilds per shard
* @param {FetchRecommendedShardsOptions} [options] Options for fetching recommended shard count
scottbucher marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Promise<number>} The recommended number of shards
*/
static fetchRecommendedShards(token, guildsPerShard = 1000) {
static fetchRecommendedShards(token, { guildsPerShard = 1000, largeBotSharding = false } = {}) {
if (!token) throw new DiscordError('TOKEN_MISSING');
const defaults = Options.createDefault();
const multiple = largeBotSharding ? 16 : 1;
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
return fetch(`${defaults.http.api}/v${defaults.http.version}${Endpoints.botGateway}`, {
method: 'GET',
headers: { Authorization: `Bot ${token.replace(/^Bot\s*/i, '')}` },
Expand All @@ -272,7 +279,7 @@ class Util extends null {
if (res.status === 401) throw new DiscordError('TOKEN_INVALID');
throw res;
})
.then(data => data.shards * (1000 / guildsPerShard));
.then(data => Math.ceil(data.shards * (1000 / guildsPerShard), multiple) * multiple);
scottbucher marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
7 changes: 6 additions & 1 deletion typings/index.d.ts
Expand Up @@ -1531,6 +1531,11 @@ export class ShardingManager extends EventEmitter {
public once(event: 'shardCreate', listener: (shard: Shard) => Awaited<void>): this;
}

export interface FetchRecommendedShardsOptions {
guildsPerShard?: number;
largeBotSharding?: boolean;
}

export class SnowflakeUtil extends null {
private constructor();
public static deconstruct(snowflake: Snowflake): DeconstructedSnowflake;
Expand Down Expand Up @@ -1791,7 +1796,7 @@ export class Util extends null {
public static escapeStrikethrough(text: string): string;
public static escapeSpoiler(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 idToBinary(num: Snowflake): string;
public static makeError(obj: MakeErrorOptions): Error;
Expand Down