@imaentity/selfjs 3.3.0-b02 → 4.0.1
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/README.md +12 -11
- package/docs.md +848 -480
- package/package.json +1 -1
- package/self.js +723 -802
- package/changelog.log +0 -37
package/docs.md
CHANGED
|
@@ -1,480 +1,848 @@
|
|
|
1
|
-
#
|
|
2
|
-
Welcome To SelfJS Documentation
|
|
3
|
-
|
|
4
|
-
> [!WARNING]
|
|
5
|
-
>
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
1
|
+
# SelfJS Documentation
|
|
2
|
+
Welcome To SelfJS Documentation
|
|
3
|
+
|
|
4
|
+
> [!WARNING]
|
|
5
|
+
> These docs might not be up to date or 100% correct, Self is always being added to.
|
|
6
|
+
> If a descrepancy is found perfer the JSdoc included with the self.js file
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @imaentity/selfjs
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Import
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
const self = require("@imaentity/selfjs");
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
# Constants
|
|
23
|
+
|
|
24
|
+
## `Status`
|
|
25
|
+
|
|
26
|
+
Contains the activity types used by Discord.
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
Status.PLAYING // 0
|
|
30
|
+
Status.STREAMING // 1
|
|
31
|
+
Status.LISTENING // 2
|
|
32
|
+
Status.WATCHING // 3
|
|
33
|
+
Status.CUSTOM_STATUS // 4
|
|
34
|
+
Status.COMPETING // 5
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
# Functions
|
|
40
|
+
|
|
41
|
+
## `validateToken(token)`
|
|
42
|
+
|
|
43
|
+
Checks whether a Discord token is valid without creating a persistent `Client`.
|
|
44
|
+
|
|
45
|
+
Returns the user's Discord user object if valid, or `null` if the token is invalid.
|
|
46
|
+
|
|
47
|
+
### Parameters
|
|
48
|
+
|
|
49
|
+
| Name | Type | Description |
|
|
50
|
+
| ------- | -------- | ------------------------------ |
|
|
51
|
+
| `token` | `String` | The Discord token to validate. |
|
|
52
|
+
|
|
53
|
+
### Returns
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
Promise<Object | null>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Example
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
const user = await self.validateToken(token);
|
|
63
|
+
|
|
64
|
+
if(user)
|
|
65
|
+
console.log(`Logged in as ${user.username}`);
|
|
66
|
+
else
|
|
67
|
+
console.log("Invalid token");
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## `createToken(options)`
|
|
73
|
+
|
|
74
|
+
Attempts to log into a Discord account using an email and password.
|
|
75
|
+
|
|
76
|
+
If MFA is not enabled, the returned object contains the token immediately.
|
|
77
|
+
|
|
78
|
+
If MFA is required, the returned object contains the available MFA methods and a `confirmMFA()` function.
|
|
79
|
+
|
|
80
|
+
### Parameters
|
|
81
|
+
|
|
82
|
+
| Name | Type | Description |
|
|
83
|
+
| ------------------ | -------- | ----------------- |
|
|
84
|
+
| `options` | `Object` | Login options. |
|
|
85
|
+
| `options.email` | `String` | Account email. |
|
|
86
|
+
| `options.password` | `String` | Account password. |
|
|
87
|
+
|
|
88
|
+
### Returns
|
|
89
|
+
|
|
90
|
+
```javascript
|
|
91
|
+
Promise<Object | null>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Successful login
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
{
|
|
98
|
+
token: String,
|
|
99
|
+
user_id: String,
|
|
100
|
+
mfaRequired: false
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### MFA login
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
{
|
|
108
|
+
user_id: String,
|
|
109
|
+
mfaRequired: true,
|
|
110
|
+
mfaMethods: Array<String>,
|
|
111
|
+
confirmMFA: Function
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Example
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
const login = await self.createToken({
|
|
119
|
+
email: "discord@example.com",
|
|
120
|
+
password: "password"
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
if(!login)
|
|
124
|
+
throw new Error("Login failed");
|
|
125
|
+
|
|
126
|
+
if(!login.mfaRequired) {
|
|
127
|
+
console.log(login.token);
|
|
128
|
+
} else {
|
|
129
|
+
console.log("MFA methods:", login.mfaMethods);
|
|
130
|
+
|
|
131
|
+
const result = await login.confirmMFA("totp", {
|
|
132
|
+
code: "123456"
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
console.log(result);
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
> [!NOTE]
|
|
140
|
+
> SMS MFA is currently unsupported.
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## `snowflakeToUTC(snowflake)`
|
|
145
|
+
|
|
146
|
+
Converts a Discord snowflake into a UTC timestamp.
|
|
147
|
+
|
|
148
|
+
The returned timestamp is in milliseconds since Unix epoch.
|
|
149
|
+
|
|
150
|
+
### Parameters
|
|
151
|
+
|
|
152
|
+
| Name | Type | Description |
|
|
153
|
+
| ----------- | -------- | ------------------ |
|
|
154
|
+
| `snowflake` | `String` | Discord snowflake. |
|
|
155
|
+
|
|
156
|
+
### Returns
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
159
|
+
Number
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Example
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
const timestamp = self.snowflakeToUTC("1329029486758592595");
|
|
166
|
+
|
|
167
|
+
console.log(new Date(timestamp));
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## `UTCToSnowflake(timestamp)`
|
|
173
|
+
|
|
174
|
+
Converts a UTC timestamp into a Discord snowflake.
|
|
175
|
+
|
|
176
|
+
The generated snowflake only contains the timestamp portion. Worker ID, process ID, and sequence values are zero.
|
|
177
|
+
|
|
178
|
+
### Parameters
|
|
179
|
+
|
|
180
|
+
| Name | Type | Description |
|
|
181
|
+
| ----------- | -------- | ------------------------------ |
|
|
182
|
+
| `timestamp` | `Number` | UTC timestamp in milliseconds. |
|
|
183
|
+
|
|
184
|
+
### Returns
|
|
185
|
+
|
|
186
|
+
```javascript
|
|
187
|
+
String
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Example
|
|
191
|
+
|
|
192
|
+
```javascript
|
|
193
|
+
const snowflake = self.UTCToSnowflake(Date.now());
|
|
194
|
+
|
|
195
|
+
console.log(snowflake);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
# Client
|
|
201
|
+
|
|
202
|
+
`Client` provides an interface for connecting to Discord and interacting with the account.
|
|
203
|
+
|
|
204
|
+
## Creating a client
|
|
205
|
+
|
|
206
|
+
```javascript
|
|
207
|
+
const client = new self.Client();
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Options
|
|
211
|
+
|
|
212
|
+
```javascript
|
|
213
|
+
const client = new self.Client({
|
|
214
|
+
properties: {
|
|
215
|
+
os: process.platform,
|
|
216
|
+
browser: "SelfJS",
|
|
217
|
+
device: "NodeJS"
|
|
218
|
+
},
|
|
219
|
+
|
|
220
|
+
debugLogs: true,
|
|
221
|
+
|
|
222
|
+
intents: 0
|
|
223
|
+
});
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
| Option | Type | Default | Description |
|
|
227
|
+
| ------------ | --------- | ------------- | --------------------------------------- |
|
|
228
|
+
| `properties` | `Object` | `LOGIN_PROPS` | Gateway identify properties. |
|
|
229
|
+
| `debugLogs` | `Boolean` | `true` | Enables SelfJS debug logging. |
|
|
230
|
+
| `intents` | `Number` | `null` | Gateway intents, used for bot accounts. |
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
# Properties
|
|
235
|
+
|
|
236
|
+
## `client.user`
|
|
237
|
+
|
|
238
|
+
The user object belonging to the logged-in account.
|
|
239
|
+
|
|
240
|
+
```javascript
|
|
241
|
+
console.log(client.user);
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
This is populated after the `READY` event.
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## `client.token`
|
|
249
|
+
|
|
250
|
+
The token currently being used by the client.
|
|
251
|
+
|
|
252
|
+
```javascript
|
|
253
|
+
console.log(client.token);
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
## `client.latency`
|
|
259
|
+
|
|
260
|
+
The time between sending a heartbeat and receiving its acknowledgement.
|
|
261
|
+
|
|
262
|
+
```javascript
|
|
263
|
+
console.log(client.latency);
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
The value is in milliseconds.
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
# Login
|
|
271
|
+
|
|
272
|
+
## `client.login(token)`
|
|
273
|
+
|
|
274
|
+
Connects the client to the Discord Gateway using the provided token.
|
|
275
|
+
|
|
276
|
+
### Parameters
|
|
277
|
+
|
|
278
|
+
| Name | Type | Description |
|
|
279
|
+
| ------- | -------- | ---------------------- |
|
|
280
|
+
| `token` | `String` | Discord account token. |
|
|
281
|
+
|
|
282
|
+
### Example
|
|
283
|
+
|
|
284
|
+
```javascript
|
|
285
|
+
client.login(token);
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
Once connected, events can be received using `client.on()`.
|
|
289
|
+
|
|
290
|
+
---
|
|
291
|
+
|
|
292
|
+
# Events
|
|
293
|
+
|
|
294
|
+
`Client` extends Node.js `EventEmitter`, so events can be listened to using `.on()`.
|
|
295
|
+
|
|
296
|
+
```javascript
|
|
297
|
+
client.on("MESSAGE_CREATE", message => {
|
|
298
|
+
console.log(message.content);
|
|
299
|
+
});
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## `READY`
|
|
303
|
+
|
|
304
|
+
Emitted when the client successfully logs in.
|
|
305
|
+
|
|
306
|
+
```javascript
|
|
307
|
+
client.on("READY", data => {
|
|
308
|
+
console.log("Logged in as:", data.user.username);
|
|
309
|
+
});
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
---
|
|
313
|
+
|
|
314
|
+
## `MESSAGE_CREATE`
|
|
315
|
+
|
|
316
|
+
Emitted when a message is received.
|
|
317
|
+
|
|
318
|
+
```javascript
|
|
319
|
+
client.on("MESSAGE_CREATE", message => {
|
|
320
|
+
console.log(message.content);
|
|
321
|
+
});
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
The message object also receives:
|
|
325
|
+
|
|
326
|
+
```javascript
|
|
327
|
+
message.author.self
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
which is `true` when the message was sent by the current account.
|
|
331
|
+
|
|
332
|
+
### Preventing automatic acknowledgement
|
|
333
|
+
|
|
334
|
+
Messages are automatically acknowledged unless they were sent by the current account.
|
|
335
|
+
|
|
336
|
+
Call:
|
|
337
|
+
|
|
338
|
+
```javascript
|
|
339
|
+
message.preventACK();
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
to prevent the automatic acknowledgement.
|
|
343
|
+
|
|
344
|
+
Example:
|
|
345
|
+
|
|
346
|
+
```javascript
|
|
347
|
+
client.on("MESSAGE_CREATE", message => {
|
|
348
|
+
if(message.content === "keep this unread")
|
|
349
|
+
message.preventACK();
|
|
350
|
+
});
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
---
|
|
354
|
+
|
|
355
|
+
## `DISCONNECT`
|
|
356
|
+
|
|
357
|
+
Emitted when the Gateway connection closes.
|
|
358
|
+
|
|
359
|
+
```javascript
|
|
360
|
+
client.on("DISCONNECT", () => {
|
|
361
|
+
console.log("Disconnected");
|
|
362
|
+
});
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
---
|
|
366
|
+
|
|
367
|
+
## `INVALID_SESSION`
|
|
368
|
+
|
|
369
|
+
Emitted when Discord invalidates the current session and it cannot be resumed.
|
|
370
|
+
|
|
371
|
+
```javascript
|
|
372
|
+
client.on("INVALID_SESSION", () => {
|
|
373
|
+
console.log("Session invalidated");
|
|
374
|
+
});
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
---
|
|
378
|
+
|
|
379
|
+
# Messages
|
|
380
|
+
|
|
381
|
+
## `client.sendMessage(message)`
|
|
382
|
+
|
|
383
|
+
Sends a message to a channel.
|
|
384
|
+
|
|
385
|
+
### Parameters
|
|
386
|
+
|
|
387
|
+
| Name | Type | Description |
|
|
388
|
+
| --------------------------- | --------------- | -------------------------- |
|
|
389
|
+
| `message` | `Object` | Message data. |
|
|
390
|
+
| `message.channel_id` | `String` | Channel ID. |
|
|
391
|
+
| `message.content` | `String` | Message content. |
|
|
392
|
+
| `message.files` | `Array<Object>` | Optional file attachments. |
|
|
393
|
+
| `message.message_reference` | `Object` | Optional reply reference. |
|
|
394
|
+
|
|
395
|
+
### Reply reference
|
|
396
|
+
|
|
397
|
+
```javascript
|
|
398
|
+
{
|
|
399
|
+
id: "123456789",
|
|
400
|
+
channel_id: "987654321"
|
|
401
|
+
}
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### File
|
|
405
|
+
|
|
406
|
+
A file object can contain:
|
|
407
|
+
|
|
408
|
+
```javascript
|
|
409
|
+
{
|
|
410
|
+
filename: "image.png",
|
|
411
|
+
data: Buffer,
|
|
412
|
+
spoiled: false
|
|
413
|
+
}
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
### Example
|
|
417
|
+
|
|
418
|
+
```javascript
|
|
419
|
+
await client.sendMessage({
|
|
420
|
+
channel_id: "123456789",
|
|
421
|
+
content: "Hello!"
|
|
422
|
+
});
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
### Example with a file
|
|
426
|
+
|
|
427
|
+
```javascript
|
|
428
|
+
await client.sendMessage({
|
|
429
|
+
channel_id: "123456789",
|
|
430
|
+
content: "Here is a file",
|
|
431
|
+
files: [{
|
|
432
|
+
filename: "image.png",
|
|
433
|
+
data: require("fs").readFileSync("image.png"),
|
|
434
|
+
spoiled: false
|
|
435
|
+
}]
|
|
436
|
+
});
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
---
|
|
440
|
+
|
|
441
|
+
## `client.editMessage(message)`
|
|
442
|
+
|
|
443
|
+
Edits an existing message.
|
|
444
|
+
|
|
445
|
+
### Parameters
|
|
446
|
+
|
|
447
|
+
| Name | Type | Description |
|
|
448
|
+
| -------------------- | --------------- | ------------------------- |
|
|
449
|
+
| `message` | `Object` | Message data. |
|
|
450
|
+
| `message.id` | `String` | Message ID. |
|
|
451
|
+
| `message.channel_id` | `String` | Channel ID. |
|
|
452
|
+
| `message.content` | `String` | New message content. |
|
|
453
|
+
| `message.files` | `Array<Object>` | Optional new attachments. |
|
|
454
|
+
|
|
455
|
+
### Example
|
|
456
|
+
|
|
457
|
+
```javascript
|
|
458
|
+
await client.editMessage({
|
|
459
|
+
channel_id: "123456789",
|
|
460
|
+
id: "987654321",
|
|
461
|
+
content: "Edited message"
|
|
462
|
+
});
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
---
|
|
466
|
+
|
|
467
|
+
## `client.getMessages(options)`
|
|
468
|
+
|
|
469
|
+
Gets recent messages from a channel.
|
|
470
|
+
|
|
471
|
+
### Parameters
|
|
472
|
+
|
|
473
|
+
| Name | Type | Description |
|
|
474
|
+
| -------------------- | -------- | -------------------------------------------- |
|
|
475
|
+
| `options.channel_id` | `String` | Channel ID. |
|
|
476
|
+
| `options.limit` | `Number` | Maximum number of messages. |
|
|
477
|
+
| `options.before` | `String` | Only return messages before this message ID. |
|
|
478
|
+
|
|
479
|
+
### Returns
|
|
480
|
+
|
|
481
|
+
```javascript
|
|
482
|
+
Promise<Array>
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
Messages are returned from newest to oldest.
|
|
486
|
+
|
|
487
|
+
### Example
|
|
488
|
+
|
|
489
|
+
```javascript
|
|
490
|
+
const messages = await client.getMessages({
|
|
491
|
+
channel_id: "123456789",
|
|
492
|
+
limit: 25
|
|
493
|
+
});
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
---
|
|
497
|
+
|
|
498
|
+
## `client.ackMessage(message)`
|
|
499
|
+
|
|
500
|
+
Acknowledges a message, removing its unread notification.
|
|
501
|
+
|
|
502
|
+
### Parameters
|
|
503
|
+
|
|
504
|
+
| Name | Type | Description |
|
|
505
|
+
| -------------------- | -------- | ----------- |
|
|
506
|
+
| `message.channel_id` | `String` | Channel ID. |
|
|
507
|
+
| `message.id` | `String` | Message ID. |
|
|
508
|
+
|
|
509
|
+
### Returns
|
|
510
|
+
|
|
511
|
+
```javascript
|
|
512
|
+
Promise<Object>
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
### Example
|
|
516
|
+
|
|
517
|
+
```javascript
|
|
518
|
+
await client.ackMessage({
|
|
519
|
+
channel_id: "123456789",
|
|
520
|
+
id: "987654321"
|
|
521
|
+
});
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
---
|
|
525
|
+
|
|
526
|
+
# Reactions
|
|
527
|
+
|
|
528
|
+
## `client.addReaction(options)`
|
|
529
|
+
|
|
530
|
+
Adds a reaction to a message.
|
|
531
|
+
|
|
532
|
+
### Parameters
|
|
533
|
+
|
|
534
|
+
| Name | Type | Description |
|
|
535
|
+
| -------------------- | -------- | -------------------- |
|
|
536
|
+
| `options.channel_id` | `String` | Channel ID. |
|
|
537
|
+
| `options.message_id` | `String` | Message ID. |
|
|
538
|
+
| `options.emoji` | `String` | Emoji to react with. |
|
|
539
|
+
|
|
540
|
+
### Example
|
|
541
|
+
|
|
542
|
+
```javascript
|
|
543
|
+
await client.addReaction({
|
|
544
|
+
channel_id: "123456789",
|
|
545
|
+
message_id: "987654321",
|
|
546
|
+
emoji: "👍"
|
|
547
|
+
});
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
---
|
|
551
|
+
|
|
552
|
+
## `client.removeReaction(options)`
|
|
553
|
+
|
|
554
|
+
Removes the client's reaction from a message.
|
|
555
|
+
|
|
556
|
+
### Parameters
|
|
557
|
+
|
|
558
|
+
| Name | Type | Description |
|
|
559
|
+
| -------------------- | -------- | ---------------- |
|
|
560
|
+
| `options.channel_id` | `String` | Channel ID. |
|
|
561
|
+
| `options.message_id` | `String` | Message ID. |
|
|
562
|
+
| `options.emoji` | `String` | Emoji to remove. |
|
|
563
|
+
|
|
564
|
+
### Example
|
|
565
|
+
|
|
566
|
+
```javascript
|
|
567
|
+
await client.removeReaction({
|
|
568
|
+
channel_id: "123456789",
|
|
569
|
+
message_id: "987654321",
|
|
570
|
+
emoji: "👍"
|
|
571
|
+
});
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
---
|
|
575
|
+
|
|
576
|
+
# Search
|
|
577
|
+
|
|
578
|
+
## `client.search(options)`
|
|
579
|
+
|
|
580
|
+
Searches for messages in a channel.
|
|
581
|
+
|
|
582
|
+
### Parameters
|
|
583
|
+
|
|
584
|
+
| Name | Type | Description |
|
|
585
|
+
| ---------------------- | ------------------------------- | ---------------------------------------- |
|
|
586
|
+
| `options.channel_id` | `String` | Channel ID. |
|
|
587
|
+
| `options.content` | `String` | Search message content. |
|
|
588
|
+
| `options.authors` | `Array<String>` | Filter by author IDs. |
|
|
589
|
+
| `options.mentions` | `Array<String>` | Filter by mentioned user IDs. |
|
|
590
|
+
| `options.contentTypes` | `Array<String>` | Filter by content type. |
|
|
591
|
+
| `options.pinned` | `Boolean` | Only return pinned messages. |
|
|
592
|
+
| `options.authorTypes` | `Array<String>` | Filter by author type. |
|
|
593
|
+
| `options.sort` | `"new" \| "old" \| "relevance"` | Search sorting mode. |
|
|
594
|
+
| `options.offset` | `Number` | Number of results to skip. |
|
|
595
|
+
| `options.after` | `Number` | Only messages after this UTC timestamp. |
|
|
596
|
+
| `options.before` | `Number` | Only messages before this UTC timestamp. |
|
|
597
|
+
|
|
598
|
+
### Content types
|
|
599
|
+
|
|
600
|
+
Valid `contentTypes` values include:
|
|
601
|
+
|
|
602
|
+
```text
|
|
603
|
+
image
|
|
604
|
+
video
|
|
605
|
+
link
|
|
606
|
+
file
|
|
607
|
+
embed
|
|
608
|
+
sound
|
|
609
|
+
poll
|
|
610
|
+
sticker
|
|
611
|
+
snapshot
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
### Author types
|
|
615
|
+
|
|
616
|
+
Valid `authorTypes` values include:
|
|
617
|
+
|
|
618
|
+
```text
|
|
619
|
+
user
|
|
620
|
+
bot
|
|
621
|
+
webhook
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
### Sorting
|
|
625
|
+
|
|
626
|
+
```javascript
|
|
627
|
+
sort: "new"
|
|
628
|
+
sort: "old"
|
|
629
|
+
sort: "relevance"
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
### Example
|
|
633
|
+
|
|
634
|
+
```javascript
|
|
635
|
+
const results = await client.search({
|
|
636
|
+
channel_id: "123456789",
|
|
637
|
+
content: "hello",
|
|
638
|
+
sort: "relevance"
|
|
639
|
+
});
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
### Date filtering
|
|
643
|
+
|
|
644
|
+
`after` and `before` use UTC timestamps in milliseconds.
|
|
645
|
+
|
|
646
|
+
```javascript
|
|
647
|
+
const results = await client.search({
|
|
648
|
+
channel_id: "123456789",
|
|
649
|
+
after: Date.now() - 86400000
|
|
650
|
+
});
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
---
|
|
654
|
+
|
|
655
|
+
# Channels
|
|
656
|
+
|
|
657
|
+
## `client.getOpenChannels()`
|
|
658
|
+
|
|
659
|
+
Gets the channels currently present in the account's DM list.
|
|
660
|
+
|
|
661
|
+
This can contain both direct messages and group DMs.
|
|
662
|
+
|
|
663
|
+
### Returns
|
|
664
|
+
|
|
665
|
+
```javascript
|
|
666
|
+
Promise<Object>
|
|
667
|
+
```
|
|
668
|
+
|
|
669
|
+
### Example
|
|
670
|
+
|
|
671
|
+
```javascript
|
|
672
|
+
const channels = await client.getOpenChannels();
|
|
673
|
+
console.log(channels);
|
|
674
|
+
```
|
|
675
|
+
|
|
676
|
+
---
|
|
677
|
+
|
|
678
|
+
# Status
|
|
679
|
+
|
|
680
|
+
## `client.setStatus(options)`
|
|
681
|
+
|
|
682
|
+
Sets the account's status and activities.
|
|
683
|
+
|
|
684
|
+
### Parameters
|
|
685
|
+
|
|
686
|
+
| Name | Type | Description |
|
|
687
|
+
| -------------------- | -------------------------------------------- | ---------------------- |
|
|
688
|
+
| `options.status` | `"online" \| "idle" \| "dnd" \| "invisible"` | Account status. |
|
|
689
|
+
| `options.activities` | `Array<Object>` | Activities to display. |
|
|
690
|
+
|
|
691
|
+
Activity objects contain:
|
|
692
|
+
|
|
693
|
+
```javascript
|
|
694
|
+
{
|
|
695
|
+
type: Number,
|
|
696
|
+
name: String
|
|
697
|
+
}
|
|
698
|
+
```
|
|
699
|
+
|
|
700
|
+
### Example
|
|
701
|
+
|
|
702
|
+
```javascript
|
|
703
|
+
client.setStatus({
|
|
704
|
+
status: "dnd",
|
|
705
|
+
|
|
706
|
+
activities: [{
|
|
707
|
+
name: "with the Discord API",
|
|
708
|
+
type: Status.PLAYING
|
|
709
|
+
}]
|
|
710
|
+
});
|
|
711
|
+
```
|
|
712
|
+
|
|
713
|
+
### Custom status
|
|
714
|
+
|
|
715
|
+
```javascript
|
|
716
|
+
client.setStatus({
|
|
717
|
+
status: "online",
|
|
718
|
+
|
|
719
|
+
activities: [{
|
|
720
|
+
name: "my custom status",
|
|
721
|
+
type: Status.CUSTOM_STATUS
|
|
722
|
+
}]
|
|
723
|
+
});
|
|
724
|
+
```
|
|
725
|
+
|
|
726
|
+
### Streaming
|
|
727
|
+
|
|
728
|
+
Streaming activities automatically receive the activity's name as `details`.
|
|
729
|
+
|
|
730
|
+
```javascript
|
|
731
|
+
client.setStatus({
|
|
732
|
+
status: "online",
|
|
733
|
+
|
|
734
|
+
activities: [{
|
|
735
|
+
name: "Minecraft",
|
|
736
|
+
type: Status.STREAMING
|
|
737
|
+
}]
|
|
738
|
+
});
|
|
739
|
+
```
|
|
740
|
+
|
|
741
|
+
---
|
|
742
|
+
|
|
743
|
+
# Authentication
|
|
744
|
+
|
|
745
|
+
## `client.logout()`
|
|
746
|
+
|
|
747
|
+
Logs out the current account and closes the Gateway connection.
|
|
748
|
+
|
|
749
|
+
### Returns
|
|
750
|
+
|
|
751
|
+
```javascript
|
|
752
|
+
Promise<Object>
|
|
753
|
+
```
|
|
754
|
+
|
|
755
|
+
### Example
|
|
756
|
+
|
|
757
|
+
```javascript
|
|
758
|
+
await client.logout();
|
|
759
|
+
```
|
|
760
|
+
|
|
761
|
+
---
|
|
762
|
+
|
|
763
|
+
# Connection
|
|
764
|
+
|
|
765
|
+
## `client.disconnect(code)`
|
|
766
|
+
|
|
767
|
+
Closes the current Gateway session.
|
|
768
|
+
|
|
769
|
+
After disconnecting, the client will no longer receive Gateway events. Call `login()` again to create a new session.
|
|
770
|
+
|
|
771
|
+
### Parameters
|
|
772
|
+
|
|
773
|
+
| Name | Type | Default | Description |
|
|
774
|
+
| ------ | -------- | ------- | --------------------- |
|
|
775
|
+
| `code` | `Number` | `1000` | WebSocket close code. |
|
|
776
|
+
|
|
777
|
+
### Example
|
|
778
|
+
|
|
779
|
+
```javascript
|
|
780
|
+
client.disconnect();
|
|
781
|
+
```
|
|
782
|
+
|
|
783
|
+
Or with a specific close code:
|
|
784
|
+
|
|
785
|
+
```javascript
|
|
786
|
+
client.disconnect(1000);
|
|
787
|
+
```
|
|
788
|
+
|
|
789
|
+
---
|
|
790
|
+
|
|
791
|
+
# Complete Example
|
|
792
|
+
|
|
793
|
+
```javascript
|
|
794
|
+
const self = require("@imaentity/selfjs");
|
|
795
|
+
|
|
796
|
+
const client = new self.Client({
|
|
797
|
+
debugLogs: true
|
|
798
|
+
});
|
|
799
|
+
|
|
800
|
+
client.on("READY", data => {
|
|
801
|
+
console.log(`Logged in as ${data.user.username}`);
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
client.on("MESSAGE_CREATE", async message => {
|
|
805
|
+
console.log(`${message.author.username}: ${message.content}`);
|
|
806
|
+
|
|
807
|
+
if(message.content === "!hello") {
|
|
808
|
+
await client.sendMessage({
|
|
809
|
+
channel_id: message.channel_id,
|
|
810
|
+
content: "Hello!"
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
});
|
|
814
|
+
|
|
815
|
+
client.on("DISCONNECT", () => {
|
|
816
|
+
console.log("Disconnected");
|
|
817
|
+
});
|
|
818
|
+
|
|
819
|
+
client.login(process.env.DISCORD_TOKEN);
|
|
820
|
+
```
|
|
821
|
+
|
|
822
|
+
---
|
|
823
|
+
|
|
824
|
+
# Exported API
|
|
825
|
+
|
|
826
|
+
SelfJS currently exports:
|
|
827
|
+
|
|
828
|
+
```javascript
|
|
829
|
+
module.exports = {
|
|
830
|
+
Status,
|
|
831
|
+
validateToken,
|
|
832
|
+
createToken,
|
|
833
|
+
UTCToSnowflake,
|
|
834
|
+
snowflakeToUTC,
|
|
835
|
+
Client
|
|
836
|
+
};
|
|
837
|
+
```
|
|
838
|
+
|
|
839
|
+
So the following are available:
|
|
840
|
+
|
|
841
|
+
```javascript
|
|
842
|
+
self.Status
|
|
843
|
+
self.validateToken
|
|
844
|
+
self.createToken
|
|
845
|
+
self.UTCToSnowflake
|
|
846
|
+
self.snowflakeToUTC
|
|
847
|
+
self.Client
|
|
848
|
+
```
|