@imaentity/selfjs 3.2.0 → 3.3.0-b02

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/changelog.log +11 -7
  2. package/docs.md +2 -2
  3. package/package.json +1 -1
  4. package/self.js +109 -89
package/changelog.log CHANGED
@@ -1,13 +1,13 @@
1
- V2.9.11
1
+ v2.9.11
2
2
  Added:
3
3
  changelog.log
4
4
 
5
5
  Changed:
6
6
  Client.setStatus now has default params
7
7
 
8
- V2.10.11
8
+ v2.10.11
9
9
  Added:
10
- Docs link to README.md
10
+ A docs link to README.md
11
11
 
12
12
  Changed:
13
13
  Structure of docs.md
@@ -15,11 +15,11 @@ V2.10.11
15
15
  Removed:
16
16
  The default option in the status enum
17
17
 
18
- V3.0.0
18
+ v3.0.0
19
19
  Changed:
20
20
  Overhaul to the structure of self.js
21
21
 
22
- V3.1.0
22
+ v3.1.0
23
23
  Added:
24
24
  Webhooks can now send files
25
25
  Client.getUserProfile to fetch user profiles
@@ -28,6 +28,10 @@ V3.1.0
28
28
  Changed:
29
29
  Client.getUser now gets the actual user instead of their profile
30
30
 
31
- V3.2.0
31
+ v3.2.0
32
32
  Added:
33
- Client.refreshURL to refresh expired urls
33
+ Client.refreshURL to refresh expired urls
34
+
35
+ v3.3.0
36
+ Added:
37
+ Client websockets now auto-reconnect after a failed heartbeat
package/docs.md CHANGED
@@ -174,11 +174,11 @@ Welcome To SelfJS Documentation
174
174
 
175
175
  + ### 👤 getUserProfile
176
176
  Gets the profile of a user.
177
- >Paramaters: userID (String)
177
+ >Paramaters: userID (String), mutualGuilds (Boolean | false)
178
178
 
179
179
  <br>Example:
180
180
  ```javascript
181
- client.getUserProfile("01234567891011121314");
181
+ client.getUserProfile("01234567891011121314", true);
182
182
  ```
183
183
 
184
184
  + ### 👤 getUserData
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imaentity/selfjs",
3
- "version": "3.2.0",
3
+ "version": "3.3.0-b02",
4
4
  "description": "Breaking Discord's TOS to bot user accounts.",
5
5
  "main": "self.js",
6
6
  "repository": {
package/self.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * @name SelfJS
3
3
  * @description Breaking Discord's TOS to bot user accounts.
4
4
  * @author Эмберс
5
- * @version 3.2.0
5
+ * @version 3.3.0-b02
6
6
  */
7
7
 
8
8
  const https = require("https");
@@ -129,6 +129,111 @@ module.exports = {
129
129
  this.latency = null;
130
130
  this.beforeHB = null;
131
131
  this.hearbeatInterval = null;
132
+ this.receivedLastHB = true;
133
+
134
+ // Session, token, sequence
135
+ }
136
+
137
+ async onSockMsg(payload) {
138
+ payload = JSON.parse(payload.toString());
139
+
140
+ if(payload.op == 10) {
141
+ if(logMsgs) console.log("[MainControlSocket] Hello ACK received");
142
+
143
+ if(logMsgs) console.log("[MainControlSocket] Sending heartbeat");
144
+ this.beforeHB = Date.now();
145
+ this.socket.send(module.exports.jsonEncode({op: 1, d: this.sequenceID}));
146
+
147
+ this.hearbeatInterval = setInterval(function() {
148
+ if(logMsgs) console.log("[MainControlSocket] Sending heartbeat");
149
+ this.beforeHB = Date.now();
150
+ this.socket.send(module.exports.jsonEncode({op: 1, d: this.sequenceID}));
151
+
152
+ if(!this.receivedLastHB) {
153
+ if(logMsgs) console.log("[MainControlSocket] Heartbeat ACK failed, reconnecting");
154
+
155
+ this.receivedLastHB = true;
156
+ this.reconnect(this.resumeURL, this.token, this.sessionID, this.sequenceID);
157
+
158
+ return;
159
+ }
160
+
161
+ this.receivedLastHB = false;
162
+ }.bind(this), payload.d.heartbeat_interval);
163
+ } else if(payload.op == 11) {
164
+ if(logMsgs) console.log("[MainControlSocket] Heartbeat ACK received");
165
+ this.latency = Date.now() - this.beforeHB;
166
+
167
+ this.receivedLastHB = true;
168
+ } else if(payload.op == 0) {
169
+ this.sequenceID = payload.s;
170
+
171
+ if(payload.t == "READY") {
172
+ this.userID = payload.d.user.id;
173
+ this.sessionID = payload.d.session_id;
174
+ this.resumeURL = payload.d.resume_gateway_url;
175
+
176
+ if(logMsgs) console.log("[MainControlSocket] Ready message received");
177
+ resolve();
178
+ } else if(payload.t == "MESSAGE_CREATE") {
179
+ const ackData = module.exports.jsonEncode({token: null});
180
+ const options = {
181
+ ...module.exports.APIBaseOpt,
182
+ method: "POST",
183
+ path: `/api/v10/channels/${payload.d.channel_id}/messages/${payload.d.id}/ack`,
184
+ headers: {
185
+ "Authorization": this.token,
186
+ "Content-Type": "application/json",
187
+ "Content-Length": ackData.length
188
+ }
189
+ };
190
+
191
+ const req = https.request(options);
192
+
193
+ req.write(ackData);
194
+ req.end();
195
+
196
+ try {payload.d.author.self = payload.d.author.id == this.userID;} catch(e) {}
197
+
198
+ if(typeof this.onMessageFunction == "function") this.onMessageFunction(payload.d);
199
+ } else if(payload.t == "MESSAGE_UPDATE") {
200
+ try {payload.d.author.self = payload.d.author.id == this.userID;} catch(e) {}
201
+
202
+ if(typeof this.onEditFunction == "function") this.onEditFunction(payload.d);
203
+ } else if(payload.t == "PRESENCE_UPDATE") {
204
+ try {payload.d.author.self = payload.d.author.id == this.userID;} catch(e) {}
205
+
206
+ if(typeof this.statusUpdateFunction == "function") this.statusUpdateFunction(payload.d);
207
+ } else if(payload.t == "MESSAGE_DELETE") {
208
+ if(typeof this.onDeleteFunction == "function") this.onDeleteFunction(payload.d);
209
+ }
210
+ } else if(payload.op == 7) {
211
+ if(logMsgs) console.log("[MainControlSocket] Reconnect message received");
212
+
213
+ this.receivedLastHB = true;
214
+ this.reconnect(this.resumeURL, this.token, this.sessionID, this.sequenceID);
215
+ }
216
+ }
217
+
218
+ reconnect(resumeURL, token, sessionID, sequence) {
219
+ this.socket.terminate();
220
+ if(this.hearbeatInterval)
221
+ clearInterval(this.hearbeatInterval);
222
+
223
+ this.socket = new ws(resumeURL);
224
+
225
+ this.socket.on("open", function() {
226
+ this.socket.send(module.exports.jsonEncode({
227
+ op: 6,
228
+ d: {
229
+ token: token,
230
+ session_id: sessionID,
231
+ seq: sequence
232
+ }
233
+ }));
234
+ }.bind(this));
235
+
236
+ this.socket.on("message", onSockMsg);
132
237
  }
133
238
 
134
239
  login(token, isMobile = false, logMsgs = false) {
@@ -154,92 +259,7 @@ module.exports = {
154
259
  }));
155
260
  }.bind(this));
156
261
 
157
- const socketMessageFunction = async function(payload) {
158
- payload = JSON.parse(payload.toString());
159
-
160
- if(payload.op == 10) {
161
- if(logMsgs) console.log("[MainControlSocket] Hello ACK received");
162
-
163
- if(logMsgs) console.log("[MainControlSocket] Sending heartbeat");
164
- this.beforeHB = Date.now();
165
- this.socket.send(module.exports.jsonEncode({op: 1, d: this.sequenceID}));
166
-
167
- this.hearbeatInterval = setInterval(function() {
168
- if(logMsgs) console.log("[MainControlSocket] Sending heartbeat");
169
- this.beforeHB = Date.now();
170
- this.socket.send(module.exports.jsonEncode({op: 1, d: this.sequenceID}));
171
- }.bind(this), payload.d.heartbeat_interval);
172
- } else if(payload.op == 11) {
173
- if(logMsgs) console.log("[MainControlSocket] Heartbeat ACK received");
174
- this.latency = Date.now() - this.beforeHB;
175
- } else if(payload.op == 0) {
176
- this.sequenceID = payload.s;
177
-
178
- if(payload.t == "READY") {
179
- this.userID = payload.d.user.id;
180
- this.sessionID = payload.d.session_id;
181
- this.resumeURL = payload.d.resume_gateway_url;
182
-
183
- if(logMsgs) console.log("[MainControlSocket] Ready message received");
184
- resolve();
185
- } else if(payload.t == "MESSAGE_CREATE") {
186
- const ackData = module.exports.jsonEncode({token: null});
187
- const options = {
188
- ...module.exports.APIBaseOpt,
189
- method: "POST",
190
- path: `/api/v10/channels/${payload.d.channel_id}/messages/${payload.d.id}/ack`,
191
- headers: {
192
- "Authorization": this.token,
193
- "Content-Type": "application/json",
194
- "Content-Length": ackData.length
195
- }
196
- };
197
-
198
- const req = https.request(options);
199
-
200
- req.write(ackData);
201
- req.end();
202
-
203
- try {payload.d.author.self = payload.d.author.id == this.userID;} catch(e) {}
204
-
205
- if(typeof this.onMessageFunction == "function") this.onMessageFunction(payload.d);
206
- } else if(payload.t == "MESSAGE_UPDATE") {
207
- try {payload.d.author.self = payload.d.author.id == this.userID;} catch(e) {}
208
-
209
- if(typeof this.onEditFunction == "function") this.onEditFunction(payload.d);
210
- } else if(payload.t == "PRESENCE_UPDATE") {
211
- try {payload.d.author.self = payload.d.author.id == this.userID;} catch(e) {}
212
-
213
- if(typeof this.statusUpdateFunction == "function") this.statusUpdateFunction(payload.d);
214
- } else if(payload.t == "MESSAGE_DELETE") {
215
- if(typeof this.onDeleteFunction == "function") this.onDeleteFunction(payload.d);
216
- }
217
- } else if(payload.op == 7) {
218
- if(logMsgs) console.log("[MainControlSocket] Reconnect message received");
219
-
220
- if(this.hearbeatInterval) clearInterval(this.hearbeatInterval);
221
- this.socket.terminate();
222
-
223
- this.socket = new ws("wss://gateway.discord.gg?v=10&encoding=json");
224
-
225
- this.socket.on("open", function() {
226
- if(logMsgs) console.log("[MainControlSocket] Resume message sent");
227
-
228
- this.socket.send(module.exports.jsonEncode({
229
- op: 6,
230
- d: {
231
- token: this.token,
232
- session_id: this.sessionID,
233
- seq: this.sequenceID
234
- }
235
- }));
236
- }.bind(this));
237
-
238
- this.socket.on("message", socketMessageFunction);
239
- }
240
- }.bind(this);
241
-
242
- this.socket.on("message", socketMessageFunction);
262
+ this.socket.on("message", onSockMsg);
243
263
  }.bind(this));
244
264
  }
245
265
 
@@ -482,10 +502,10 @@ module.exports = {
482
502
  });
483
503
  }
484
504
 
485
- async getUserProfile(userID) {
505
+ async getUserProfile(userID, mutualGuilds=false) {
486
506
  return await this.makeRequest({
487
507
  method: "GET",
488
- path: `/api/v10/users/${userID}/profile?with_mutual_guilds=false`
508
+ path: `/api/v10/users/${userID}/profile?with_mutual_guilds=${mutualGuilds}`
489
509
  });
490
510
  }
491
511