mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 04:23:31 +01:00
switch to node-fetch
This commit is contained in:
@@ -34,9 +34,10 @@
|
|||||||
"runkitExampleFilename": "./docs/examples/ping.js",
|
"runkitExampleFilename": "./docs/examples/ping.js",
|
||||||
"unpkg": "./webpack/discord.min.js",
|
"unpkg": "./webpack/discord.min.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"form-data": "^2.3.2",
|
||||||
|
"node-fetch": "^2.1.2",
|
||||||
"pako": "^1.0.0",
|
"pako": "^1.0.0",
|
||||||
"prism-media": "^0.2.0",
|
"prism-media": "^0.2.0",
|
||||||
"snekfetch": "^3.6.0",
|
|
||||||
"tweetnacl": "^1.0.0",
|
"tweetnacl": "^1.0.0",
|
||||||
"ws": "^4.0.0"
|
"ws": "^4.0.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
const querystring = require('querystring');
|
const querystring = require('querystring');
|
||||||
const snekfetch = require('snekfetch');
|
const FormData = require('form-data');
|
||||||
const https = require('https');
|
const https = require('https');
|
||||||
const { browser, UserAgent } = require('../util/Constants');
|
const { browser, UserAgent } = require('../util/Constants');
|
||||||
|
var fetch = require('node-fetch');
|
||||||
|
|
||||||
if (https.Agent) var agent = new https.Agent({ keepAlive: true });
|
if (https.Agent) var agent = new https.Agent({ keepAlive: true });
|
||||||
|
|
||||||
@@ -9,7 +10,7 @@ class APIRequest {
|
|||||||
constructor(rest, method, path, options) {
|
constructor(rest, method, path, options) {
|
||||||
this.rest = rest;
|
this.rest = rest;
|
||||||
this.client = rest.client;
|
this.client = rest.client;
|
||||||
this.method = method;
|
this.method = method.toUpperCase();
|
||||||
this.route = options.route;
|
this.route = options.route;
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
|
||||||
@@ -17,24 +18,34 @@ class APIRequest {
|
|||||||
this.path = `${path}${queryString ? `?${queryString}` : ''}`;
|
this.path = `${path}${queryString ? `?${queryString}` : ''}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
gen() {
|
make() {
|
||||||
const API = this.options.versioned === false ? this.client.options.http.api :
|
const API = this.options.versioned === false ? this.client.options.http.api :
|
||||||
`${this.client.options.http.api}/v${this.client.options.http.version}`;
|
`${this.client.options.http.api}/v${this.client.options.http.version}`;
|
||||||
|
const url = API + this.path;
|
||||||
|
let headers = {};
|
||||||
|
|
||||||
const request = snekfetch[this.method](`${API}${this.path}`, { agent });
|
if (this.options.auth !== false) headers.Authorization = this.rest.getAuth();
|
||||||
|
if (this.options.reason) headers['X-Audit-Log-Reason'] = encodeURIComponent(this.options.reason);
|
||||||
if (this.options.auth !== false) request.set('Authorization', this.rest.getAuth());
|
if (!browser) headers['User-Agent'] = UserAgent;
|
||||||
if (this.options.reason) request.set('X-Audit-Log-Reason', encodeURIComponent(this.options.reason));
|
if (this.options.headers) headers = Object.assign(headers, this.options.headers);
|
||||||
if (!browser) request.set('User-Agent', UserAgent);
|
|
||||||
if (this.options.headers) request.set(this.options.headers);
|
|
||||||
|
|
||||||
|
let body;
|
||||||
if (this.options.files) {
|
if (this.options.files) {
|
||||||
for (const file of this.options.files) if (file && file.file) request.attach(file.name, file.file, file.name);
|
body = new FormData();
|
||||||
if (typeof this.options.data !== 'undefined') request.attach('payload_json', JSON.stringify(this.options.data));
|
for (const file of this.options.files) if (file && file.file) body.append(file.name, file.file, file.name);
|
||||||
} else if (typeof this.options.data !== 'undefined') {
|
if (typeof this.options.data !== 'undefined') body.append('payload_json', JSON.stringify(this.options.data));
|
||||||
request.send(this.options.data);
|
if (!browser) headers = Object.assign(headers, body.getHeaders());
|
||||||
|
} else if (this.options.data != null) { // eslint-disable-line eqeqeq
|
||||||
|
body = JSON.stringify(this.options.data);
|
||||||
|
headers['Content-Type'] = 'application/json';
|
||||||
}
|
}
|
||||||
return request;
|
|
||||||
|
return fetch(url, {
|
||||||
|
method: this.method,
|
||||||
|
headers,
|
||||||
|
agent,
|
||||||
|
body,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,34 +68,37 @@ class RequestHandler {
|
|||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
item.request.gen().end((err, res) => {
|
item.request.make().then(res => {
|
||||||
if (res && res.headers) {
|
if (res && res.headers) {
|
||||||
if (res.headers['x-ratelimit-global']) this.manager.globallyRateLimited = true;
|
if (res.headers['x-ratelimit-global']) this.manager.globallyRateLimited = true;
|
||||||
this.limit = Number(res.headers['x-ratelimit-limit']);
|
this.limit = Number(res.headers['x-ratelimit-limit']);
|
||||||
this.resetTime = Date.now() + Number(res.headers['retry-after']);
|
this.resetTime = Date.now() + Number(res.headers['retry-after']);
|
||||||
this.remaining = Number(res.headers['x-ratelimit-remaining']);
|
this.remaining = Number(res.headers['x-ratelimit-remaining']);
|
||||||
}
|
}
|
||||||
if (err) {
|
|
||||||
if (err.status === 429) {
|
if (res.ok) {
|
||||||
this.queue.unshift(item);
|
res.json().then(item.resolve, item.reject);
|
||||||
finish(Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
|
finish();
|
||||||
} else if (err.status >= 500 && err.status < 600) {
|
return;
|
||||||
if (item.retried) {
|
}
|
||||||
item.reject(err);
|
|
||||||
finish();
|
if (res.status === 429) {
|
||||||
} else {
|
this.queue.unshift(item);
|
||||||
item.retried = true;
|
finish(Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
|
||||||
this.queue.unshift(item);
|
} else if (res.status >= 500 && res.status < 600) {
|
||||||
finish(1e3 + this.client.options.restTimeOffset);
|
if (item.retried) {
|
||||||
}
|
item.reject(res);
|
||||||
} else {
|
|
||||||
item.reject(err.status >= 400 && err.status < 500 ?
|
|
||||||
new DiscordAPIError(res.request.path, res.body, res.request.method) : err);
|
|
||||||
finish();
|
finish();
|
||||||
|
} else {
|
||||||
|
item.retried = true;
|
||||||
|
this.queue.unshift(item);
|
||||||
|
finish(1e3 + this.client.options.restTimeOffset);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const data = res && res.body ? res.body : {};
|
res.json().then(data => {
|
||||||
item.resolve(data);
|
item.reject(res.status >= 400 && res.status < 500 ?
|
||||||
|
new DiscordAPIError(item.path, data, item.method) : res);
|
||||||
|
}, item.reject);
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const snekfetch = require('snekfetch');
|
const fetch = require('node-fetch');
|
||||||
const Util = require('../util/Util');
|
const Util = require('../util/Util');
|
||||||
const { Error: DiscordError, TypeError } = require('../errors');
|
const { Error: DiscordError, TypeError } = require('../errors');
|
||||||
const { browser } = require('../util/Constants');
|
const { browser } = require('../util/Constants');
|
||||||
@@ -83,13 +83,13 @@ class DataResolver {
|
|||||||
* @returns {Promise<Buffer>}
|
* @returns {Promise<Buffer>}
|
||||||
*/
|
*/
|
||||||
static resolveFile(resource) {
|
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 (browser && resource instanceof ArrayBuffer) return Promise.resolve(Util.convertToBuffer(resource));
|
||||||
|
|
||||||
if (typeof resource === 'string') {
|
if (typeof resource === 'string') {
|
||||||
if (/^https?:\/\//.test(resource)) {
|
if (/^https?:\/\//.test(resource)) {
|
||||||
return snekfetch.get(resource).then(res => res.body instanceof Buffer ? res.body : Buffer.from(res.text));
|
return fetch(resource).then(res => browser ? res.blob() : res.buffer());
|
||||||
} else {
|
} else if (!browser) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const file = browser ? resource : path.resolve(resource);
|
const file = browser ? resource : path.resolve(resource);
|
||||||
fs.stat(file, (err, stats) => {
|
fs.stat(file, (err, stats) => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const snekfetch = require('snekfetch');
|
|
||||||
const Collection = require('./Collection');
|
const Collection = require('./Collection');
|
||||||
const { Colors, DefaultOptions, Endpoints } = require('./Constants');
|
const { Colors, DefaultOptions, Endpoints } = require('./Constants');
|
||||||
|
const fetch = require('node-fetch');
|
||||||
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
|
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
|
||||||
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
||||||
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
|
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
|
||||||
@@ -90,15 +90,14 @@ class Util {
|
|||||||
* @returns {Promise<number>} The recommended number of shards
|
* @returns {Promise<number>} The recommended number of shards
|
||||||
*/
|
*/
|
||||||
static fetchRecommendedShards(token, guildsPerShard = 1000) {
|
static fetchRecommendedShards(token, guildsPerShard = 1000) {
|
||||||
return new Promise((resolve, reject) => {
|
if (!token) throw new DiscordError('TOKEN_MISSING');
|
||||||
if (!token) throw new DiscordError('TOKEN_MISSING');
|
return fetch(`${DefaultOptions.http.api}/v${DefaultOptions.http.version}${Endpoints.botGateway}`, {
|
||||||
snekfetch.get(`${DefaultOptions.http.api}/v${DefaultOptions.http.version}${Endpoints.botGateway}`)
|
method: 'GET',
|
||||||
.set('Authorization', `Bot ${token.replace(/^Bot\s*/i, '')}`)
|
headers: { Authorization: `Bot ${token.replace(/^Bot\s*/i, '')}` },
|
||||||
.end((err, res) => {
|
}).then(res => {
|
||||||
if (err) reject(err);
|
if (res.ok) return res.json();
|
||||||
resolve(res.body.shards * (1000 / guildsPerShard));
|
throw res;
|
||||||
});
|
}).then(data => data.shards * (1000 / guildsPerShard));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
const Discord = require('../');
|
const Discord = require('../');
|
||||||
|
const { token } = require('./auth');
|
||||||
|
|
||||||
const sharder = new Discord.ShardingManager(`${process.cwd()}/test/shard.js`, 4, false);
|
const sharder = new Discord.ShardingManager(`${process.cwd()}/test/shard.js`, { token, respawn: false });
|
||||||
|
|
||||||
sharder.on('launch', shard => console.log(`launched ${shard.id}`));
|
sharder.on('launch', shard => console.log(`launched ${shard.id}`));
|
||||||
|
|
||||||
sharder.spawn(4);
|
sharder.spawn();
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script type="text/javascript" src="../webpack/discord.min.js"></script>
|
<script type="text/javascript" src="../webpack/discord.js"></script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
(() => {
|
(() => {
|
||||||
const client = window.client = new Discord.Client();
|
const client = window.client = new Discord.Client();
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
client.login(localStorage.token || window.token || prompt('token pls', 'abcdef123456'))
|
client.login(localStorage.token || window.token || prompt('token pls', 'abcdef123456'))
|
||||||
.then((token) => localStorage.token = token);
|
.then((token) => localStorage.token = token, console.log);
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user