From bef7a6a629539c6377bf5dc35b5aabd9ca72a256 Mon Sep 17 00:00:00 2001 From: d2n0s4ur Date: Tue, 13 Feb 2024 22:42:37 +0900 Subject: [PATCH 1/2] :ambulance: Fix critical bug --- fixup.sh | 2 +- package.json | 2 +- src/index.js | 5 -- src/lib/chzzkChat.d.ts | 22 --------- src/lib/chzzkChat.js | 108 ----------------------------------------- 5 files changed, 2 insertions(+), 137 deletions(-) delete mode 100644 src/index.js delete mode 100644 src/lib/chzzkChat.d.ts delete mode 100644 src/lib/chzzkChat.js diff --git a/fixup.sh b/fixup.sh index 4d7a38b..5fb9db4 100755 --- a/fixup.sh +++ b/fixup.sh @@ -2,7 +2,7 @@ cp dist/mjs/index.d.ts dist rm -rf dist/*/index.d.ts mkdir dist/lib -cp src/lib/chzzkChat.d.ts dist/lib/ +cp dist/mjs/lib/* dist/lib cat >dist/cjs/package.json < void; -export declare class ChzzkChat { - private initialization; - ws: WebSocket | undefined; - messageHandler: messageHandler; - chzzkChannelId: string; - chatChannelAccessToken: string; - chatChannelId: string; - sid: string; - uuid: string; - addMessageHandler: (handler: messageHandler) => void; - getChatChannelId: (chzzkChannelId: string) => Promise; - getChatChannelAccessToken: (chatChannelId: string) => Promise; - parseBadgeUrl: (badge: any[] | undefined) => string[]; - init: () => Promise; - constructor(chzzkChannelId: string); - onOpen(event: Event): void; - onClose(event: CloseEvent): void; - onError(event: Event): void; - onMessage(event: MessageEvent): void; - closs: () => void; -} diff --git a/src/lib/chzzkChat.js b/src/lib/chzzkChat.js deleted file mode 100644 index ec02a73..0000000 --- a/src/lib/chzzkChat.js +++ /dev/null @@ -1,108 +0,0 @@ -const chzzkIRCUrl = "wss://kr-ss3.chat.naver.com/chat"; -export class ChzzkChat { - initialization; - ws; - messageHandler; - chzzkChannelId; - chatChannelAccessToken; - chatChannelId; - sid; - uuid; - addMessageHandler = (handler) => { - this.messageHandler = handler; - }; - getChatChannelId = async (chzzkChannelId) => { - const url = `https://api.chzzk.naver.com/polling/v2/channels/${chzzkChannelId}/live-status`; - const response = await fetch(url); - const data = await response.json(); - return data.content.chatChannelId; - }; - getChatChannelAccessToken = async (chatChannelId) => { - const url = `https://comm-api.game.naver.com/nng_main/v1/chats/access-token?channelId=${chatChannelId}&chatType=STREAMING`; - const response = await fetch(url); - const data = await response.json(); - return data.content.accessToken; - }; - parseBadgeUrl = (badge) => { - if (!badge) - return []; - return badge.map((b) => { - return b.imageUrl; - }); - }; - init = async () => { - this.chatChannelId = await this.getChatChannelId(this.chzzkChannelId); - this.chatChannelAccessToken = await this.getChatChannelAccessToken(this.chatChannelId); - this.ws = new WebSocket(chzzkIRCUrl); - this.ws.onopen = this.onOpen.bind(this); - this.ws.onmessage = this.onMessage.bind(this); - this.ws.onclose = this.onClose.bind(this); - this.ws.onerror = this.onError.bind(this); - }; - constructor(chzzkChannelId) { - this.chzzkChannelId = chzzkChannelId; - this.chatChannelId = ""; - this.chatChannelAccessToken = ""; - this.sid = ""; - this.uuid = ""; - // this.ws = undefined; - this.messageHandler = () => { }; - this.initialization = this.init(); - } - onOpen(event) { - if (!this.ws) - return; - console.log("Connected to Chzzk IRC"); - this.ws.send(`{"ver":"2","cmd":100,"svcid":"game","cid":"${this.chatChannelId}","bdy":{"uid":null,"devType":2001,"accTkn":"${this.chatChannelAccessToken}","auth":"READ"},"tid":1}`); - } - onClose(event) { - console.log("Disconnected from Chzzk IRC"); - } - onError(event) { - console.log("Error from Chzzk IRC: ", event); - } - onMessage(event) { - if (!this.ws) - return; - const data = JSON.parse(event.data.toString()); - switch (data.cmd) { - case 10100: - this.sid = data.bdy.sid; - this.uuid = data.bdy.uuid; - console.log(`Connected to Chzzk Chat Successfully: ${this.sid}`); - break; - case 0: - this.ws.send(`{"ver": "2", "cmd": 10000}`); - break; - case 94008: - // cleanBot message - break; - default: - if (!data.bdy) - return; - data.bdy.forEach((msg) => { - const profile = JSON.parse(msg.profile); - switch (data.cmd) { - case 93101: - if (!this.messageHandler) - return; - this.messageHandler(this.parseBadgeUrl(profile.activityBadges), profile ? profile.nickname : "익명", msg.msgTypeCode === "CBOTBLIND" - ? "클린봇에 의해 삭제된 메시지입니다." - : msg.msg, false); - break; - case 93102: - const extras = JSON.parse(data.bdy[0].extras); - if (!this.messageHandler) - return; - this.messageHandler([], profile ? profile.nickname : "익명", msg.msg, true, extras.payAmount); - break; - } - }); - } - } - closs = () => { - if (!this.ws) - return; - this.ws.close(); - }; -} From f24d38c12fef09c358f04acf4460da06ac0d6b70 Mon Sep 17 00:00:00 2001 From: d2n0s4ur Date: Tue, 13 Feb 2024 22:52:34 +0900 Subject: [PATCH 2/2] :ambulance: Hotfix --- package.json | 2 +- src/lib/chzzkChat.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 296ede7..6594827 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@d2n0s4ur/chzzk-chat", - "version": "1.2.1", + "version": "1.2.2", "description": "chzzk-chat: node.js 개발자를 위한 chzzk 채팅 & 후원 비공식 라이브러리", "main": "dist/cjs/index.js", "module": "dist/mjs/index.js", diff --git a/src/lib/chzzkChat.ts b/src/lib/chzzkChat.ts index b6ecf33..f78b0c6 100644 --- a/src/lib/chzzkChat.ts +++ b/src/lib/chzzkChat.ts @@ -120,7 +120,7 @@ export class ChzzkChat { case 93101: // default message if (!this.messageHandler) return; this.messageHandler( - this.parseBadgeUrl(profile.activityBadges as string[]), + this.parseBadgeUrl(profile ? profile.activityBadges : undefined), profile.nickname, msg.msgStatusType === "CBOTBLIND" ? "클린봇에 의해 삭제된 메시지입니다." @@ -131,7 +131,7 @@ export class ChzzkChat { const extras = JSON.parse(data.bdy[0].extras); if (!this.donationHandler) return; this.donationHandler( - this.parseBadgeUrl(profile.activityBadges as string[]), + this.parseBadgeUrl(profile ? profile.activityBadges : undefined), msg.uid !== "anonymous" ? profile.nickname : "익명의 후원자", msg.msg, msg.uid === "anonymous",