switch to node-fetch (#2587)

* switch to node-fetch

* remove useless var declaration

* remove method uppercasing

* rework concurrency

* Revert "rework concurrency"

This reverts commit ef6aa2697e.

* fix headers
This commit is contained in:
Will Nelson
2018-06-19 11:10:55 -07:00
committed by Crawl
parent 791f58e130
commit 5e011dbc11
7 changed files with 70 additions and 55 deletions

View File

@@ -1,6 +1,6 @@
const snekfetch = require('snekfetch');
const Collection = require('./Collection');
const { Colors, DefaultOptions, Endpoints } = require('./Constants');
const fetch = require('node-fetch');
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
@@ -90,15 +90,14 @@ class Util {
* @returns {Promise<number>} The recommended number of shards
*/
static fetchRecommendedShards(token, guildsPerShard = 1000) {
return new Promise((resolve, reject) => {
if (!token) throw new DiscordError('TOKEN_MISSING');
snekfetch.get(`${DefaultOptions.http.api}/v${DefaultOptions.http.version}${Endpoints.botGateway}`)
.set('Authorization', `Bot ${token.replace(/^Bot\s*/i, '')}`)
.end((err, res) => {
if (err) reject(err);
resolve(res.body.shards * (1000 / guildsPerShard));
});
});
if (!token) throw new DiscordError('TOKEN_MISSING');
return fetch(`${DefaultOptions.http.api}/v${DefaultOptions.http.version}${Endpoints.botGateway}`, {
method: 'GET',
headers: { Authorization: `Bot ${token.replace(/^Bot\s*/i, '')}` },
}).then(res => {
if (res.ok) return res.json();
throw res;
}).then(data => data.shards * (1000 / guildsPerShard));
}
/**