disgroove 1.2.0 → 1.2.1-dev.bc38dd2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,23 +1,11 @@
|
|
|
1
1
|
import { File } from ".";
|
|
2
|
-
export interface Request {
|
|
3
|
-
method: string;
|
|
4
|
-
endpoint: string;
|
|
5
|
-
data?: {
|
|
6
|
-
json?: Record<string, any> | Array<Record<string, any>>;
|
|
7
|
-
files?: Array<File> | null;
|
|
8
|
-
reason?: string;
|
|
9
|
-
query?: Record<string, any>;
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
2
|
export declare class RequestsManager {
|
|
13
3
|
token: string;
|
|
14
4
|
auth: "Bot" | "Bearer";
|
|
15
|
-
|
|
5
|
+
rateLimits: Array<string>;
|
|
16
6
|
globalBlock: boolean;
|
|
17
7
|
constructor(token: string, auth: "Bot" | "Bearer");
|
|
18
|
-
|
|
19
|
-
private checkRateLimits;
|
|
20
|
-
request<T>(method: string, endpoint: string, data?: {
|
|
8
|
+
request<T = unknown>(method: string, endpoint: string, data?: {
|
|
21
9
|
json?: Record<string, any> | Array<Record<string, any>>;
|
|
22
10
|
files?: Array<File> | null;
|
|
23
11
|
reason?: string;
|
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RequestsManager = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
4
5
|
const undici_1 = require("undici");
|
|
5
6
|
const constants_1 = require("../constants");
|
|
6
7
|
const utils_1 = require("../utils");
|
|
7
8
|
class RequestsManager {
|
|
8
9
|
token;
|
|
9
10
|
auth;
|
|
10
|
-
|
|
11
|
+
rateLimits;
|
|
11
12
|
globalBlock;
|
|
12
13
|
constructor(token, auth) {
|
|
13
14
|
this.token = token;
|
|
14
15
|
this.auth = auth;
|
|
15
|
-
this.
|
|
16
|
+
this.rateLimits = [];
|
|
16
17
|
this.globalBlock = false;
|
|
17
18
|
}
|
|
18
|
-
|
|
19
|
+
request(method, endpoint, data) {
|
|
19
20
|
return new Promise(async (resolve, reject) => {
|
|
20
|
-
if (this.
|
|
21
|
+
if (this.globalBlock)
|
|
21
22
|
return;
|
|
22
|
-
const { method, endpoint, data } = this.queue.shift();
|
|
23
23
|
let url = new URL(`https://discord.com/api/v10/${endpoint}`);
|
|
24
24
|
if (data?.query) {
|
|
25
25
|
for (const [key, value] of Object.entries(data?.query)) {
|
|
@@ -31,7 +31,7 @@ class RequestsManager {
|
|
|
31
31
|
Authorization: `${this.auth} ${this.token}`,
|
|
32
32
|
};
|
|
33
33
|
let body;
|
|
34
|
-
if (method !==
|
|
34
|
+
if (method !== _1.RESTMethods.Get) {
|
|
35
35
|
if (data?.files && data?.files?.length !== 0) {
|
|
36
36
|
const formData = new undici_1.FormData(), files = data?.files;
|
|
37
37
|
for (let i = 0; i < data?.files.length; i++) {
|
|
@@ -55,17 +55,40 @@ class RequestsManager {
|
|
|
55
55
|
body,
|
|
56
56
|
headers,
|
|
57
57
|
});
|
|
58
|
-
this.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
58
|
+
if (this.rateLimits.find((bucket) => bucket === response.headers.get("X-RateLimit-Bucket")))
|
|
59
|
+
return;
|
|
60
|
+
if (response.headers.has("X-RateLimit-Bucket") &&
|
|
61
|
+
response.headers.get("X-RateLimit-Remaining") === "0" &&
|
|
62
|
+
!this.rateLimits.find((bucket) => bucket === response.headers.get("X-RateLimit-Bucket"))) {
|
|
63
|
+
this.rateLimits.push(response.headers.get("X-RateLimit-Bucket"));
|
|
64
|
+
setTimeout(() => {
|
|
65
|
+
this.rateLimits = this.rateLimits.filter((bucket) => bucket !== response.headers.get("X-RateLimit-Bucket"));
|
|
66
|
+
this.request(method, endpoint, data).then(resolve).catch(reject);
|
|
67
|
+
}, Number(response.headers.get("X-RateLimit-Reset-After")) * 1000);
|
|
68
|
+
}
|
|
69
|
+
if (response.status >= constants_1.HTTPResponseCodes.NotModified) {
|
|
70
|
+
if (response.status === constants_1.HTTPResponseCodes.TooManyRequests) {
|
|
71
|
+
if (response.headers.has("X-RateLimit-Global")) {
|
|
72
|
+
this.globalBlock = true;
|
|
73
|
+
setTimeout(() => {
|
|
74
|
+
this.globalBlock = false;
|
|
75
|
+
this.rateLimits = this.rateLimits.filter((bucket) => bucket !== response.headers.get("X-RateLimit-Bucket"));
|
|
76
|
+
this.request(method, endpoint, data)
|
|
77
|
+
.then(resolve)
|
|
78
|
+
.catch(reject);
|
|
79
|
+
}, Number(response.headers.get("Retry-After")) * 1000);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
const responseJSON = await response.json();
|
|
84
|
+
reject(responseJSON &&
|
|
85
|
+
typeof responseJSON === "object" &&
|
|
86
|
+
"code" in responseJSON &&
|
|
87
|
+
"message" in responseJSON &&
|
|
88
|
+
responseJSON.code !== 0
|
|
89
|
+
? new utils_1.RESTError(`[${responseJSON.code}] ${responseJSON.message}`)
|
|
90
|
+
: new utils_1.HTTPError(`[${response.status}] ${response.statusText}`));
|
|
91
|
+
}
|
|
69
92
|
}
|
|
70
93
|
else if (response.status === constants_1.HTTPResponseCodes.NoContent) {
|
|
71
94
|
resolve(null);
|
|
@@ -77,26 +100,6 @@ class RequestsManager {
|
|
|
77
100
|
catch (err) {
|
|
78
101
|
reject(err);
|
|
79
102
|
}
|
|
80
|
-
finally {
|
|
81
|
-
this.process();
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
checkRateLimits(headers) {
|
|
86
|
-
if (!headers.has("X-RateLimit-Reset"))
|
|
87
|
-
return;
|
|
88
|
-
if (headers.get("X-RateLimit-Remaining") !== "0")
|
|
89
|
-
return;
|
|
90
|
-
this.globalBlock = true;
|
|
91
|
-
setTimeout(() => {
|
|
92
|
-
this.globalBlock = false;
|
|
93
|
-
this.process();
|
|
94
|
-
}, new Date(Number(headers.get("X-RateLimit-Reset")) * 1000).getTime() - new Date().getTime());
|
|
95
|
-
}
|
|
96
|
-
request(method, endpoint, data) {
|
|
97
|
-
return new Promise((resolve) => {
|
|
98
|
-
this.queue.push({ method, endpoint, data });
|
|
99
|
-
resolve(this.process());
|
|
100
103
|
});
|
|
101
104
|
}
|
|
102
105
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RequestsManager.js","sourceRoot":"","sources":["../../src/rest/RequestsManager.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"RequestsManager.js","sourceRoot":"","sources":["../../src/rest/RequestsManager.ts"],"names":[],"mappings":";;;AAAA,wBAAsC;AACtC,mCAA6D;AAC7D,4CAAiD;AACjD,oCAAgD;AAEhD,MAAa,eAAe;IACnB,KAAK,CAAS;IACd,IAAI,CAAmB;IACvB,UAAU,CAAgB;IAC1B,WAAW,CAAU;IAE5B,YAAY,KAAa,EAAE,IAAsB;QAC/C,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;IAC3B,CAAC;IAEM,OAAO,CACZ,MAAc,EACd,QAAgB,EAChB,IAKC;QAED,OAAO,IAAI,OAAO,CAAI,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE;YAC9C,IAAI,IAAI,CAAC,WAAW;gBAAE,OAAO;YAE7B,IAAI,GAAG,GAAQ,IAAI,GAAG,CAAC,+BAA+B,QAAQ,EAAE,CAAC,CAAC;YAElE,IAAI,IAAI,EAAE,KAAK,EAAE;gBACf,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE;oBAC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