i hate discord 😭
This commit is contained in:
Elysia
2023-02-09 19:26:39 +07:00
parent 9bab1f1983
commit 756a241051
11 changed files with 41 additions and 92 deletions

View File

@@ -4,10 +4,8 @@ const { parse } = require('node:path');
const process = require('node:process');
const { Collection } = require('@discordjs/collection');
const axios = require('axios');
const fetch = require('node-fetch');
const { Colors, Endpoints } = require('./Constants');
const Options = require('./Options');
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
const { Colors } = require('./Constants');
const { RangeError, TypeError } = require('../errors');
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
const isObject = d => typeof d === 'object' && d !== null;
@@ -332,33 +330,6 @@ class Util extends null {
return text.replaceAll(/\[.+\]\(.+\)/gm, '\\$&');
}
/**
* @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.
* @param {string} token Discord auth token
* @param {FetchRecommendedShardsOptions} [options] Options for fetching the recommended shard count
* @returns {Promise<number>} The recommended number of shards
*/
static async fetchRecommendedShards(token, { guildsPerShard = 1_000, multipleOf = 1 } = {}) {
if (!token) throw new DiscordError('TOKEN_MISSING');
const defaults = Options.createDefault();
const response = await fetch(`${defaults.http.api}/v${defaults.http.version}${Endpoints.botGateway}`, {
method: 'GET',
headers: { Authorization: `Bot ${token.replace(/^Bot\s*/i, '')}` },
});
if (!response.ok) {
if (response.status === 401) throw new DiscordError('TOKEN_INVALID');
throw response;
}
const { shards } = await response.json();
return Math.ceil((shards * (1_000 / guildsPerShard)) / multipleOf) * multipleOf;
}
/**
* Parses emoji info out of a string. The string must be one of:
* * A UTF-8 emoji (no id)