buzzk 1.9.3 → 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 -42
  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,27 +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
- setTimeout(async () => {
41
- this.#polling();
42
- }, 30000);
43
- }
44
- //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;
45
41
 
46
42
  //Get ChatID
47
43
  let cidRes = await getStatus(this.channelID);
48
- 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
+ }
49
51
  this.#chatID = cidRes.chatID;
50
52
  //Get ChatID
51
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
+
52
64
  //Get accTkn
53
65
  let accRes = await reqGame("nng_main/v1/chats/access-token?channelId=" + this.#chatID + "&chatType=STREAMING");
54
66
  if (accRes.code != 200) return resolve(null);
@@ -62,6 +74,7 @@ class chzzkChat {
62
74
 
63
75
  //Connect Web Socket
64
76
  this.#ws = new WebSocket("wss://kr-ss" + this.#ssID + ".chat.naver.com/chat");
77
+ this.#status.wsID++;
65
78
  //Connect Web Socket
66
79
 
67
80
  //WS Open
@@ -92,6 +105,8 @@ class chzzkChat {
92
105
 
93
106
  this.#ws.on("close", () => {
94
107
  console.log("[WS] Disconnect!");
108
+ this.#status.ws = false;
109
+ this.#status.ping = false;
95
110
  });
96
111
 
97
112
  //WS Message
@@ -122,7 +137,21 @@ class chzzkChat {
122
137
 
123
138
  //Connected
124
139
  else if (data.cmd == 10100) {
125
- 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
+
126
155
  resolve(true);
127
156
  }
128
157
  //Connected
@@ -138,7 +167,7 @@ class chzzkChat {
138
167
  */
139
168
  send(message) {
140
169
  return new Promise(async (resolve, reject) => {
141
- if (!this.#ws) return resolve(null);
170
+ if (!this.#status.ws) return resolve(null);
142
171
 
143
172
  let extras = {
144
173
  "chatType":"STREAMING",
@@ -202,13 +231,15 @@ class chzzkChat {
202
231
  type: "message",
203
232
  callback: callback
204
233
  };
205
- this.#onMessageHandler(callback);
234
+ this.#onMessageHandler(this.#status.wsID, callback);
206
235
  }
207
236
 
208
- #onMessageHandler (callback) {
209
- if (!this.#ws) return callback(null);
237
+ #onMessageHandler (wsID, callback) {
238
+ if (!this.#status.ws) return callback(null);
210
239
 
211
240
  this.#ws.on("message", async (data) => {
241
+ if (wsID != this.#status.wsID) return;
242
+
212
243
  try {
213
244
  data = await JSON.parse(data);
214
245
  }
@@ -276,7 +307,7 @@ class chzzkChat {
276
307
  }
277
308
 
278
309
  #onDonationHandler (callback) {
279
- if (!this.#ws) return callback(null);
310
+ if (!this.#status.ws) return callback(null);
280
311
 
281
312
  this.#ws.on("message", async (data) => {
282
313
  try {
@@ -343,7 +374,7 @@ class chzzkChat {
343
374
  * @param {function} callback
344
375
  */
345
376
  onDisconnect(callback) {
346
- if (!this.#ws) return callback(null);
377
+ if (!this.#status.ws) return callback(null);
347
378
 
348
379
  this.#ws.on("close", () => {
349
380
  callback();
@@ -356,7 +387,7 @@ class chzzkChat {
356
387
  */
357
388
  getRecentChat(size) {
358
389
  return new Promise(async (resolve, reject) => {
359
- if (!this.#ws) return resolve(null);
390
+ if (!this.#status.ws) return resolve(null);
360
391
 
361
392
  let reqOpt = {
362
393
  "ver": "2",
@@ -464,17 +495,18 @@ class chzzkChat {
464
495
  */
465
496
  async disconnect() {
466
497
  return new Promise(async (resolve, reject) => {
467
- if (!this.#ws) return resolve(null);
498
+ if (!this.#status.ws) return resolve(null);
468
499
 
469
500
  await this.#ws.close();
470
501
  this.#ws = null;
502
+ this.#status.ws = false;
503
+ this.#status.wsID++;
471
504
  return resolve(true);
472
505
  });
473
506
  }
474
507
 
475
508
  async #polling () {
476
- //if (!this.#ws) return this.#pollingStatus = false;
477
- this.#pollingStatus = true;
509
+ if (!this.#status.polling) return;
478
510
 
479
511
  //Get ChatID
480
512
  let cidRes = await getStatus(this.channelID);
@@ -482,16 +514,9 @@ class chzzkChat {
482
514
  this.#chatID = cidRes.chatID;
483
515
 
484
516
  //Reconnect
485
- this.#pollingStatus = false;
517
+ this.#status.reconnect = true;
486
518
  await this.disconnect();
487
- let connectRes = await this.connect();
488
- if (connectRes) {
489
- for (let o in this.#callbacks) {
490
- if (this.#callbacks[o].type === "message") this.#onMessageHandler(this.#callbacks[o].callback);
491
- else if (this.#callbacks[o].type === "donation") this.#onDonationHandler(this.#callbacks[o].callback);
492
- }
493
- return;
494
- }
519
+ await this.connect();
495
520
  //Reconnect
496
521
  }
497
522
  //Get ChatID
@@ -505,18 +530,18 @@ class chzzkChat {
505
530
  }, interval);
506
531
  }
507
532
 
508
- async #ping () {
509
- //if (!this.#ws) return this.#pingStatus = false;
510
- 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;
511
536
 
512
537
  let pingOpt = {
513
538
  "ver": "2",
514
539
  "cmd": 0
515
540
  };
516
- 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));
517
542
 
518
543
  setTimeout(() => {
519
- return this.#ping();
544
+ return this.#ping(pingID);
520
545
  }, 20000);
521
546
  }
522
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.3",
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
+ }