@imaentity/selfjs 3.3.0-b → 4.0.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 (5) hide show
  1. package/README.md +1 -2
  2. package/docs.md +841 -473
  3. package/package.json +1 -2
  4. package/self.js +723 -802
  5. package/changelog.log +0 -37
package/docs.md CHANGED
@@ -1,480 +1,848 @@
1
- # 📜 Documentation
2
- Welcome To SelfJS Documentation
3
-
4
- > [!WARNING]
5
- > Doc's might not be Up-to-Date or 100% Correct.
6
-
7
- ## Properties
8
- + ### ℹ Activites Status:
9
- The enum that holds the different status types.
10
-
11
- **Types:**
12
- ```
13
- Playing : 0
14
- Streaming : 1
15
- Listening : 2
16
- Watching : 3
17
- Custom : 4
18
- Competing : 5
19
- ```
20
-
21
- + ### 🎮 Status:
22
- The string that holds the different status types.
23
-
24
- **Types:**
25
- ```
26
- "online",
27
- "dnd",
28
- "idle",
29
- "invisible"
30
- ```
31
-
32
- ## Functions
33
- + ### 😴 Sleep:
34
- A promise-based sleep function that pauses execution for the given time in milliseconds.
35
- > Paramaters: Miliseconds (Int)
36
-
37
- <br>Example:
38
- ```javascript
39
- self.sleep(1000);
40
- ```
41
- + ### 🤖 Send Webhook Message:
42
- A function to send a webhook message. The webhook token and ID are provided, along with any input data.
43
- >Paramaters: webhookID (String), webhookToken (String or JSON), inputData (JSON)
44
-
45
- <br>Example:
46
- ```javascript
47
- self.sendWebhookMessage("012345678910111213", "012345678910111213141516.123456.123456789101112131415161718", {
48
- "content": "Sending Content Through Webhook"
49
- });
50
- ```
51
- or
52
- ```javascript
53
- self.sendWebhookMessage("https://discord.com/api/webhooks/01234567891011121314/012345678910111213141516.123456.123456789101112131415161718", {
54
- "content": "Sending Content Through Webhook"
55
- });
56
- ```
57
-
58
- + ### 📄 JSON Encode
59
- Encodes a JSON object for use with the discord API. (Self does this automatically.)
60
- >Paramaters: jsonObject (JSON)
61
-
62
- <br>Example:
63
- ```javascript
64
- self.jsonEncode({content: "Hello, world!"});
65
- ```
66
-
67
- ## Class
68
- + ### Client
69
- This class allows the interaction with the Discord server.
70
-
71
- <br>Example:
72
- ```javascript
73
- const self = require("@imaentity/selfjs");
74
- const client = new self.Client();
75
- ```
76
-
77
- + ### 🧑 UserID: The client's user ID.
78
-
79
- + ### 🎟 Token: The client's token.
80
-
81
- + ### 📶 Latency: The latency of the client's websocket connection.
82
-
83
- + ### 🔑 Login (REQUIRED)
84
- Logs in the client to Discord.
85
- >Paramaters: Token (String), isMobile (Boolean | false), logMsgs (Boolean | false)
86
-
87
- <br>Example:
88
- ```javascript
89
- client.login("012345678910111213141516.123456.123456789101112131415161718");
90
- ```
91
-
92
- + ### 💬 onMessage
93
- Function to be executed when a message is received.
94
- >Paramaters: msgFunc (Function)
95
-
96
- <br>Example:
97
- ```javascript
98
- client.onMessage(async function(msg) {
99
- console.log("Message Received: " + msg.content);
100
- });
101
- ```
1
+ # SelfJS Documentation
2
+ Welcome To SelfJS Documentation
102
3
 
103
- + ### ✏ onMessageEdit
104
- Function to be executed when a message is edited.
105
- >Paramaters: editFunc (Function)
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
106
7
 
107
- <br>Example:
108
- ```javascript
109
- client.onMessageEdit(async function(msg) {
110
- console.log("Message Edited: " + msg.content);
111
- });
112
- ```
8
+ ## Installation
113
9
 
114
- + ### 🚮 onMessageDelete
115
- Function to be executed when a message is deleted.
116
- Deleted message objects only receive the channel and message ids.
117
- >Paramaters: deleteFunc (Function)
10
+ ```bash
11
+ npm install @imaentity/selfjs
12
+ ```
118
13
 
119
- <br>Example:
120
- ```javascript
121
- client.onMessageDelete(async function(msg) {
122
- console.log("Message Deleted: " + msg.id);
123
- });
124
- ```
14
+ ## Import
125
15
 
126
- + ### 🕶 onStatusUpdate
127
- Function to be executed when a status update occurs.
128
- >Paramaters: statusFunc (Function)
16
+ ```javascript
17
+ const self = require("@imaentity/selfjs");
18
+ ```
129
19
 
130
- <br>Example:
131
- ```javascript
132
- client.onStatusUpdate(async function(status) {
133
- console.log(status);
134
- });
135
- ```
136
-
137
- + ### 🔏 getDMChannel
138
- Gets the Direct Message (DM) channel for the specified user.
139
- >Paramaters: userID (String)
140
-
141
- <br>Example:
142
- ```javascript
143
- client.getDMChannel("01234567891011121314");
144
- ```
145
-
146
- + ### 🧪 getRoles
147
- Gets the roles for a user in a server.
148
- >Paramaters: serverID (String), userID (String)
149
-
150
- <br>Example:
151
- ```javascript
152
- client.getRoles("01234567891011121314", "41312111019876543210");
153
- ```
154
-
155
- + ### 📁 uploadFile
156
- Uploads a file to the specified channel.
157
- >Paramaters: channelID (String), fileName (String), isSpoiled (Boolean | false), msgContent (String | ""), messageID (String | null)
158
-
159
- <br>Example:
160
- ```javascript
161
- client.uploadFile("01234567891011121314", "image.png", true, "Message Related to The Image", "41312111019876543210");
162
- ```
163
-
164
- + ### 🔍 search
165
- Search for messages in a channel based on the specified options.
166
- >Paramaters: channelID (String), options (JSON)
167
-
168
- <br>Example:
169
- ```javascript
170
- client.search("01234567891011121314", {
171
- content: "Hello, world!"
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"
172
133
  });
173
- ```
174
-
175
- + ### 👤 getUserProfile
176
- Gets the profile of a user.
177
- >Paramaters: userID (String), mutualGuilds (Boolean | false)
178
-
179
- <br>Example:
180
- ```javascript
181
- client.getUserProfile("01234567891011121314", true);
182
- ```
183
-
184
- + ### 👤 getUserData
185
- Gets the data of a user.
186
- >Paramaters: userID (String)
187
-
188
- <br>Example:
189
- ```javascript
190
- client.getUserData("01234567891011121314");
191
- ```
192
-
193
- + ### 🔨 setRolesForMember
194
- Sets roles for a member in a server.
195
- >Paramaters: serverID (String), userID (String), roleIDs (Array)
196
-
197
- <br>Example:
198
- ```javascript
199
- client.setRolesForMember("01234567891011121314", "41312111019876543210", [
200
- "1285712985712985712",
201
- "1284578912561872568",
202
- "9127498127459812749"
203
- ]);
204
- ```
205
-
206
- + ### 🗣 sendMessage
207
- Sends a message to a specified channel.
208
- >Paramaters: channelID (String), message (String)
209
-
210
- <br>Example:
211
- ```javascript
212
- client.sendMessage("01234567891011121314", "Message");
213
- ```
214
-
215
- + ### 👨🏻‍🤝‍👨🏻 replyToMessage
216
- Replies to a specified message.
217
- >Paramaters: channelID (String), messageID (String), message (String)
218
-
219
- <br>Example:
220
- ```javascript
221
- client.replyToMessage("01234567891011121314", "41312111019876543210", "Replied Message");
222
- ```
223
-
224
- + ### 📔 createChannel
225
- Creates a channel in a server based on the specified options.
226
- >Paramaters: guildID (String), name (String), type (Int), parentID (String | null)
227
-
228
- <br>Example:
229
- ```javascript
230
- // 0 = GUILD_TEXT
231
-
232
- client.createChannel("01234567891011121314", "new-channel", 0, "41312111019876543210");
233
- ```
234
-
235
- + ### 👑 setChannelPermissons
236
- Sets a users permissions in a channel based on the specified options.
237
- >Paramaters: channelID (String), userID (String), allow (String | 0), deny (String | 0)
238
-
239
- <br>Example:
240
- ```javascript
241
- client.setChannelPermissons("01234567891011121314", "41312111019876543210");
242
- ```
243
-
244
- + ### 📖 getMessages
245
- Gets messages from a channel with a limit.
246
- >Paramaters: channelID (String), limit (Int)
247
-
248
- <br>Example:
249
- ```javascript
250
- client.getMessages("01234567891011121314", 10);
251
- ```
252
-
253
- + ### 🏙 getUsers
254
- Gets users from a guild.
255
- >Paramaters: guildID (String)
256
-
257
- <br>Example:
258
- ```javascript
259
- client.getUsers("01234567891011121314");
260
- ```
261
-
262
- + ### ❌ removeFromChannel
263
- Removes a user from a channel.
264
- >Paramaters: channelID (String), userID (String)
265
-
266
- <br>Example:
267
- ```javascript
268
- client.removeFromChannel("01234567891011121314", "41312111019876543210");
269
- ```
270
-
271
- + ### 🏃‍♀️ leaveChannel
272
- Leaves a channel.
273
- >Paramaters: channelID (String)
274
-
275
- <br>Example:
276
- ```javascript
277
- client.leaveChannel("01234567891011121314");
278
- ```
279
-
280
- + ### 📞 ring
281
- Starts a call with the specified users in a channel.
282
- >Paramaters: channelID (String), userIDs (Array)
283
-
284
- <br>Example:
285
- ```javascript
286
- client.ring("01234567891011121314", [
287
- "12549812509818205",
288
- "19024578912857987"
289
- ]);
290
- ```
291
-
292
- + ### 📴 stopRinging
293
- Stops ringing the specified users in a channel.
294
- >Paramaters: channelID (String), userIDs (Array)
295
-
296
- <br>Example:
297
- ```javascript
298
- client.stopRinging("01234567891011121314", [
299
- "12549812509818205",
300
- "19024578912857987"
301
- ]);
302
- ```
303
-
304
- + ### ➕ addToChannel
305
- Adds a user to a channel.
306
- >Paramaters: channelID (String), userID (String)
307
-
308
- <br>Example:
309
- ```javascript
310
- client.addToChannel("01234567891011121314", "41312111019876543210");
311
- ```
312
-
313
- + ### 📷 getAvatar
314
- Gets the avatar of a user.
315
- >Paramaters: userID (String), size (Int | 256)
316
-
317
- <br>Example:
318
- ```javascript
319
- client.getAvatar("01234567891011121314");
320
- ```
321
-
322
- + ### 🤼 addFriend
323
- Sends a friend request to a user.
324
- >Paramaters: userID (String)
325
-
326
- <br>Example:
327
- ```javascript
328
- client.addFriend("01234567891011121314");
329
- ```
330
-
331
- + ### ➕ createServer
332
- Creates a server with the given options.
333
- >Paramaters: options ({name: String, icon: String | null})
334
-
335
- <br>Example:
336
- ```javascript
337
- client.createServer({name: "My Server", icon: "data:image/png;base64,aGloaSA6Mw=="});
338
- ```
339
-
340
- + ### 🖊 editMessage
341
- Edits a message.
342
- >Paramaters: channelID (String), messageID (String), message (String)
343
-
344
- <br>Example:
345
- ```javascript
346
- client.editMessage("01234567891011121314", "41312111019876543210", "Edited Message");
347
- ```
348
-
349
- + ### 👋 removeFriend
350
- Removes a user from the friend list.
351
- >Paramaters: userID (String)
352
-
353
- <br>Example:
354
- ```javascript
355
- client.removeFriend("01234567891011121314");
356
- ```
357
-
358
- + ### 📛 renameChannel
359
- Renames a channel.
360
- >Paramaters: channelID (String), channelName (String)
361
-
362
- <br>Example:
363
- ```javascript
364
- client.renameChannel("01234567891011121314", "Renamed Channel");
365
- ```
366
-
367
- + ### ❎ block
368
- Blocks a user.
369
- >Paramaters: userID (String)
370
-
371
- <br>Example:
372
- ```javascript
373
- client.block("01234567891011121314");
374
- ```
375
-
376
- + ### ✅ unblock
377
- Unblocks a user.
378
- >Paramaters: userID (String)
379
-
380
- <br>Example:
381
- ```javascript
382
- client.unblock("01234567891011121314");
383
- ```
384
-
385
- + ### 🚮 deleteMessage
386
- Deletes a message.
387
- >Paramaters: channelID (String), messageID (String)
388
-
389
- <br>Example:
390
- ```javascript
391
- client.deleteMessage("01234567891011121314", "41312111019876543210");
392
- ```
393
-
394
- + ### 🗿 setStatus
395
- Sets the status of the client.
396
- >Paramaters: status (String), activites (Array), afk (Boolean | false)
397
-
398
- <br>Example:
399
- ```javascript
400
- client.setStatus("dnd", [{
401
- name: "with the discord API",
402
- type: self.status.PLAYING
403
- }]);
404
- ```
405
-
406
- + ### 🤼 createGroupChat
407
- Creates a new channel with the specified users.
408
- >Paramaters: userIDs (Array)
409
-
410
- <br>Example:
411
- ```javascript
412
- client.createGroupChat([
413
- "12847128957125",
414
- "12905901285798",
415
- "64837698943868"
416
- ]);
417
- ```
418
-
419
- + ### 🎹 startTyping
420
- Simulates the client typing in a channel.
421
- >Paramaters: channelID (String)
422
-
423
- <br>Example:
424
- ```javascript
425
- client.startTyping("01234567891011121314");
426
- ```
427
-
428
- + ### 📌 pinMessage
429
- Pins a message in a channel.
430
- >Paramaters: channelID (String), messageID (String)
431
-
432
- <br>Example:
433
- ```javascript
434
- client.pinMessage("01234567891011121314", "41312111019876543210");
435
- ```
436
-
437
- + ### ❌ unpinMessage
438
- Unpins a message in a channel.
439
- >Paramaters: channelID (String), messageID (String)
440
-
441
- <br>Example:
442
- ```javascript
443
- client.unpinMessage("01234567891011121314", "41312111019876543210");
444
- ```
445
-
446
- + ### 📝 editNote
447
- Edits a note for a user.
448
- >Paramaters: userID (String), note (String)
449
-
450
- <br>Example:
451
- ```javascript
452
- client.editNote("01234567891011121314", "Edited Note");
453
- ```
454
-
455
- + ### 💾 getChannelData
456
- Gets data of a channel.
457
- >Paramaters: channelID (String)
458
-
459
- <br>Example:
460
- ```javascript
461
- client.getChannelData("01234567891011121314");
462
- ```
463
-
464
- + ### 👑 transferOwnership
465
- Transfers ownership of a channel to another user.
466
- >Paramaters: channelID (String), userID (String)
467
-
468
- <br>Example:
469
- ```javascript
470
- client.transferOwnership("01234567891011121314", "41312111019876543210");
471
- ```
472
-
473
- + ### 🔁 refreshURL
474
- Refreshes an expired attachment url.
475
- >Paramaters: url (String)
476
-
477
- <br>Example:
478
- ```javascript
479
- client.refreshURL();
480
- ```
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
+ ```