mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 01:53:30 +01:00
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:
@@ -1,6 +1,6 @@
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const snekfetch = require('snekfetch');
|
||||
const fetch = require('node-fetch');
|
||||
const Util = require('../util/Util');
|
||||
const { Error: DiscordError, TypeError } = require('../errors');
|
||||
const { browser } = require('../util/Constants');
|
||||
@@ -83,13 +83,13 @@ class DataResolver {
|
||||
* @returns {Promise<Buffer>}
|
||||
*/
|
||||
static resolveFile(resource) {
|
||||
if (resource instanceof Buffer) return Promise.resolve(resource);
|
||||
if (!browser && resource instanceof Buffer) return Promise.resolve(resource);
|
||||
if (browser && resource instanceof ArrayBuffer) return Promise.resolve(Util.convertToBuffer(resource));
|
||||
|
||||
if (typeof resource === 'string') {
|
||||
if (/^https?:\/\//.test(resource)) {
|
||||
return snekfetch.get(resource).then(res => res.body instanceof Buffer ? res.body : Buffer.from(res.text));
|
||||
} else {
|
||||
return fetch(resource).then(res => browser ? res.blob() : res.buffer());
|
||||
} else if (!browser) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const file = browser ? resource : path.resolve(resource);
|
||||
fs.stat(file, (err, stats) => {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user