refactor(*): use async functions (#6210)

This commit is contained in:
Sugden
2021-08-02 00:47:43 +01:00
committed by GitHub
parent 626ff85ae7
commit e2e4f6518b
29 changed files with 298 additions and 399 deletions

View File

@@ -267,19 +267,19 @@ class Util extends null {
* @param {FetchRecommendedShardsOptions} [options] Options for fetching the recommended shard count
* @returns {Promise<number>} The recommended number of shards
*/
static fetchRecommendedShards(token, { guildsPerShard = 1000, multipleOf = 1 } = {}) {
static async fetchRecommendedShards(token, { guildsPerShard = 1000, multipleOf = 1 } = {}) {
if (!token) throw new DiscordError('TOKEN_MISSING');
const defaults = Options.createDefault();
return fetch(`${defaults.http.api}/v${defaults.http.version}${Endpoints.botGateway}`, {
const response = await fetch(`${defaults.http.api}/v${defaults.http.version}${Endpoints.botGateway}`, {
method: 'GET',
headers: { Authorization: `Bot ${token.replace(/^Bot\s*/i, '')}` },
})
.then(res => {
if (res.ok) return res.json();
if (res.status === 401) throw new DiscordError('TOKEN_INVALID');
throw res;
})
.then(data => Math.ceil((data.shards * (1000 / guildsPerShard)) / multipleOf) * multipleOf);
});
if (!response.ok) {
if (response.status === 401) throw new DiscordError('TOKEN_INVALID');
throw response;
}
const { shards } = await response.json();
return Math.ceil((shards * (1000 / guildsPerShard)) / multipleOf) * multipleOf;
}
/**
@@ -500,11 +500,12 @@ class Util extends null {
* @returns {Promise<Channel[]|Role[]>} Updated item list, with `id` and `position` properties
* @private
*/
static setPosition(item, position, relative, sorted, route, reason) {
static async setPosition(item, position, relative, sorted, route, reason) {
let updatedItems = [...sorted.values()];
Util.moveElementInArray(updatedItems, item, position, relative);
updatedItems = updatedItems.map((r, i) => ({ id: r.id, position: i }));
return route.patch({ data: updatedItems, reason }).then(() => updatedItems);
await route.patch({ data: updatedItems, reason });
return updatedItems;
}
/**