buzzk 1.9.4 → 1.9.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/LICENSE +20 -20
  2. package/lib/chat.js +67 -44
  3. package/lib/live.js +1 -1
  4. package/package.json +36 -36
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Emin-G
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Emin-G
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/lib/chat.js CHANGED
@@ -19,8 +19,15 @@ class chzzkChat {
19
19
  #chatID;
20
20
  #callbacks = new Map();
21
21
 
22
- #pollingStatus = false;
23
- #pingStatus = false;
22
+ #status = {
23
+ polling: false,
24
+ pollingID: 0,
25
+ ping: false,
26
+ pingID: 0,
27
+ ws: false,
28
+ wsID: 0,
29
+ reconnect: false
30
+ }
24
31
  //Private
25
32
 
26
33
  /**
@@ -28,29 +35,32 @@ class chzzkChat {
28
35
  */
29
36
  connect() {
30
37
  return new Promise(async (resolve, reject) => {
31
- if (this.#ws) return resolve(null);
32
-
33
- //Get UID
34
- let myInfo = await reqGame("nng_main/v1/user/getUserStatus");
35
- if (myInfo.code != 200) return resolve(null);
36
- this.#uid = myInfo.content.userIdHash;
37
-
38
- //SetPolling
39
- if (!this.#pollingStatus) {
40
- this.#pollingStatus = true;
41
-
42
- setTimeout(async () => {
43
- this.#polling();
44
- }, 30000);
45
- }
46
- //SetPolling
38
+ if (this.#status.ws) return resolve(null);
39
+ if (this.#status.polling && !this.#status.reconnect) return resolve(null);
40
+ else this.#status.reconnect = false;
47
41
 
48
42
  //Get ChatID
49
43
  let cidRes = await getStatus(this.channelID);
50
- if (!cidRes) return resolve(null);
44
+ if (!cidRes) {
45
+ if (!this.#status.polling) {
46
+ this.#status.polling = true;
47
+ this.#polling();
48
+ }
49
+ return resolve(null);
50
+ }
51
51
  this.#chatID = cidRes.chatID;
52
52
  //Get ChatID
53
53
 
54
+ //Set Status
55
+ this.#status.ws = true;
56
+ //Set Status
57
+
58
+ //Get UID
59
+ let myInfo = await reqGame("nng_main/v1/user/getUserStatus");
60
+ if (myInfo.code != 200) return resolve(null);
61
+ this.#uid = myInfo.content.userIdHash;
62
+ //Get UID
63
+
54
64
  //Get accTkn
55
65
  let accRes = await reqGame("nng_main/v1/chats/access-token?channelId=" + this.#chatID + "&chatType=STREAMING");
56
66
  if (accRes.code != 200) return resolve(null);
@@ -64,6 +74,7 @@ class chzzkChat {
64
74
 
65
75
  //Connect Web Socket
66
76
  this.#ws = new WebSocket("wss://kr-ss" + this.#ssID + ".chat.naver.com/chat");
77
+ this.#status.wsID++;
67
78
  //Connect Web Socket
68
79
 
69
80
  //WS Open
@@ -94,6 +105,8 @@ class chzzkChat {
94
105
 
95
106
  this.#ws.on("close", () => {
96
107
  console.log("[WS] Disconnect!");
108
+ this.#status.ws = false;
109
+ this.#status.ping = false;
97
110
  });
98
111
 
99
112
  //WS Message
@@ -124,7 +137,21 @@ class chzzkChat {
124
137
 
125
138
  //Connected
126
139
  else if (data.cmd == 10100) {
127
- if (!this.#pingStatus) this.#ping();
140
+ if (!this.#status.ping) {
141
+ this.#status.ping = true;
142
+ this.#status.pingID++;
143
+ this.#ping(this.#status.pingID);
144
+ }
145
+ if (!this.#status.polling) {
146
+ this.#status.polling = true;
147
+ this.#polling();
148
+ }
149
+
150
+ for (let o in this.#callbacks) {
151
+ if (this.#callbacks[o].type === "message") this.#onMessageHandler(this.#status.wsID, this.#callbacks[o].callback);
152
+ else if (this.#callbacks[o].type === "donation") this.#onDonationHandler(this.#status.wsID, this.#callbacks[o].callback);
153
+ }
154
+
128
155
  resolve(true);
129
156
  }
130
157
  //Connected
@@ -140,7 +167,7 @@ class chzzkChat {
140
167
  */
141
168
  send(message) {
142
169
  return new Promise(async (resolve, reject) => {
143
- if (!this.#ws) return resolve(null);
170
+ if (!this.#status.ws) return resolve(null);
144
171
 
145
172
  let extras = {
146
173
  "chatType":"STREAMING",
@@ -204,13 +231,15 @@ class chzzkChat {
204
231
  type: "message",
205
232
  callback: callback
206
233
  };
207
- this.#onMessageHandler(callback);
234
+ this.#onMessageHandler(this.#status.wsID, callback);
208
235
  }
209
236
 
210
- #onMessageHandler (callback) {
211
- if (!this.#ws) return callback(null);
237
+ #onMessageHandler (wsID, callback) {
238
+ if (!this.#status.ws) return callback(null);
212
239
 
213
240
  this.#ws.on("message", async (data) => {
241
+ if (wsID != this.#status.wsID) return;
242
+
214
243
  try {
215
244
  data = await JSON.parse(data);
216
245
  }
@@ -278,7 +307,7 @@ class chzzkChat {
278
307
  }
279
308
 
280
309
  #onDonationHandler (callback) {
281
- if (!this.#ws) return callback(null);
310
+ if (!this.#status.ws) return callback(null);
282
311
 
283
312
  this.#ws.on("message", async (data) => {
284
313
  try {
@@ -345,7 +374,7 @@ class chzzkChat {
345
374
  * @param {function} callback
346
375
  */
347
376
  onDisconnect(callback) {
348
- if (!this.#ws) return callback(null);
377
+ if (!this.#status.ws) return callback(null);
349
378
 
350
379
  this.#ws.on("close", () => {
351
380
  callback();
@@ -358,7 +387,7 @@ class chzzkChat {
358
387
  */
359
388
  getRecentChat(size) {
360
389
  return new Promise(async (resolve, reject) => {
361
- if (!this.#ws) return resolve(null);
390
+ if (!this.#status.ws) return resolve(null);
362
391
 
363
392
  let reqOpt = {
364
393
  "ver": "2",
@@ -466,17 +495,18 @@ class chzzkChat {
466
495
  */
467
496
  async disconnect() {
468
497
  return new Promise(async (resolve, reject) => {
469
- if (!this.#ws) return resolve(null);
498
+ if (!this.#status.ws) return resolve(null);
470
499
 
471
500
  await this.#ws.close();
472
501
  this.#ws = null;
502
+ this.#status.ws = false;
503
+ this.#status.wsID++;
473
504
  return resolve(true);
474
505
  });
475
506
  }
476
507
 
477
508
  async #polling () {
478
- //if (!this.#ws) return this.#pollingStatus = false;
479
- this.#pollingStatus = true;
509
+ if (!this.#status.polling) return;
480
510
 
481
511
  //Get ChatID
482
512
  let cidRes = await getStatus(this.channelID);
@@ -484,16 +514,9 @@ class chzzkChat {
484
514
  this.#chatID = cidRes.chatID;
485
515
 
486
516
  //Reconnect
487
- this.#pollingStatus = false;
517
+ this.#status.reconnect = true;
488
518
  await this.disconnect();
489
- let connectRes = await this.connect();
490
- if (connectRes) {
491
- for (let o in this.#callbacks) {
492
- if (this.#callbacks[o].type === "message") this.#onMessageHandler(this.#callbacks[o].callback);
493
- else if (this.#callbacks[o].type === "donation") this.#onDonationHandler(this.#callbacks[o].callback);
494
- }
495
- return;
496
- }
519
+ await this.connect();
497
520
  //Reconnect
498
521
  }
499
522
  //Get ChatID
@@ -507,18 +530,18 @@ class chzzkChat {
507
530
  }, interval);
508
531
  }
509
532
 
510
- async #ping () {
511
- //if (!this.#ws) return this.#pingStatus = false;
512
- this.#pingStatus = true;
533
+ async #ping (pingID) {
534
+ if (!this.#status.ws) return this.#status.ping = false;
535
+ if (!this.#status.ping || pingID != this.#status.pingID) return;
513
536
 
514
537
  let pingOpt = {
515
538
  "ver": "2",
516
539
  "cmd": 0
517
540
  };
518
- if (this.#ws && this.#ws.readyState == WebSocket.OPEN) this.#ws.send(JSON.stringify(pingOpt));
541
+ if (this.#ws.readyState == WebSocket.OPEN) this.#ws.send(JSON.stringify(pingOpt));
519
542
 
520
543
  setTimeout(() => {
521
- return this.#ping();
544
+ return this.#ping(pingID);
522
545
  }, 20000);
523
546
  }
524
547
  }
package/lib/live.js CHANGED
@@ -83,7 +83,7 @@ async function getStatus (channelID) {
83
83
  return new Promise(async (resolve, reject) => {
84
84
 
85
85
  let res = await reqChzzk("polling/v2/channels/" + channelID + "/live-status");
86
- if (res.code != 200) return resolve(null);
86
+ if (res.code != 200 || !res.content) return resolve(null);
87
87
  res = res.content;
88
88
 
89
89
  try {
package/package.json CHANGED
@@ -1,36 +1,36 @@
1
- {
2
- "name": "buzzk",
3
- "displayName": "BUZZK",
4
- "version": "1.9.4",
5
- "description": "뿌지직 (BUZZK) - 치지직(CHZZK) 챗봇을 더욱 쉽게 개발할 수 있도록 돕는 비공식 라이브러리.",
6
- "main": "lib/index.js",
7
- "type": "commonjs",
8
- "directories": {
9
- "lib": "lib"
10
- },
11
- "scripts": {
12
- "test": "echo \"Error: no test specified\" && exit 1"
13
- },
14
- "author": {
15
- "name": "Emin-G",
16
- "email": "dev.mingowo@gmail.com"
17
- },
18
- "license": "MIT",
19
- "repository": {
20
- "type": "git",
21
- "url": "git+https://github.com/Emin-G/buzzk.git"
22
- },
23
- "bugs": {
24
- "url": "https://github.com/Emin-G/buzzk/issues"
25
- },
26
- "homepage": "https://github.com/Emin-G/buzzk",
27
- "keywords": [
28
- "chzzk",
29
- "buzzk",
30
- "치지직",
31
- "뿌지직"
32
- ],
33
- "dependencies": {
34
- "ws": "^8.16.0"
35
- }
36
- }
1
+ {
2
+ "name": "buzzk",
3
+ "displayName": "BUZZK",
4
+ "version": "1.9.5",
5
+ "description": "뿌지직 (BUZZK) - 치지직(CHZZK) 챗봇을 더욱 쉽게 개발할 수 있도록 돕는 비공식 라이브러리.",
6
+ "main": "lib/index.js",
7
+ "type": "commonjs",
8
+ "directories": {
9
+ "lib": "lib"
10
+ },
11
+ "scripts": {
12
+ "test": "echo \"Error: no test specified\" && exit 1"
13
+ },
14
+ "author": {
15
+ "name": "Emin-G",
16
+ "email": "dev.mingowo@gmail.com"
17
+ },
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/Emin-G/buzzk.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/Emin-G/buzzk/issues"
25
+ },
26
+ "homepage": "https://github.com/Emin-G/buzzk",
27
+ "keywords": [
28
+ "chzzk",
29
+ "buzzk",
30
+ "치지직",
31
+ "뿌지직"
32
+ ],
33
+ "dependencies": {
34
+ "ws": "^8.16.0"
35
+ }
36
+ }