@jnode/discord 1.0.15 → 1.0.17
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.
- package/package.json +2 -2
- package/src/client.js +16 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jnode/discord",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.17",
|
|
4
4
|
"description": "Simple Discord API package for Node.js.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -27,4 +27,4 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@jnode/request": ">=1.1.3 <2.0.0"
|
|
29
29
|
}
|
|
30
|
-
}
|
|
30
|
+
}
|
package/src/client.js
CHANGED
|
@@ -25,16 +25,16 @@ class DiscordAPIError extends Error {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
//Discord Client, everything starts here
|
|
28
|
-
class DiscordClient
|
|
28
|
+
class DiscordClient {
|
|
29
29
|
constructor(token, options = {}) {
|
|
30
30
|
this.token = token;
|
|
31
|
-
|
|
31
|
+
|
|
32
32
|
//client api options
|
|
33
33
|
this.apiVersion = options.apiVersion ?? 10;
|
|
34
34
|
this.apiBase = options.apiBase ?? 'discord.com/api';
|
|
35
35
|
this.apiAutoRetry = options.apiAutoRetry ?? true;
|
|
36
36
|
this.apiThrowError = options.apiThrowError ?? true; //throw error while api status code is not 2xx
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
//client gateway options
|
|
39
39
|
this.gatewayIntents = options.gatewayIntents ?? 0b11001100011111111111111111;
|
|
40
40
|
this.gatewayUrl = options.gatewayUrl ?? 'wss://gateway.discord.gg';
|
|
@@ -42,16 +42,16 @@ class DiscordClient {
|
|
|
42
42
|
this.gatewayReconnectDelay = options.gatewayReconnectDelay ?? 5000; //less than 0 to disable auto reconnect
|
|
43
43
|
this.gatewayConnectTimeout = options.gatewayConnectTimeout ?? 5000; //less than 0 to disable connect timeout (may cause error)
|
|
44
44
|
this.gatewayThrowError = options.gatewayThrowError ?? true; //throw error while gateway went something wrong
|
|
45
|
-
|
|
45
|
+
|
|
46
46
|
//gateway
|
|
47
47
|
this.gateway = new DiscordGateway(this);
|
|
48
48
|
}
|
|
49
|
-
|
|
49
|
+
|
|
50
50
|
//get full api url with base, version and path
|
|
51
51
|
apiUrl(path) {
|
|
52
52
|
return `https://${this.apiBase}/v${this.apiVersion}${path}`;
|
|
53
53
|
}
|
|
54
|
-
|
|
54
|
+
|
|
55
55
|
//make an request to Discord
|
|
56
56
|
async apiRequest(method = 'GET', path = '/', body) {
|
|
57
57
|
const res = await request.request(method, this.apiUrl(path), {
|
|
@@ -59,19 +59,19 @@ class DiscordClient {
|
|
|
59
59
|
'User-Agent': 'DiscordBot',
|
|
60
60
|
'Content-Type': (body !== undefined) ? 'application/json' : null
|
|
61
61
|
}, (body !== undefined) ? JSON.stringify(body) : undefined); //make an request
|
|
62
|
-
|
|
62
|
+
|
|
63
63
|
if ((res.statusCode === 429) && this.apiAutoRetry) { //retry if recieved 429
|
|
64
64
|
await delay(res.json().retry_after);
|
|
65
65
|
return this.apiRequest(method, path, body);
|
|
66
66
|
}
|
|
67
|
-
|
|
67
|
+
|
|
68
68
|
if (((res.statusCode > 299) || (res.statusCode < 200)) && this.apiThrowError) { //throw error if not 2xx
|
|
69
69
|
throw new DiscordAPIError(res.statusCode, res.json() ?? res.text(), res.headers);
|
|
70
70
|
}
|
|
71
|
-
|
|
71
|
+
|
|
72
72
|
return res;
|
|
73
73
|
}
|
|
74
|
-
|
|
74
|
+
|
|
75
75
|
//make an multi part request to Discord
|
|
76
76
|
async apiRequestMultipart(method = 'GET', path = '/', body, attachments = []) {
|
|
77
77
|
let parts = [];
|
|
@@ -80,7 +80,7 @@ class DiscordClient {
|
|
|
80
80
|
type: 'application/json',
|
|
81
81
|
data: JSON.stringify(body)
|
|
82
82
|
});
|
|
83
|
-
|
|
83
|
+
|
|
84
84
|
for (let i = 0; i < attachments.length; i++) { //add every attachment
|
|
85
85
|
parts.push({
|
|
86
86
|
disposition: `form-data; name="files[${i}]"; filename="${encodeURIComponent(attachments[i].name)}"`,
|
|
@@ -91,24 +91,24 @@ class DiscordClient {
|
|
|
91
91
|
file: attachments[i].file
|
|
92
92
|
});
|
|
93
93
|
}
|
|
94
|
-
|
|
94
|
+
|
|
95
95
|
const res = await request.multipartRequest(method, this.apiUrl(path), {
|
|
96
96
|
'Authorization': `Bot ${this.token}`,
|
|
97
97
|
'User-Agent': 'DiscordBot'
|
|
98
98
|
}, parts); //make an request
|
|
99
|
-
|
|
99
|
+
|
|
100
100
|
if ((res.statusCode === 429) && this.apiAutoRetry) { //retry if recieved 429
|
|
101
101
|
await delay(res.json().retry_after);
|
|
102
102
|
return this.apiRequest(method, path, body, multipart, true);
|
|
103
103
|
}
|
|
104
|
-
|
|
104
|
+
|
|
105
105
|
if (((res.statusCode > 299) || (res.statusCode < 200)) && this.apiThrowError) { //throw error if not 2xx
|
|
106
106
|
throw new DiscordAPIError(res.statusCode, res.json() ?? res.text(), res.headers);
|
|
107
107
|
}
|
|
108
|
-
|
|
108
|
+
|
|
109
109
|
return res;
|
|
110
110
|
}
|
|
111
|
-
|
|
111
|
+
|
|
112
112
|
async connectGateway(cb) {
|
|
113
113
|
await this.gateway.getGatewayUrl();
|
|
114
114
|
this.gateway.connect();
|