@imaentity/selfjs 1.3.5

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 +16 -0
  2. package/docs.md +81 -0
  3. package/package.json +21 -0
  4. package/self.js +1041 -0
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # SelfJS
2
+ An example of an echo bot:
3
+ ```JS
4
+ const discord = require("SelfJS");
5
+ const client = new discord.Client();
6
+
7
+ (async function() {
8
+ await client.login("TOKEN");
9
+
10
+ client.onMessage(async function(msg) {
11
+ if(msg.author.bot || msg.author.self) return;
12
+
13
+ await client.sendMessage(msg.channel_id, msg.content);
14
+ });
15
+ }());
16
+ ```
package/docs.md ADDED
@@ -0,0 +1,81 @@
1
+
2
+ # Properties
3
+ - **status**: The enum that holds the different status types.
4
+
5
+ # Functions
6
+ - **sleep(millis)**: A promise-based sleep function that pauses execution for the given time in milliseconds.
7
+
8
+ - **sendWebhookMessage(webhookID, webhookToken, inputData)**: A function to send a webhook message. The webhook token and ID are provided, along with any input data.
9
+
10
+ # Class
11
+ - **Client**: This class allows the interaction with the Discord server. The following are its methods:
12
+
13
+ - **login(token, isMobile, logMsgs)**: Logs in the client to Discord.
14
+
15
+ - **onMessage(msgFunc)**: Function to be executed when a message is received.
16
+
17
+ - **onMessageEdit(editFunc)**: Function to be executed when a message is edited.
18
+
19
+ - **onStatusUpdate(statusFunc)**: Function to be executed when a status update occurs.
20
+
21
+ - **getDMChannel(userID)**: Gets the Direct Message (DM) channel for the specified user.
22
+
23
+ - **getRoles(serverID, userID)**: Gets the roles for a user in a server.
24
+
25
+ - **uploadFile(channelID, fileName, msgContent = "", messageID = null)**: Uploads a file to the specified channel.
26
+
27
+ - **search(channelID, options)**: Search for messages in a channel based on the specified options.
28
+
29
+ - **getUserData(userID)**: Gets the data of a user.
30
+
31
+ - **setRolesForMember(serverID, userID, roleIDs)**: Sets roles for a member in a server.
32
+
33
+ - **sendMessage(channelID, message)**: Sends a message to a specified channel.
34
+
35
+ - **replyToMessage(channelID, messageID, message)**: Replies to a specified message.
36
+
37
+ - **getMessages(channelID, limit)**: Gets messages from a channel with a limit.
38
+
39
+ - **getUsers(guildID)**: Gets users from a guild.
40
+
41
+ - **removeFromChannel(channelID, userID)**: Removes a user from a channel.
42
+
43
+ - **leaveChannel(channelID)**: Leaves a channel.
44
+
45
+ - **ring(channelID, userIDs)**: Starts a call with the specified users in a channel.
46
+
47
+ - **stopRinging(channelID, userIDs)**: Stops ringing the specified users in a channel.
48
+
49
+ - **addToChannel(channelID, userID)**: Adds a user to a channel.
50
+
51
+ - **getAvatar(userID, size = 256)**: Gets the avatar of a user.
52
+
53
+ - **addFriend(userID)**: Sends a friend request to a user.
54
+
55
+ - **editMessage(channelID, messageID, newMessage)**: Edits a message.
56
+
57
+ - **removeFriend(userID)**: Removes a user from the friend list.
58
+
59
+ - **renameChannel(channelID, channelName)**: Renames a channel.
60
+
61
+ - **block(userID)**: Blocks a user.
62
+
63
+ - **unblock(userID)**: Unblocks a user.
64
+
65
+ - **deleteMessage(channelID, messageID)**: Deletes a message.
66
+
67
+ - **setStatus(status, activity, type)**: Sets the status of the client.
68
+
69
+ - **createChannel(userIDs)**: Creates a new channel with the specified users.
70
+
71
+ - **startTyping(channelID)**: Simulates the client typing in a channel.
72
+
73
+ - **pinMessage(channelID, messageID)**: Pins a message in a channel.
74
+
75
+ - **unpinMessage(channelID, messageID)**: Unpins a message in a channel.
76
+
77
+ - **editNote(userID, note)**: Edits a note for a user.
78
+
79
+ - **getChannelData(channelID)**: Gets data of a channel.
80
+
81
+ - **transferOwnership(channelID, userID)**: Transfers ownership of a channel to another user.
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@imaentity/selfjs",
3
+ "version": "1.3.5",
4
+ "description": "Breaking Discord's TOS to bot user accounts.",
5
+ "main": "self.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/ImaEntity/SelfJS.git"
9
+ },
10
+ "keywords": [
11
+ "bot",
12
+ "discord",
13
+ "library"
14
+ ],
15
+ "author": "ImaEntity",
16
+ "license": "ISC",
17
+ "bugs": {
18
+ "url": "https://github.com/ImaEntity/SelfJS/issues"
19
+ },
20
+ "homepage": "https://github.com/ImaEntity/SelfJS#readme"
21
+ }
package/self.js ADDED
@@ -0,0 +1,1041 @@
1
+ /**
2
+ * @name SelfJS
3
+ * @description Breaking Discord's TOS to bot user accounts.
4
+ * @author Эмберс
5
+ * @version 1.3.5
6
+ */
7
+
8
+ const https = require("https");
9
+ const ws = require("ws");
10
+ const fs = require("fs");
11
+ const crypto = require("crypto");
12
+ const dgram = require("dgram");
13
+
14
+ module.exports = {
15
+ CDNBaseOpt: {
16
+ host: "cdn.discordapp.com",
17
+ port: 443
18
+ },
19
+
20
+ APIBaseOpt: {
21
+ host: "discord.com",
22
+ port: 443
23
+ },
24
+
25
+ status: {
26
+ DEFAULT: -1,
27
+ PLAYING: 0,
28
+ LISTENING: 2,
29
+ WATCHING: 3,
30
+ COMPETING: 5
31
+ },
32
+
33
+ sleep: function(millis) {
34
+ return new Promise(function(res) {
35
+ setTimeout(res, millis);
36
+ });
37
+ },
38
+
39
+ sendWebhookMessage: function(webhookID, webhookToken, inputData) {
40
+ let options = null;
41
+ let msgData = null;
42
+
43
+ if(inputData) {
44
+ msgData = JSON.stringify(inputData);
45
+ options = {
46
+ ...module.exports.APIBaseOpt,
47
+ method: "POST",
48
+ path: `/api/webhooks/${webhookID}/${webhookToken}`,
49
+ headers: {
50
+ "Content-Type": "application/json",
51
+ "Content-Length": msgData.length
52
+ }
53
+ };
54
+ } else {
55
+ msgData = JSON.stringify(webhookToken);
56
+ options = {
57
+ ...module.exports.APIBaseOpt,
58
+ method: "POST",
59
+ path: webhookID.split("//")[1].split('/').slice(1).join('/'),
60
+ headers: {
61
+ "Content-Type": "application/json",
62
+ "Content-Length": msgData.length
63
+ }
64
+ };
65
+ }
66
+
67
+ return new Promise(function(resolve) {
68
+ const req = https.request(options, function(res) {
69
+ res.on("end", resolve);
70
+ });
71
+
72
+ req.write(msgData);
73
+ req.end();
74
+ });
75
+ },
76
+
77
+ Client: class {
78
+ constructor() {
79
+ this.userID = null;
80
+ this.token = null;
81
+ this.socket = null;
82
+ this.sessionID = null;
83
+ this.onMessageFunction = null;
84
+ this.onEditFunction = null;
85
+ this.sequenceID = null;
86
+ this.resumeURL = null;
87
+ this.statusUpdateFunction = null;
88
+ }
89
+
90
+ login(token, isMobile, logMsgs) {
91
+ return new Promise(function(resolve) {
92
+ this.token = token;
93
+ this.socket = new ws("wss://gateway.discord.gg?v=10&encoding=json");
94
+ this.userID = null;
95
+ this.sessionID = null;
96
+
97
+ this.socket.on("open", function() {
98
+ if(logMsgs) console.log("[MainControlSocket] Sending hello");
99
+
100
+ this.socket.send(JSON.stringify({
101
+ op: 2,
102
+ d: {
103
+ token: this.token,
104
+ properties: {
105
+ $os: isMobile ? "linux" : "windows",
106
+ $browser: isMobile ? "Discord iOS" : "disco",
107
+ $device: isMobile ? "discord.gblk" : "disco"
108
+ }
109
+ }
110
+ }));
111
+ }.bind(this));
112
+
113
+ const socketMessageFunction = async function(payload) {
114
+ payload = JSON.parse(payload.toString());
115
+
116
+ if(payload.op == 10) {
117
+ if(logMsgs) console.log("[MainControlSocket] Hello ACK received");
118
+
119
+ if(logMsgs) console.log("[MainControlSocket] Sending heartbeat");
120
+ this.socket.send(JSON.stringify({op: 1, d: this.sequenceID}));
121
+
122
+ setInterval(function() {
123
+ if(logMsgs) console.log("[MainControlSocket] Sending heartbeat");
124
+ this.socket.send(JSON.stringify({op: 1, d: this.sequenceID}));
125
+ }.bind(this), payload.d.heartbeat_interval);
126
+ } else if(payload.op == 11) {
127
+ if(logMsgs) console.log("[MainControlSocket] Heartbeat ACK received");
128
+ } else if(payload.op == 0) {
129
+ this.sequenceID = payload.s;
130
+
131
+ if(payload.t == "READY") {
132
+ this.userID = payload.d.user.id;
133
+ this.sessionID = payload.d.session_id;
134
+ this.resumeURL = payload.d.resume_gateway_url;
135
+
136
+ if(logMsgs) console.log("[MainControlSocket] Ready message received");
137
+ resolve();
138
+ } else if(payload.t == "MESSAGE_CREATE") {
139
+ const ackData = JSON.stringify({token: null});
140
+ const options = {
141
+ ...module.exports.APIBaseOpt,
142
+ method: "POST",
143
+ path: `/api/v10/channels/${payload.d.channel_id}/messages/${payload.d.id}/ack`,
144
+ headers: {
145
+ "Authorization": this.token,
146
+ "Content-Type": "application/json",
147
+ "Content-Length": ackData.length
148
+ }
149
+ };
150
+
151
+ const req = https.request(options);
152
+
153
+ req.write(ackData);
154
+ req.end();
155
+
156
+ try {payload.d.author.self = payload.d.author.id == this.userID;} catch(e) {}
157
+
158
+ if(typeof this.onMessageFunction == "function") this.onMessageFunction(payload.d);
159
+ } else if(payload.t == "MESSAGE_UPDATE") {
160
+ try {payload.d.author.self = payload.d.author.id == this.userID;} catch(e) {}
161
+
162
+ if(typeof this.onEditFunction == "function") this.onEditFunction(payload.d);
163
+ } else if(payload.t == "PRESENCE_UPDATE") {
164
+ try {payload.d.author.self = payload.d.author.id == this.userID;} catch(e) {}
165
+
166
+ if(typeof this.statusUpdateFunction == "function") this.statusUpdateFunction(payload.d);
167
+ }
168
+ } else if(payload.op == 7) {
169
+ if(logMsgs) console.log("[MainControlSocket] Reconnect message received");
170
+
171
+ this.socket = new ws("wss://gateway.discord.gg?v=10&encoding=json");
172
+
173
+ this.socket.on("open", function() {
174
+ if(logMsgs) console.log("[MainControlSocket] Resume message sent");
175
+
176
+ this.socket.send(JSON.stringify({
177
+ op: 6,
178
+ d: {
179
+ token: this.token,
180
+ session_id: this.sessionID,
181
+ seq: this.sequenceID
182
+ }
183
+ }));
184
+ }.bind(this));
185
+
186
+ this.socket.on("message", socketMessageFunction);
187
+ }
188
+ }.bind(this);
189
+
190
+ this.socket.on("message", socketMessageFunction);
191
+ }.bind(this));
192
+ }
193
+
194
+ onMessage(msgFunc) {
195
+ this.onMessageFunction = msgFunc;
196
+ }
197
+
198
+ onMessageEdit(editFunc) {
199
+ this.onEditFunction = editFunc;
200
+ }
201
+
202
+ onStatusUpdate(statusFunc) {
203
+ this.statusUpdateFunction = statusFunc;
204
+ }
205
+
206
+ getDMChannel(userID) {
207
+ const channelData = JSON.stringify({recipients: [userID]});
208
+ const options = {
209
+ ...module.exports.APIBaseOpt,
210
+ method: "POST",
211
+ path: "/api/v10/users/@me/channels",
212
+ headers: {
213
+ "Authorization": this.token,
214
+ "Content-Type": "application/json",
215
+ "Content-Length": channelData.length
216
+ }
217
+ };
218
+
219
+ return new Promise(function(resolve) {
220
+ const req = https.request(options, function(res) {
221
+ const chunks = [];
222
+
223
+ res.on("data", function(chunk) {
224
+ chunks.push(chunk);
225
+ }).on("end", function() {
226
+ resolve(JSON.parse(Buffer.concat(chunks)));
227
+ });
228
+ });
229
+
230
+ req.write(channelData);
231
+ req.end();
232
+ });
233
+ }
234
+
235
+ getRoles(serverID, userID) {
236
+ const options = {
237
+ ...module.exports.APIBaseOpt,
238
+ method: "GET",
239
+ path: `/api/v10/users/${userID}/profile?with_mutual_guilds=false&guild_id=${serverID}`,
240
+ headers: {
241
+ "Authorization": this.token
242
+ }
243
+ };
244
+
245
+ return new Promise(function(resolve) {
246
+ const req = https.request(options, function(res) {
247
+ const chunks = [];
248
+
249
+ res.on("data", function(chunk) {
250
+ chunks.push(chunk);
251
+ }).on("end", function() {
252
+ resolve(JSON.parse(Buffer.concat(chunks)).guild_member.roles);
253
+ });
254
+ });
255
+
256
+ req.end();
257
+ });
258
+ }
259
+
260
+ uploadFile(channelID, fileName, msgContent = "", messageID = null) {
261
+ const fileData = fs.readFileSync(fileName);
262
+
263
+ if(fileData.length >= 26214400) return null;
264
+
265
+ fileName = fileName.split('/').pop();
266
+
267
+ const requestData = JSON.stringify({
268
+ files: [
269
+ {
270
+ filename: fileName,
271
+ file_size: fileData.length,
272
+ id: '1'
273
+ }
274
+ ]
275
+ });
276
+ const requestOptions = {
277
+ ...module.exports.APIBaseOpt,
278
+ method: "POST",
279
+ path: `/api/v10/channels/${channelID}/attachments`,
280
+ headers: {
281
+ "Content-Type": "application/json",
282
+ "Authorization": this.token,
283
+ "Content-Length": requestData.length
284
+ }
285
+ };
286
+
287
+ return new Promise(function(resolve) {
288
+ const requestReq = https.request(requestOptions, function(requestRes) {
289
+ const requestChunks = [];
290
+ const fileTable = {
291
+ png: "image/png",
292
+ jpg: "image/jpeg",
293
+ jpeg: "image/jpeg",
294
+ js: "application/javascript",
295
+ json: "application/json",
296
+ html: "text/html",
297
+ mp4: "video/mp4",
298
+ mp3: "audio/mpeg",
299
+ css: "text/css",
300
+ c: "text/x-c",
301
+ cpp: "text/x-c",
302
+ h: "text/x-c",
303
+ hpp: "text/x-c",
304
+ bat: "application/x-msdownload",
305
+ exe: "application/x-msdownload",
306
+ asm: "text/x-asm",
307
+ webp: "image/webp",
308
+ webm: "video/webm",
309
+ mov: "video/quicktime",
310
+ mkv: "video/x-matroska",
311
+ java: "text/x-java-source",
312
+ class: "application/java-vm",
313
+ jar: "application/java-archive",
314
+ wav: "audio/x-wav",
315
+ zip: "application/zip",
316
+ rar: "application/x-rar-compressed",
317
+ txt: "text/plain",
318
+ log: "text/plain",
319
+ lua: "text/x-lua",
320
+ xml: "application/xml"
321
+ };
322
+
323
+ requestRes.on("data", function(chunk) {
324
+ requestChunks.push(chunk);
325
+ }).on("end", function() {
326
+ const requestResData = JSON.parse(Buffer.concat(requestChunks));
327
+ const fileOptions = {
328
+ host: "discord-attachments-uploads-prd.storage.googleapis.com",
329
+ port: 443,
330
+ method: "PUT",
331
+ path: `/${requestResData.attachments[0].upload_url.split('/').slice(3).join('/')}`,
332
+ headers: {
333
+ "Content-Type": fileTable[fileName.split('.').slice(-1)[0]] ?? false,
334
+ "Content-Length": fileData.length
335
+ }
336
+ };
337
+
338
+ const fileRequest = https.request(fileOptions, function(fileRes) {
339
+ const fileChunks = [];
340
+
341
+ fileRes.on("data", function(chunk) {
342
+ fileChunks.push(chunk);
343
+ }).on("end", function() {
344
+ let finalData = JSON.stringify({
345
+ content: msgContent,
346
+ attachments: [
347
+ {
348
+ id: '0',
349
+ filename: fileName,
350
+ uploaded_filename: requestResData.attachments[0].upload_filename
351
+ }
352
+ ]
353
+ });
354
+
355
+ if(messageID) {
356
+ finalData = JSON.parse(finalData);
357
+ finalData.message_reference = {
358
+ channel_id: channelID,
359
+ message_id: messageID
360
+ };
361
+
362
+ finalData = JSON.stringify(finalData);
363
+ }
364
+
365
+ const finalOptions = {
366
+ ...module.exports.APIBaseOpt,
367
+ method: "POST",
368
+ path: `/api/v10/channels/${channelID}/messages`,
369
+ headers: {
370
+ "Content-Type": "application/json",
371
+ "Authorization": this.token,
372
+ "Content-Length": finalData.length
373
+ }
374
+ };
375
+
376
+ const finalReq = https.request(finalOptions, function(finalRes) {
377
+ const finalChunks = [];
378
+
379
+ finalRes.on("data", function(chunk) {
380
+ finalChunks.push(chunk);
381
+ }).on("end", function() {
382
+ resolve(JSON.parse(Buffer.concat(finalChunks)));
383
+ });
384
+ });
385
+
386
+ finalReq.write(finalData);
387
+ finalReq.end();
388
+ }.bind(this));
389
+ }.bind(this));
390
+
391
+ fileRequest.write(fileData);
392
+ fileRequest.end();
393
+ }.bind(this));
394
+ }.bind(this));
395
+
396
+ requestReq.write(requestData);
397
+ requestReq.end();
398
+ }.bind(this));
399
+ }
400
+
401
+ search(channelID, options) {
402
+ options = options || {};
403
+
404
+ const pinned = options.pinned ?? null;
405
+ const author = options.author ?? null;
406
+ const mentions = options.mentions ?? null;
407
+ const has = options.has ?? null;
408
+ const minDate = options.minDate ?? null;
409
+ const maxDate = options.maxData ?? null;
410
+ const content = options.content ?? null;
411
+ const offset = options.offset ?? null;
412
+
413
+ let path = `/api/v10/channels/${channelID}/messages/search?`;
414
+
415
+ if(pinned != null) path += `pinned=${pinned}&`;
416
+ if(minDate != null) path += `min_id=${minDate}&`;
417
+ if(maxDate != null) path += `max_id=${maxDate}&`;
418
+ if(offset != null) path += `offset=${offset}&`;
419
+ if(content != null) path += `content=${encodeURI(content)}&`;
420
+ if(author != null) path += author.map((e) => `author_id=${e}`).join('&') + '&';
421
+ if(mentions != null) path += mentions.map((e) => `mentions=${e}`).join('&') + '&';
422
+ if(has != null) path += has.map((e) => `has=${e}`).join('&');
423
+
424
+ const requestOptions = {
425
+ ...module.exports.APIBaseOpt,
426
+ method: "GET",
427
+ path,
428
+ headers: {
429
+ "Authorization": this.token
430
+ }
431
+ };
432
+
433
+ return new Promise(function(resolve) {
434
+ const req = https.request(requestOptions, function(res) {
435
+ const chunks = [];
436
+
437
+ res.on("data", function(chunk) {
438
+ chunks.push(chunk);
439
+ }).on("end", function() {
440
+ resolve(JSON.parse(Buffer.concat(chunks)));
441
+ });
442
+ });
443
+
444
+ req.end();
445
+ });
446
+ }
447
+
448
+ getUserData(userID) {
449
+ const options = {
450
+ ...module.exports.APIBaseOpt,
451
+ method: "GET",
452
+ path: `/api/v10/users/${userID}/profile?with_mutual_guilds=false`,
453
+ headers: {
454
+ "Authorization": this.token
455
+ }
456
+ };
457
+
458
+ return new Promise(function(resolve) {
459
+ const req = https.request(options, function(res) {
460
+ const chunks = [];
461
+
462
+ res.on("data", function(chunk) {
463
+ chunks.push(chunk);
464
+ }).on("end", function() {
465
+ resolve(JSON.parse(Buffer.concat(chunks)));
466
+ });
467
+ });
468
+
469
+ req.end();
470
+ });
471
+ }
472
+
473
+ setRolesForMemeber(serverID, userID, roleIDs) {
474
+ const roleData = JSON.stringify(roleIDs);
475
+ const options = {
476
+ ...module.exports.APIBaseOpt,
477
+ method: "PATCH",
478
+ path: `/api/v10/guilds/${serverID}/members/${userID}`,
479
+ headers: {
480
+ "Content-Type": "application/json",
481
+ "Content-Length": roleData.length,
482
+ "Authorization": this.token
483
+ }
484
+ };
485
+
486
+ return new Promise(function(resolve) {
487
+ const req = https.request(options, function(res) {
488
+ res.on("end", resolve);
489
+ });
490
+
491
+ req.write(roleData);
492
+ req.end();
493
+ });
494
+ }
495
+
496
+ sendMessage(channelID, message) {
497
+ const msgData = JSON.stringify({content: message});
498
+ const options = {
499
+ ...module.exports.APIBaseOpt,
500
+ method: "POST",
501
+ path: `/api/v10/channels/${channelID}/messages`,
502
+ headers: {
503
+ "Authorization": this.token,
504
+ "Content-Type": "application/json",
505
+ "Content-Length": msgData.length
506
+ }
507
+ };
508
+
509
+ return new Promise(function(resolve) {
510
+ const req = https.request(options, function(res) {
511
+ const chunks = [];
512
+
513
+ res.on("data", function(chunk) {
514
+ chunks.push(chunk);
515
+ }).on("end", function() {
516
+ resolve(JSON.parse(Buffer.concat(chunks)));
517
+ });
518
+ });
519
+
520
+ req.write(msgData);
521
+ req.end();
522
+ });
523
+ }
524
+
525
+ replyToMessage(channelID, messageID, message) {
526
+ const msgData = JSON.stringify({content: message, message_reference: {channel_id: channelID, message_id: messageID}});
527
+ const options = {
528
+ ...module.exports.APIBaseOpt,
529
+ method: "POST",
530
+ path: `/api/v10/channels/${channelID}/messages`,
531
+ headers: {
532
+ "Authorization": this.token,
533
+ "Content-Type": "application/json",
534
+ "Content-Length": msgData.length
535
+ }
536
+ };
537
+
538
+ return new Promise(function(resolve) {
539
+ const req = https.request(options, function(res) {
540
+ const chunks = [];
541
+
542
+ res.on("data", function(chunk) {
543
+ chunks.push(chunk);
544
+ }).on("end", function() {
545
+ resolve(JSON.parse(Buffer.concat(chunks)));
546
+ });
547
+ });
548
+
549
+ req.write(msgData);
550
+ req.end();
551
+ });
552
+ }
553
+
554
+ getMessages(channelID, limit) {
555
+ const options = {
556
+ ...module.exports.APIBaseOpt,
557
+ method: "GET",
558
+ path: `/api/v10/channels/${channelID}/messages?limit=${limit}`,
559
+ headers: {
560
+ "Authorization": this.token
561
+ }
562
+ };
563
+
564
+ return new Promise(function(resolve) {
565
+ const req = https.request(options, function(res) {
566
+ const chunks = [];
567
+
568
+ res.on("data", function(chunk) {
569
+ chunks.push(chunk);
570
+ }).on("end", function() {
571
+ resolve(JSON.parse(Buffer.concat(chunks)));
572
+ });
573
+ });
574
+
575
+ req.end();
576
+ });
577
+ }
578
+
579
+ getUsers(guildID) {
580
+ const options = {
581
+ ...module.exports.APIBaseOpt,
582
+ method: "GET",
583
+ path: `/api/v10/guilds/${guildID}/members`,
584
+ headers: {
585
+ "Authorization": this.token
586
+ }
587
+ };
588
+
589
+ return new Promise(function(resolve) {
590
+ const req = https.request(options, function(res) {
591
+ const chunks = [];
592
+
593
+ res.on("data", function(chunk) {
594
+ chunks.push(chunk);
595
+ }).on("end", function() {
596
+ resolve(JSON.parse(Buffer.concat(chunks)));
597
+ });
598
+ });
599
+
600
+ req.end();
601
+ });
602
+ }
603
+
604
+ removeFromChannel(channelID, userID) {
605
+ const options = {
606
+ ...module.exports.APIBaseOpt,
607
+ method: "DELETE",
608
+ path: `/api/v10/channels/${channelID}/recipients/${userID}`,
609
+ headers: {
610
+ "Authorization": this.token
611
+ }
612
+ };
613
+
614
+ return new Promise(function(resolve) {
615
+ const req = https.request(options, function(res) {
616
+ res.on("end", resolve);
617
+ });
618
+
619
+ req.end();
620
+ });
621
+ }
622
+
623
+ leaveChannel(channelID) {
624
+ return new Promise(function(resolve) {
625
+ this.removeFromChannel(channelID, this.userID).then(resolve);
626
+ }.bind(this));
627
+ }
628
+
629
+ ring(channelID, userIDs) {
630
+ const ringData = JSON.stringify({recipients: userIDs});
631
+ const options = {
632
+ ...module.exports.APIBaseOpt,
633
+ method: "POST",
634
+ path: `/api/v10/channels/${channelID}/call/ring`,
635
+ headers: {
636
+ "Authorization": this.token,
637
+ "Content-Type": "application/json",
638
+ "Content-Length": ringData.length
639
+ }
640
+ };
641
+
642
+ return new Promise(function(resolve) {
643
+ const req = https.request(options, function(res) {
644
+ res.on("end", resolve);
645
+ });
646
+
647
+ req.write(ringData);
648
+ req.end();
649
+ });
650
+ }
651
+
652
+ stopRinging(channelID, userIDs) {
653
+ const ringData = JSON.stringify({recipients: userIDs});
654
+ const options = {
655
+ ...module.exports.APIBaseOpt,
656
+ method: "POST",
657
+ path: `/api/v10/channels/${channelID}/call/stop-ringing`,
658
+ headers: {
659
+ "Authorization": this.token,
660
+ "Content-Type": "application/json",
661
+ "Content-Length": ringData.length
662
+ }
663
+ };
664
+
665
+ return new Promise(function(resolve) {
666
+ const req = https.request(options, function(res) {
667
+ res.on("end", resolve);
668
+ });
669
+
670
+ req.write(ringData);
671
+ req.end();
672
+ });
673
+ }
674
+
675
+ addToChannel(channelID, userID) {
676
+ const options = {
677
+ ...module.exports.APIBaseOpt,
678
+ method: "PUT",
679
+ path: `/api/v10/channels/${channelID}/recipients/${userID}`,
680
+ headers: {
681
+ "Authorization": this.token,
682
+ "Content-Length": 0
683
+ }
684
+ };
685
+
686
+ return new Promise(function(resolve) {
687
+ const req = https.request(options, function(res) {
688
+ res.on("end", resolve);
689
+ });
690
+
691
+ req.write("");
692
+ req.end();
693
+ });
694
+ }
695
+
696
+ async getAvatar(userID, size = 256) {
697
+ let avatarID = null;
698
+
699
+ try {
700
+ avatarID = await this.getUserData(userID);
701
+ avatarID = avatarID.user.avatar;
702
+ } catch(e) {
703
+ return null;
704
+ }
705
+
706
+ const options = {
707
+ ...module.exports.CDNBaseOpt,
708
+ method: "GET",
709
+ path: `/avatars/${userID}/${avatarID}.webp?size=${size}`
710
+ };
711
+
712
+ return new Promise(function(resolve) {
713
+ const req = https.request(options, function(res) {
714
+ const chunks = [];
715
+
716
+ res.on("data", function(chunk) {
717
+ chunks.push(chunk);
718
+ }).on("end", function() {
719
+ resolve(Buffer.concat(chunks));
720
+ });
721
+ });
722
+
723
+ req.end();
724
+ });
725
+ }
726
+
727
+ addFriend(userID) {
728
+ const options = {
729
+ ...module.exports.APIBaseOpt,
730
+ method: "PUT",
731
+ path: `/api/v10/users/@me/relationships/${userID}`,
732
+ headers: {
733
+ "Authorization": this.token,
734
+ "Content-Length": 0
735
+ }
736
+ };
737
+
738
+ return new Promise(function(resolve) {
739
+ const req = https.request(options, function(res) {
740
+ res.on("end", resolve);
741
+ });
742
+
743
+ req.write("");
744
+ req.end();
745
+ });
746
+ }
747
+
748
+ editMessage(channelID, messageID, newMessage) {
749
+ const msgData = JSON.stringify({content: newMessage});
750
+ const options = {
751
+ ...module.exports.APIBaseOpt,
752
+ method: "PATCH",
753
+ path: `/api/v10/channels/${channelID}/messages/${messageID}`,
754
+ headers: {
755
+ "Authorization": this.token,
756
+ "Content-Type": "application/json",
757
+ "Content-Length": msgData.length
758
+ }
759
+ };
760
+
761
+ return new Promise(function(resolve) {
762
+ const req = https.request(options, function(res) {
763
+ res.on("end", resolve);
764
+ });
765
+
766
+ req.write(msgData);
767
+ req.end();
768
+ });
769
+ }
770
+
771
+ removeFriend(userID) {
772
+ const options = {
773
+ ...module.exports.APIBaseOpt,
774
+ method: "DELETE",
775
+ path: `/api/v10/users/@me/relationships/${userID}`,
776
+ headers: {
777
+ "Authorization": this.token
778
+ }
779
+ };
780
+
781
+ return new Promise(function(resolve) {
782
+ const req = https.request(options, function(res) {
783
+ res.on("end", resolve);
784
+ });
785
+
786
+ req.end();
787
+ });
788
+ }
789
+
790
+ renameChannel(channelID, channelName) {
791
+ const nameData = JSON.stringify({name: channelName});
792
+ const options = {
793
+ ...module.exports.APIBaseOpt,
794
+ method: "PATCH",
795
+ path: `/api/v10/channels/${channelID}`,
796
+ headers: {
797
+ "Authorization": this.token,
798
+ "Content-Type": "application/json",
799
+ "Content-Length": nameData.length
800
+ }
801
+ };
802
+
803
+ return new Promise(function(resolve) {
804
+ const req = https.request(options, function(res) {
805
+ res.on("end", resolve);
806
+ });
807
+
808
+ req.write(nameData);
809
+ req.end();
810
+ });
811
+ }
812
+
813
+ block(userID) {
814
+ const blockData = JSON.stringify({type: 2});
815
+ const options = {
816
+ ...module.exports.APIBaseOpt,
817
+ method: "PUT",
818
+ path: `/api/v10/users/@me/relationships/${userID}`,
819
+ headers: {
820
+ "Authorization": this.token,
821
+ "Content-Type": "application/json",
822
+ "Content-Length": blockData.length
823
+ }
824
+ };
825
+
826
+ return new Promise(function(resolve) {
827
+ const req = https.request(options, function(res) {
828
+ res.on("end", resolve);
829
+ });
830
+
831
+ req.write(blockData);
832
+ req.end();
833
+ });
834
+ }
835
+
836
+ unblock(userID) {
837
+ return new Promise(function(resolve) {
838
+ this.removeFriend(userID).then(resolve);
839
+ }.bind(this));
840
+ }
841
+
842
+ deleteMessage(channelID, messageID) {
843
+ const options = {
844
+ ...module.exports.APIBaseOpt,
845
+ method: "DELETE",
846
+ path: `/api/v10/channels/${channelID}/messages/${messageID}`,
847
+ headers: {
848
+ "Authorization": this.token
849
+ }
850
+ };
851
+
852
+ return new Promise(function(resolve) {
853
+ const req = https.request(options, function(res) {
854
+ res.on("end", resolve);
855
+ });
856
+
857
+ req.end();
858
+ });
859
+ }
860
+
861
+ setStatus(status, activity, type) {
862
+ return new Promise(function(resolve) {
863
+ this.socket.send(JSON.stringify({
864
+ op: 3,
865
+ d: {
866
+ since: Date.now(),
867
+ status,
868
+ activities: [{
869
+ name: activity,
870
+ type
871
+ }],
872
+ afk: false
873
+ }
874
+ }));
875
+
876
+ setTimeout(resolve, 1000);
877
+ }.bind(this));
878
+ }
879
+
880
+ createChannel(userIDs) {
881
+ const userData = JSON.stringify({recipients: userIDs});
882
+ const options = {
883
+ ...module.exports.APIBaseOpt,
884
+ method: "POST",
885
+ path: `/api/v10/users/@me/channels`,
886
+ headers: {
887
+ "Authorization": this.token,
888
+ "Content-Type": "application/json",
889
+ "Content-Length": userData.length
890
+ }
891
+ };
892
+
893
+ return new Promise(function(resolve) {
894
+ const req = https.request(options, function(res) {
895
+ const chunks = [];
896
+
897
+ res.on("data", function(chunk) {
898
+ chunks.push(chunk);
899
+ }).on("end", function() {
900
+ resolve(JSON.parse(Buffer.concat(chunks)));
901
+ });
902
+ });
903
+
904
+ req.write(userData);
905
+ req.end();
906
+ });
907
+ }
908
+
909
+ startTyping(channelID) {
910
+ const options = {
911
+ ...module.exports.APIBaseOpt,
912
+ method: "POST",
913
+ path: `/api/v10/channels/${channelID}/typing`,
914
+ headers: {
915
+ "Authorization": this.token,
916
+ "Content-Length": 0
917
+ }
918
+ };
919
+
920
+ return new Promise(function(resolve) {
921
+ const req = https.request(options, function(res) {
922
+ res.on("end", resolve);
923
+ });
924
+
925
+ req.write("");
926
+ req.end();
927
+ });
928
+ }
929
+
930
+ pinMessage(channelID, messageID) {
931
+ const options = {
932
+ ...module.exports.APIBaseOpt,
933
+ method: "PUT",
934
+ path: `/api/v10/channels/${channelID}/pins/${messageID}`,
935
+ headers: {
936
+ "Authorization": this.token,
937
+ "Content-Length": 0
938
+ }
939
+ };
940
+
941
+ return new Promise(function(resolve) {
942
+ const req = https.request(options, function(res) {
943
+ res.on("end", resolve);
944
+ });
945
+
946
+ req.write("");
947
+ req.end();
948
+ });
949
+ }
950
+
951
+ unpinMessage(channelID, messageID) {
952
+ const options = {
953
+ ...module.exports.APIBaseOpt,
954
+ method: "DELETE",
955
+ path: `/api/v10/channels/${channelID}/pins/${messageID}`,
956
+ headers: {
957
+ "Authorization": this.token
958
+ }
959
+ };
960
+
961
+ return new Promise(function(resolve) {
962
+ const req = https.request(options, function(res) {
963
+ res.on("end", resolve);
964
+ });
965
+
966
+ req.end();
967
+ });
968
+ }
969
+
970
+ editNote(userID, note) {
971
+ const noteData = JSON.stringify({note});
972
+ const options = {
973
+ ...module.exports.APIBaseOpt,
974
+ method: "PUT",
975
+ path: `/api/v10/users/@me/notes/${userID}`,
976
+ headers: {
977
+ "Authorization": this.token,
978
+ "Content-Type": "application/json",
979
+ "Content-Length": noteData.length
980
+ }
981
+ };
982
+
983
+ return new Promise(function(resolve) {
984
+ const req = https.request(options, function(res) {
985
+ res.on("end", resolve);
986
+ });
987
+
988
+ req.write(noteData);
989
+ req.end();
990
+ });
991
+ }
992
+
993
+ getChannelData(channelID) {
994
+ const options = {
995
+ ...module.exports.APIBaseOpt,
996
+ method: "GET",
997
+ path: `/api/v10/channels/${channelID}`,
998
+ headers: {
999
+ "Authorization": this.token
1000
+ }
1001
+ };
1002
+
1003
+ return new Promise(function(resolve) {
1004
+ const req = https.request(options, function(res) {
1005
+ let chunks = [];
1006
+
1007
+ res.on("data", function(chunk) {
1008
+ chunks.push(chunk);
1009
+ }).on("end", function() {
1010
+ resolve(JSON.parse(Buffer.concat(chunks)));
1011
+ });
1012
+ });
1013
+
1014
+ req.end();
1015
+ });
1016
+ }
1017
+
1018
+ transferOwnership(channelID, userID) {
1019
+ const ownershipData = JSON.stringify({owner: userID});
1020
+ const options = {
1021
+ ...module.exports.APIBaseOpt,
1022
+ method: "PATCH",
1023
+ path: `/api/v10/channels/${channelID}`,
1024
+ headers: {
1025
+ "Authorization": this.token,
1026
+ "Content-Type": "application/json",
1027
+ "Content-Length": ownershipData.length
1028
+ }
1029
+ };
1030
+
1031
+ return new Promise(function(resolve) {
1032
+ const req = https.request(options, function(res) {
1033
+ res.on("end", resolve);
1034
+ });
1035
+
1036
+ req.write(ownershipData);
1037
+ req.end();
1038
+ });
1039
+ }
1040
+ }
1041
+ };