@imaentity/selfjs 4.0.0 → 4.1.0

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.
Files changed (4) hide show
  1. package/README.md +12 -10
  2. package/docs.md +1142 -848
  3. package/package.json +1 -1
  4. package/self.js +151 -3
package/docs.md CHANGED
@@ -1,848 +1,1142 @@
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. |
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
- ```
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
+ # Constants
21
+
22
+ ## `Status`
23
+
24
+ Contains the activity types used by Discord.
25
+
26
+ ```javascript
27
+ Status.PLAYING // 0
28
+ Status.STREAMING // 1
29
+ Status.LISTENING // 2
30
+ Status.WATCHING // 3
31
+ Status.CUSTOM_STATUS // 4
32
+ Status.COMPETING // 5
33
+ ```
34
+
35
+ # Functions
36
+
37
+ ## `validateToken(token)`
38
+
39
+ Checks whether a Discord token is valid without creating a persistent `Client`.
40
+
41
+ Returns the user's Discord user object if valid, or `null` if the token is invalid.
42
+
43
+ ### Parameters
44
+
45
+ | Name | Type | Description |
46
+ | ------- | -------- | ------------------------------ |
47
+ | `token` | `String` | The Discord token to validate. |
48
+
49
+ ### Returns
50
+
51
+ ```javascript
52
+ Promise<Object | null>
53
+ ```
54
+
55
+ ### Example
56
+
57
+ ```javascript
58
+ const user = await self.validateToken(token);
59
+
60
+ if(user)
61
+ console.log(`Logged in as ${user.username}`);
62
+ else
63
+ console.log("Invalid token");
64
+ ```
65
+
66
+ ## `createToken(options)`
67
+
68
+ Attempts to log into a Discord account using an email and password.
69
+ If MFA is not enabled, the returned object contains the token immediately.
70
+ If MFA is required, the returned object contains the available MFA methods and a `confirmMFA()` function.
71
+
72
+ ### Parameters
73
+
74
+ | Name | Type | Description |
75
+ | ------------------ | -------- | ----------------- |
76
+ | `options` | `Object` | Login options. |
77
+ | `options.email` | `String` | Account email. |
78
+ | `options.password` | `String` | Account password. |
79
+
80
+ ### Returns
81
+
82
+ ```javascript
83
+ Promise<Object | null>
84
+ ```
85
+
86
+ ### Successful login
87
+
88
+ ```javascript
89
+ {
90
+ token: String,
91
+ user_id: String,
92
+ mfaRequired: false
93
+ }
94
+ ```
95
+
96
+ ### MFA login
97
+
98
+ ```javascript
99
+ {
100
+ user_id: String,
101
+ mfaRequired: true,
102
+ mfaMethods: Array<String>,
103
+ confirmMFA: Function
104
+ }
105
+ ```
106
+
107
+ ### Example
108
+
109
+ ```javascript
110
+ const login = await self.createToken({
111
+ email: "discord@example.com",
112
+ password: "password"
113
+ });
114
+
115
+ if(!login)
116
+ throw new Error("Login failed");
117
+
118
+ if(!login.mfaRequired) {
119
+ console.log(login.token);
120
+ } else {
121
+ console.log("MFA methods:", login.mfaMethods);
122
+
123
+ const result = await login.confirmMFA("totp", {
124
+ code: "123456"
125
+ });
126
+
127
+ console.log(result);
128
+ }
129
+ ```
130
+
131
+ > [!NOTE]
132
+ > SMS MFA is currently unsupported.
133
+
134
+ ## `snowflakeToUTC(snowflake)`
135
+
136
+ Converts a Discord snowflake into a UTC timestamp.
137
+ The returned timestamp is in milliseconds since Unix epoch.
138
+
139
+ ### Parameters
140
+
141
+ | Name | Type | Description |
142
+ | ----------- | -------- | ------------------ |
143
+ | `snowflake` | `String` | Discord snowflake. |
144
+
145
+ ### Returns
146
+
147
+ ```javascript
148
+ Number
149
+ ```
150
+
151
+ ### Example
152
+
153
+ ```javascript
154
+ const timestamp = self.snowflakeToUTC("1329029486758592595");
155
+
156
+ console.log(new Date(timestamp));
157
+ ```
158
+
159
+ ## `UTCToSnowflake(timestamp)`
160
+
161
+ Converts a UTC timestamp into a Discord snowflake.
162
+ The generated snowflake only contains the timestamp portion. Worker ID, process ID, and sequence values are zero.
163
+
164
+ ### Parameters
165
+
166
+ | Name | Type | Description |
167
+ | ----------- | -------- | ------------------------------ |
168
+ | `timestamp` | `Number` | UTC timestamp in milliseconds. |
169
+
170
+ ### Returns
171
+
172
+ ```javascript
173
+ String
174
+ ```
175
+
176
+ ### Example
177
+
178
+ ```javascript
179
+ const snowflake = self.UTCToSnowflake(Date.now());
180
+
181
+ console.log(snowflake);
182
+ ```
183
+
184
+ # Client
185
+
186
+ `Client` provides an interface for connecting to Discord and interacting with the account.
187
+
188
+ ## Creating a client
189
+
190
+ ```javascript
191
+ const client = new self.Client();
192
+ ```
193
+
194
+ ### Options
195
+
196
+ ```javascript
197
+ const client = new self.Client({
198
+ properties: {
199
+ os: process.platform,
200
+ browser: "SelfJS",
201
+ device: "NodeJS"
202
+ },
203
+
204
+ debugLogs: true,
205
+
206
+ intents: 0
207
+ });
208
+ ```
209
+
210
+ | Option | Type | Default | Description |
211
+ | ------------ | --------- | ------------- | --------------------------------------- |
212
+ | `properties` | `Object` | `LOGIN_PROPS` | Gateway identify properties. |
213
+ | `debugLogs` | `Boolean` | `true` | Enables SelfJS debug logging. |
214
+ | `intents` | `Number` | `null` | Gateway intents, used for bot accounts. |
215
+
216
+ # Properties
217
+
218
+ ## `client.user`
219
+
220
+ The user object belonging to the logged-in account.
221
+
222
+ ```javascript
223
+ console.log(client.user);
224
+ ```
225
+
226
+ This is populated after the `READY` event.
227
+
228
+ ## `client.token`
229
+
230
+ The token currently being used by the client.
231
+
232
+ ```javascript
233
+ console.log(client.token);
234
+ ```
235
+
236
+ ## `client.latency`
237
+
238
+ The time between sending a heartbeat and receiving its acknowledgement.
239
+
240
+ ```javascript
241
+ console.log(client.latency);
242
+ ```
243
+
244
+ The value is in milliseconds.
245
+
246
+ # Login
247
+
248
+ ## `client.login(token)`
249
+
250
+ Connects the client to the Discord Gateway using the provided token.
251
+
252
+ ### Parameters
253
+
254
+ | Name | Type | Description |
255
+ | ------- | -------- | ---------------------- |
256
+ | `token` | `String` | Discord account token. |
257
+
258
+ ### Example
259
+
260
+ ```javascript
261
+ client.login(token);
262
+ ```
263
+
264
+ Once connected, events can be received using `client.on()`.
265
+
266
+ # Events
267
+
268
+ `Client` extends Node.js `EventEmitter`, so events can be listened to using `.on()`.
269
+
270
+ ```javascript
271
+ client.on("MESSAGE_CREATE", message => {
272
+ console.log(message.content);
273
+ });
274
+ ```
275
+
276
+ ## `READY`
277
+
278
+ Emitted when the client successfully logs in.
279
+
280
+ ```javascript
281
+ client.on("READY", data => {
282
+ console.log("Logged in as:", data.user.username);
283
+ });
284
+ ```
285
+
286
+ ## `MESSAGE_CREATE`
287
+
288
+ Emitted when a message is received.
289
+
290
+ ```javascript
291
+ client.on("MESSAGE_CREATE", message => {
292
+ console.log(message.content);
293
+ });
294
+ ```
295
+
296
+ The message object also receives:
297
+
298
+ ```javascript
299
+ message.author.self
300
+ ```
301
+
302
+ which is `true` when the message was sent by the current account.
303
+
304
+ ### Preventing automatic acknowledgement
305
+
306
+ Messages are automatically acknowledged unless they were sent by the current account.
307
+
308
+ Call:
309
+
310
+ ```javascript
311
+ message.preventACK();
312
+ ```
313
+
314
+ to prevent the automatic acknowledgement.
315
+
316
+ Example:
317
+
318
+ ```javascript
319
+ client.on("MESSAGE_CREATE", message => {
320
+ if(message.content === "keep this unread")
321
+ message.preventACK();
322
+ });
323
+ ```
324
+
325
+ ## `DISCONNECT`
326
+
327
+ Emitted when the Gateway connection closes.
328
+
329
+ ```javascript
330
+ client.on("DISCONNECT", () => {
331
+ console.log("Disconnected");
332
+ });
333
+ ```
334
+
335
+ ## `INVALID_SESSION`
336
+
337
+ Emitted when Discord invalidates the current session and it cannot be resumed.
338
+
339
+ ```javascript
340
+ client.on("INVALID_SESSION", () => {
341
+ console.log("Session invalidated");
342
+ });
343
+ ```
344
+
345
+ # Messages
346
+
347
+ ## `client.sendMessage(message)`
348
+
349
+ Sends a message to a channel.
350
+
351
+ ### Parameters
352
+
353
+ | Name | Type | Description |
354
+ | --------------------------- | --------------- | -------------------------- |
355
+ | `message` | `Object` | Message data. |
356
+ | `message.channel_id` | `String` | Channel ID. |
357
+ | `message.content` | `String` | Message content. |
358
+ | `message.files` | `Array<Object>` | Optional file attachments. |
359
+ | `message.message_reference` | `Object` | Optional reply reference. |
360
+
361
+ ### Reply reference
362
+
363
+ ```javascript
364
+ {
365
+ id: "123456789",
366
+ channel_id: "987654321"
367
+ }
368
+ ```
369
+
370
+ ### File
371
+
372
+ A file object can contain:
373
+
374
+ ```javascript
375
+ {
376
+ filename: "image.png",
377
+ data: Buffer,
378
+ spoiled: false
379
+ }
380
+ ```
381
+
382
+ ### Example
383
+
384
+ ```javascript
385
+ await client.sendMessage({
386
+ channel_id: "123456789",
387
+ content: "Hello!"
388
+ });
389
+ ```
390
+
391
+ ### Example with a file
392
+
393
+ ```javascript
394
+ await client.sendMessage({
395
+ channel_id: "123456789",
396
+ content: "Here is a file",
397
+ files: [{
398
+ filename: "image.png",
399
+ data: require("fs").readFileSync("image.png"),
400
+ spoiled: false
401
+ }]
402
+ });
403
+ ```
404
+
405
+ ## `client.editMessage(message)`
406
+
407
+ Edits an existing message.
408
+
409
+ ### Parameters
410
+
411
+ | Name | Type | Description |
412
+ | -------------------- | --------------- | ------------------------- |
413
+ | `message` | `Object` | Message data. |
414
+ | `message.id` | `String` | Message ID. |
415
+ | `message.channel_id` | `String` | Channel ID. |
416
+ | `message.content` | `String` | New message content. |
417
+ | `message.files` | `Array<Object>` | Optional new attachments. |
418
+
419
+ ### Example
420
+
421
+ ```javascript
422
+ await client.editMessage({
423
+ channel_id: "123456789",
424
+ id: "987654321",
425
+ content: "Edited message"
426
+ });
427
+ ```
428
+
429
+ ## `client.getMessages(options)`
430
+
431
+ Gets recent messages from a channel.
432
+
433
+ ### Parameters
434
+
435
+ | Name | Type | Description |
436
+ | -------------------- | -------- | -------------------------------------------- |
437
+ | `options.channel_id` | `String` | Channel ID. |
438
+ | `options.limit` | `Number` | Maximum number of messages. |
439
+ | `options.before` | `String` | Only return messages before this message ID. |
440
+
441
+ ### Returns
442
+
443
+ ```javascript
444
+ Promise<Array>
445
+ ```
446
+
447
+ Messages are returned from newest to oldest.
448
+
449
+ ### Example
450
+
451
+ ```javascript
452
+ const messages = await client.getMessages({
453
+ channel_id: "123456789",
454
+ limit: 25
455
+ });
456
+ ```
457
+
458
+ ## `client.ackMessage(message)`
459
+
460
+ Acknowledges a message, removing its unread notification.
461
+
462
+ ### Parameters
463
+
464
+ | Name | Type | Description |
465
+ | -------------------- | -------- | ----------- |
466
+ | `message.channel_id` | `String` | Channel ID. |
467
+ | `message.id` | `String` | Message ID. |
468
+
469
+ ### Returns
470
+
471
+ ```javascript
472
+ Promise<Object>
473
+ ```
474
+
475
+ ### Example
476
+
477
+ ```javascript
478
+ await client.ackMessage({
479
+ channel_id: "123456789",
480
+ id: "987654321"
481
+ });
482
+ ```
483
+
484
+ # Reactions
485
+
486
+ ## `client.addReaction(options)`
487
+
488
+ Adds a reaction to a message.
489
+
490
+ ### Parameters
491
+
492
+ | Name | Type | Description |
493
+ | -------------------- | -------- | -------------------- |
494
+ | `options.channel_id` | `String` | Channel ID. |
495
+ | `options.message_id` | `String` | Message ID. |
496
+ | `options.emoji` | `String` | Emoji to react with. |
497
+
498
+ ### Example
499
+
500
+ ```javascript
501
+ await client.addReaction({
502
+ channel_id: "123456789",
503
+ message_id: "987654321",
504
+ emoji: "👍"
505
+ });
506
+ ```
507
+
508
+ ## `client.removeReaction(options)`
509
+
510
+ Removes the client's reaction from a message.
511
+
512
+ ### Parameters
513
+
514
+ | Name | Type | Description |
515
+ | -------------------- | -------- | ---------------- |
516
+ | `options.channel_id` | `String` | Channel ID. |
517
+ | `options.message_id` | `String` | Message ID. |
518
+ | `options.emoji` | `String` | Emoji to remove. |
519
+
520
+ ### Example
521
+
522
+ ```javascript
523
+ await client.removeReaction({
524
+ channel_id: "123456789",
525
+ message_id: "987654321",
526
+ emoji: "👍"
527
+ });
528
+ ```
529
+
530
+ # Channels
531
+
532
+ ## `client.getOpenChannels()`
533
+
534
+ Gets the channels currently present in the account's DM list.
535
+ This can contain both direct messages and group DMs.
536
+
537
+ ### Returns
538
+
539
+ ```javascript
540
+ Promise<Object>
541
+ ```
542
+
543
+ ### Example
544
+
545
+ ```javascript
546
+ const channels = await client.getOpenChannels();
547
+ console.log(channels);
548
+ ```
549
+
550
+ ## `client.search(options)`
551
+
552
+ Searches for messages in a channel.
553
+
554
+ ### Parameters
555
+
556
+ | Name | Type | Description |
557
+ | ---------------------- | ------------------------------- | ---------------------------------------- |
558
+ | `options.channel_id` | `String` | Channel ID. |
559
+ | `options.content` | `String` | Search message content. |
560
+ | `options.authors` | `Array<String>` | Filter by author IDs. |
561
+ | `options.mentions` | `Array<String>` | Filter by mentioned user IDs. |
562
+ | `options.contentTypes` | `Array<String>` | Filter by content type. |
563
+ | `options.pinned` | `Boolean` | Only return pinned messages. |
564
+ | `options.authorTypes` | `Array<String>` | Filter by author type. |
565
+ | `options.sort` | `"new" \| "old" \| "relevance"` | Search sorting mode. |
566
+ | `options.offset` | `Number` | Number of results to skip. |
567
+ | `options.after` | `Number` | Only messages after this UTC timestamp. |
568
+ | `options.before` | `Number` | Only messages before this UTC timestamp. |
569
+
570
+ ### Content types
571
+
572
+ Valid `contentTypes` values include:
573
+
574
+ ```text
575
+ image
576
+ video
577
+ link
578
+ file
579
+ embed
580
+ sound
581
+ poll
582
+ sticker
583
+ snapshot
584
+ ```
585
+
586
+ ### Author types
587
+
588
+ Valid `authorTypes` values include:
589
+
590
+ ```text
591
+ user
592
+ bot
593
+ webhook
594
+ ```
595
+
596
+ ### Sorting
597
+
598
+ ```javascript
599
+ sort: "new"
600
+ sort: "old"
601
+ sort: "relevance"
602
+ ```
603
+
604
+ ### Example
605
+
606
+ ```javascript
607
+ const results = await client.search({
608
+ channel_id: "123456789",
609
+ content: "hello",
610
+ sort: "relevance"
611
+ });
612
+ ```
613
+
614
+ ### Date filtering
615
+
616
+ `after` and `before` use UTC timestamps in milliseconds.
617
+
618
+ ```javascript
619
+ const results = await client.search({
620
+ channel_id: "123456789",
621
+ after: Date.now() - 86400000
622
+ });
623
+ ```
624
+
625
+
626
+ ## `client.createGroupDM(recipients)`
627
+
628
+ Creates a group DM with the specified users.
629
+
630
+ ### Parameters
631
+
632
+ | Name | Type | Description |
633
+ | ------------ | --------------- | ----------------------------- |
634
+ | `recipients` | `Array<String>` | User IDs to add to the group. |
635
+
636
+ ### Returns
637
+
638
+ ```javascript
639
+ Promise<Object>
640
+ ```
641
+
642
+ The new channel object.
643
+
644
+ ### Example
645
+
646
+ ```javascript
647
+ const group = await client.createGroupDM([
648
+ "123456789",
649
+ "987654321"
650
+ ]);
651
+
652
+ console.log(group.id);
653
+ ```
654
+
655
+ ## `client.addToGroup(options)`
656
+
657
+ Adds a user to a group DM. The logged-in account must be friends with the user.
658
+
659
+ ### Parameters
660
+
661
+ | Name | Type | Description |
662
+ | -------------------- | -------- | -------------------- |
663
+ | `options.channel_id` | `String` | Group DM channel ID. |
664
+ | `options.user_id` | `String` | User ID to add. |
665
+
666
+ ### Returns
667
+
668
+ ```javascript
669
+ Promise<void>
670
+ ```
671
+
672
+ ### Example
673
+
674
+ ```javascript
675
+ await client.addToGroup({
676
+ channel_id: "123456789",
677
+ user_id: "987654321"
678
+ });
679
+ ```
680
+
681
+ ## `client.removeFromGroup(options)`
682
+
683
+ Removes a user from a group DM. The logged-in account must own the group.
684
+
685
+ ### Parameters
686
+
687
+ | Name | Type | Description |
688
+ | -------------------- | -------- | -------------------- |
689
+ | `options.channel_id` | `String` | Group DM channel ID. |
690
+ | `options.user_id` | `String` | User ID to remove. |
691
+
692
+ ### Returns
693
+
694
+ ```javascript
695
+ Promise<void>
696
+ ```
697
+
698
+ ### Example
699
+
700
+ ```javascript
701
+ await client.removeFromGroup({
702
+ channel_id: "123456789",
703
+ user_id: "987654321"
704
+ });
705
+ ```
706
+
707
+ ## `client.transferGroup(options)`
708
+
709
+ Transfers ownership of a group DM to another member.
710
+
711
+ ### Parameters
712
+
713
+ | Name | Type | Description |
714
+ | -------------------- | -------- | ----------------------------- |
715
+ | `options.channel_id` | `String` | Group DM channel ID. |
716
+ | `options.user_id` | `String` | User ID to give ownership to. |
717
+
718
+ ### Returns
719
+
720
+ ```javascript
721
+ Promise<Object>
722
+ ```
723
+
724
+ The updated channel object.
725
+
726
+ ### Example
727
+
728
+ ```javascript
729
+ const group = await client.transferGroup({
730
+ channel_id: "123456789",
731
+ user_id: "987654321"
732
+ });
733
+
734
+ console.log(group.owner_id);
735
+ ```
736
+
737
+ ## `client.leaveGroup(options)`
738
+
739
+ Leaves a group DM.
740
+
741
+ ### Parameters
742
+
743
+ | Name | Type | Description |
744
+ | -------------------- | --------- | -------------------------------------------------- |
745
+ | `options.channel_id` | `String` | Group DM channel ID. |
746
+ | `options.silent` | `Boolean` | If `true`, does not notify the group of the leave. |
747
+
748
+ ### Returns
749
+
750
+ ```javascript
751
+ Promise<Object>
752
+ ```
753
+
754
+ ### Example
755
+
756
+ ```javascript
757
+ await client.leaveGroup({
758
+ channel_id: "123456789",
759
+ silent: true
760
+ });
761
+ ```
762
+
763
+ ## `client.startTyping(channel_id)`
764
+
765
+ Starts the typing indicator in a channel.
766
+ The indicator lasts for 10 seconds. Calling this again before it expires resets the timer. Sending a message clears the indicator.
767
+
768
+ ### Parameters
769
+
770
+ | Name | Type | Description |
771
+ | ------------ | -------- | ------------------------------ |
772
+ | `channel_id` | `String` | Channel ID to start typing in. |
773
+
774
+ ### Returns
775
+
776
+ ```javascript
777
+ Promise<void>
778
+ ```
779
+
780
+ ### Example
781
+
782
+ ```javascript
783
+ await client.startTyping("123456789");
784
+ ```
785
+
786
+ ## `client.pinMessage(options)`
787
+
788
+ Pins a message in a channel.
789
+
790
+ ### Parameters
791
+
792
+ | Name | Type | Description |
793
+ | -------------------- | -------- | ----------- |
794
+ | `options.channel_id` | `String` | Channel ID. |
795
+ | `options.message_id` | `String` | Message ID. |
796
+
797
+ ### Returns
798
+
799
+ ```javascript
800
+ Promise<void>
801
+ ```
802
+
803
+ ### Example
804
+
805
+ ```javascript
806
+ await client.pinMessage({
807
+ channel_id: "123456789",
808
+ message_id: "987654321"
809
+ });
810
+ ```
811
+
812
+ ## `client.unpinMessage(options)`
813
+
814
+ Unpins a message from a channel.
815
+
816
+ ### Parameters
817
+
818
+ | Name | Type | Description |
819
+ | -------------------- | -------- | ----------- |
820
+ | `options.channel_id` | `String` | Channel ID. |
821
+ | `options.message_id` | `String` | Message ID. |
822
+
823
+ ### Returns
824
+
825
+ ```javascript
826
+ Promise<void>
827
+ ```
828
+
829
+ ### Example
830
+
831
+ ```javascript
832
+ await client.unpinMessage({
833
+ channel_id: "123456789",
834
+ message_id: "987654321"
835
+ });
836
+ ```
837
+
838
+ ## `client.getChannelObject(channel_id)`
839
+
840
+ Gets the current channel object using its ID.
841
+ This works for DMs, group DMs, server text channels, and server voice channels.
842
+
843
+ ### Parameters
844
+
845
+ | Name | Type | Description |
846
+ | ------------ | -------- | ----------- |
847
+ | `channel_id` | `String` | Channel ID. |
848
+
849
+ ### Returns
850
+
851
+ ```javascript
852
+ Promise<Object>
853
+ ```
854
+
855
+ The current channel object.
856
+
857
+ ### Example
858
+
859
+ ```javascript
860
+ const channel = await client.getChannelObject("123456789");
861
+
862
+ console.log(channel);
863
+ ```
864
+
865
+ ## `client.getDMChannel(user_id)`
866
+
867
+ Gets the channel object for DMs with a certain user
868
+ Getting the channel object also opens the channel in the active DM list
869
+
870
+ ### Parameters
871
+
872
+ | Name | Type | Description |
873
+ | --------- | -------- | ----------- |
874
+ | `user_id` | `String` | User ID. |
875
+
876
+ ### Returns
877
+
878
+ ```javascript
879
+ Promise<Object>
880
+ ```
881
+
882
+ The DM channel object.
883
+
884
+ ### Example
885
+
886
+ ```javascript
887
+ const channel = await client.getDMChannel("123456789");
888
+
889
+ console.log(channel);
890
+ ```
891
+
892
+ ## `client.closeDMChannel(user_id)`
893
+
894
+ Closes and hides the DM channel for a certain user from the active list
895
+
896
+ ### Parameters
897
+
898
+ | Name | Type | Description |
899
+ | --------- | -------- | ----------- |
900
+ | `user_id` | `String` | User ID. |
901
+
902
+ ### Returns
903
+
904
+ ```javascript
905
+ Promise<Object>
906
+ ```
907
+
908
+ The DM channel object.
909
+
910
+ ### Example
911
+
912
+ ```javascript
913
+ const channel = await client.getDMChannel("123456789");
914
+
915
+ console.log(channel);
916
+ ```
917
+
918
+ # Users
919
+
920
+ ## `client.getUserProfile(options)`
921
+
922
+ Gets a user's profile.
923
+ The request succeeds if at least one of these is true:
924
+
925
+ * The client shares a server with the user.
926
+ * The client is friends with the user.
927
+ * The user has sent a friend request to the client.
928
+ * The user is a bot.
929
+
930
+ ### Parameters
931
+
932
+ | Name | Type | Description |
933
+ | ----------------------------------- | --------- | ---------------------------------------------- |
934
+ | `options` | `Object` | Profile request options. |
935
+ | `options.user_id` | `String` | ID of the user to get. |
936
+ | `options.with_mutual_guilds` | `Boolean` | Include mutual servers. Defaults to `true`. |
937
+ | `options.with_mutual_friends` | `Boolean` | Include mutual friends. |
938
+ | `options.with_mutual_friends_count` | `Boolean` | Include the number of mutual friends. |
939
+ | `options.guild_id` | `String` | Get the user's server profile for this server. |
940
+
941
+ ### Returns
942
+
943
+ ```javascript
944
+ Promise<Object>
945
+ ```
946
+
947
+ The user's profile.
948
+
949
+ If none of the access conditions are met, Discord returns a `404` error.
950
+
951
+ ### Example
952
+
953
+ ```javascript
954
+ const profile = await client.getUserProfile({
955
+ user_id: "123456789"
956
+ });
957
+
958
+ console.log(profile);
959
+ ```
960
+
961
+ ### Example with mutual friends
962
+
963
+ ```javascript
964
+ const profile = await client.getUserProfile({
965
+ user_id: "123456789",
966
+ with_mutual_friends: true,
967
+ with_mutual_friends_count: true
968
+ });
969
+ ```
970
+
971
+ ### Example with a server profile
972
+
973
+ ```javascript
974
+ const profile = await client.getUserProfile({
975
+ user_id: "123456789",
976
+ guild_id: "987654321"
977
+ });
978
+ ```
979
+
980
+ # Status
981
+
982
+ ## `client.setStatus(options)`
983
+
984
+ Sets the account's status and activities.
985
+
986
+ ### Parameters
987
+
988
+ | Name | Type | Description |
989
+ | -------------------- | -------------------------------------------- | ---------------------- |
990
+ | `options.status` | `"online" \| "idle" \| "dnd" \| "invisible"` | Account status. |
991
+ | `options.activities` | `Array<Object>` | Activities to display. |
992
+
993
+ Activity objects contain:
994
+
995
+ ```javascript
996
+ {
997
+ type: Number,
998
+ name: String
999
+ }
1000
+ ```
1001
+
1002
+ ### Example
1003
+
1004
+ ```javascript
1005
+ client.setStatus({
1006
+ status: "dnd",
1007
+
1008
+ activities: [{
1009
+ name: "with the Discord API",
1010
+ type: Status.PLAYING
1011
+ }]
1012
+ });
1013
+ ```
1014
+
1015
+ ### Custom status
1016
+
1017
+ ```javascript
1018
+ client.setStatus({
1019
+ status: "online",
1020
+
1021
+ activities: [{
1022
+ name: "my custom status",
1023
+ type: Status.CUSTOM_STATUS
1024
+ }]
1025
+ });
1026
+ ```
1027
+
1028
+ ### Streaming
1029
+
1030
+ Streaming activities automatically receive the activity's name as `details`.
1031
+
1032
+ ```javascript
1033
+ client.setStatus({
1034
+ status: "online",
1035
+
1036
+ activities: [{
1037
+ name: "Minecraft",
1038
+ type: Status.STREAMING
1039
+ }]
1040
+ });
1041
+ ```
1042
+
1043
+ # Authentication
1044
+
1045
+ ## `client.logout()`
1046
+
1047
+ Logs out the current account and closes the Gateway connection.
1048
+
1049
+ ### Returns
1050
+
1051
+ ```javascript
1052
+ Promise<Object>
1053
+ ```
1054
+
1055
+ ### Example
1056
+
1057
+ ```javascript
1058
+ await client.logout();
1059
+ ```
1060
+
1061
+ # Connection
1062
+
1063
+ ## `client.disconnect(code)`
1064
+
1065
+ Closes the current Gateway session.
1066
+
1067
+ After disconnecting, the client will no longer receive Gateway events. Call `login()` again to create a new session.
1068
+
1069
+ ### Parameters
1070
+
1071
+ | Name | Type | Default | Description |
1072
+ | ------ | -------- | ------- | --------------------- |
1073
+ | `code` | `Number` | `1000` | WebSocket close code. |
1074
+
1075
+ ### Example
1076
+
1077
+ ```javascript
1078
+ client.disconnect();
1079
+ ```
1080
+
1081
+ Or with a specific close code:
1082
+
1083
+ ```javascript
1084
+ client.disconnect(1000);
1085
+ ```
1086
+
1087
+ # Complete Example
1088
+
1089
+ ```javascript
1090
+ const self = require("@imaentity/selfjs");
1091
+
1092
+ const client = new self.Client({
1093
+ debugLogs: true
1094
+ });
1095
+
1096
+ client.on("READY", data => {
1097
+ console.log(`Logged in as ${data.user.username}`);
1098
+ });
1099
+
1100
+ client.on("MESSAGE_CREATE", async message => {
1101
+ console.log(`${message.author.username}: ${message.content}`);
1102
+
1103
+ if(message.content === "!hello") {
1104
+ await client.sendMessage({
1105
+ channel_id: message.channel_id,
1106
+ content: "Hello!"
1107
+ });
1108
+ }
1109
+ });
1110
+
1111
+ client.on("DISCONNECT", () => {
1112
+ console.log("Disconnected");
1113
+ });
1114
+
1115
+ client.login(process.env.DISCORD_TOKEN);
1116
+ ```
1117
+
1118
+ # Exported API
1119
+
1120
+ SelfJS currently exports:
1121
+
1122
+ ```javascript
1123
+ module.exports = {
1124
+ Status,
1125
+ validateToken,
1126
+ createToken,
1127
+ UTCToSnowflake,
1128
+ snowflakeToUTC,
1129
+ Client
1130
+ };
1131
+ ```
1132
+
1133
+ So the following are available:
1134
+
1135
+ ```javascript
1136
+ self.Status
1137
+ self.validateToken
1138
+ self.createToken
1139
+ self.UTCToSnowflake
1140
+ self.snowflakeToUTC
1141
+ self.Client
1142
+ ```