From 6ad3ccb35e712ebc2fc04d1bab53660b7d267fbd Mon Sep 17 00:00:00 2001 From: Killa Fish <61081555+Fiszh@users.noreply.github.com> Date: Sat, 28 Sep 2024 19:44:41 +0200 Subject: [PATCH] Update index.js --- index.js | 249 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 249 insertions(+) diff --git a/index.js b/index.js index 9b34e75..5715395 100644 --- a/index.js +++ b/index.js @@ -1155,6 +1155,116 @@ async function fetch7TVEmoteData(emoteSet) { } } +// 7TV WEBSOCKET + +async function detect7TVEmoteSetChange() { + SevenTVWebsocket = new WebSocket(`wss://events.7tv.io/v3@emote_set.update`); + + SevenTVWebsocket.onopen = async () => { + console.log(FgBlue + 'SevenTV ' + FgWhite + 'WebSocket connection opened.'); + await handleMessage(SevenTVServerUserstate, 'WEBSOCKET OPEN') + }; + + SevenTVWebsocket.onmessage = async (event) => { + try { + const message = JSON.parse(event.data); + + if (message && message.d && message.d.body) { + const body = message.d.body; + + let tableData = { + name: 'none', + url: `4x.webp`, + flags: 0, + site: '', + action: 'other' + }; + + if (body["pushed"]) { + if (!body.pushed[0]) { return; } + + const owner = body.pushed[0].value.data?.owner; + + const creator = owner && Object.keys(owner).length > 0 + ? owner.display_name || owner.username || "UNKNOWN" + : "NONE"; + + tableData = { + name: body.pushed[0].value.name, + url: `https://cdn.7tv.app/emote/${body.pushed[0]["value"].id}/4x.webp`, + flags: body.pushed[0].value.data?.flags, + original_name: body.pushed[0].value.data?.name, + creator, + site: '7TV', + user: body.actor["display_name"], + action: 'add' + }; + } else if (body["pulled"]) { + if (!body.pulled[0]) { return; } + tableData = { + name: body.pulled[0]["old_value"].name, + url: `https://cdn.7tv.app/emote/${body.pulled[0]["old_value"].id}/4x.webp`, + user: body.actor["display_name"], + action: 'remove' + }; + } else if (body["updated"]) { + if (!body.updated[0]) { return; } + + tableData = { + newName: body.updated[0]["value"].name, + oldName: body.updated[0]["old_value"].name, + user: body.actor["display_name"], + // url: `https://cdn.7tv.app/emote/${body.updated[0]["old_value"].id}/4x.webp`, + // flags: body.updated[0]["old_value"].flags, + site: '7TV', + action: 'update' + }; + } + + update7TVEmoteSet(tableData) + } + } catch (error) { + console.log('Error parsing message:', error); + } + }; + + SevenTVWebsocket.onerror = async (error) => { + console.log(FgBlue + 'SevenTV ' + FgWhite + 'WebSocket error:', error); + }; + + SevenTVWebsocket.onclose = async () => { + console.log(FgBlue + 'SevenTV ' + FgWhite + 'WebSocket connection closed.'); + await handleMessage(SevenTVServerUserstate, 'WEBSOCKET CLOSED'); + detect7TVEmoteSetChange(); + }; +} + +async function update7TVEmoteSet(table) { + if (table.url === '4x.webp') { return; } + + if (table.action === 'add') { + delete table.action; + SevenTVEmoteData.push(table); + + await handleMessage(SevenTVServerUserstate, `${table.user} ADDED ${table.name}`); + } else if (table.action === 'remove') { + let foundEmote = SevenTVEmoteData.find(emote => emote.original_name === table.name); + await handleMessage(SevenTVServerUserstate, `${table.user} REMOVED ${foundEmote.name}`); + + SevenTVEmoteData = SevenTVEmoteData.filter(emote => emote.url !== table.url); + } else if (table.action === 'update') { + let foundEmote = SevenTVEmoteData.find(emote => emote.name === table.oldName); + foundEmote.name = table.newName + //SevenTVEmoteData.push(table); + + await handleMessage(SevenTVServerUserstate, `${table.user} RENAMED ${table.oldName} TO ${table.newName}`); + + //SevenTVEmoteData = SevenTVEmoteData.filter(emote => emote.name !== table.oldName); + } + + await updateAllEmoteData(); +} + // BTTV async function loadBTTV() { @@ -1226,6 +1336,145 @@ async function fetchBTTVEmoteData(channel) { } } +// BTTV WEBSOCKET + +async function detectBTTVEmoteSetChange() { + BTTVWebsocket = new WebSocket(`wss://sockets.betterttv.net/ws`); + + BTTVWebsocket.onopen = async () => { + console.log(FgRed + 'BetterTwitchTV ' + FgWhite + 'WebSocket connection opened.'); + + const message = { + name: 'join_channel', + data: { + name: `twitch:${channelTwitchID}` + } + }; + + BTTVWebsocket.send(JSON.stringify(message)); + + await handleMessage(BTTVServerUserstate, 'WEBSOCKET OPEN') + }; + + BTTVWebsocket.onmessage = async (event) => { + try { + const message = JSON.parse(event.data); + + if (message && message.name && message.data) { + const messageType = message.name; + const messageData = message.data; + let userName; + + if (messageData.channel) { + userName = await getUsernameFromUserId(messageData.channel.split(':')[1]) + } + + let tableData = { + name: 'none', + url: `4x.webp`, + flags: 0, + site: '', + action: 'other' + }; + + if (messageType === 'emote_create') { + if (!messageData.emote) { return; } + const emoteData = messageData.emote + + tableData = { + name: emoteData.code, + url: `https://cdn.betterttv.net/emote/${emoteData.id}/3x`, + flags: 0, + user: userName, + site: 'BTTV', + action: 'add' + }; + } else if (messageType === 'emote_delete') { + const emoteFound = await BTTVEmoteData.find(emote => emote.url === `https://cdn.betterttv.net/emote/${messageData.emoteId}/3x`); + + let emoteName = ''; + if (emoteFound) { + emoteName = emoteFound.name + } + + tableData = { + name: emoteName, + url: `https://cdn.betterttv.net/emote/${messageData.id}/3x`, + flags: 0, + user: userName, + site: 'BTTV', + action: 'remove' + }; + } else if (messageType === 'emote_update') { + if (!messageData.emote) { return; } + const emoteData = messageData.emote + + tableData = { + name: emoteData.code, + url: `https://cdn.betterttv.net/emote/${emoteData.id}/3x`, + flags: 0, + user: userName, + site: 'BTTV', + action: 'update' + }; + } + + updateBTTVEmoteSet(tableData) + } + } catch (error) { + console.log('Error parsing message:', error); + } + }; + + BTTVWebsocket.onerror = async (error) => { + console.log(FgRed + 'BetterTwitchTV ' + FgWhite + 'WebSocket error:', error); + }; + + BTTVWebsocket.onclose = async () => { + console.log(FgRed + 'BetterTwitchTV ' + FgWhite + 'WebSocket connection closed.'); + await handleMessage(BTTVServerUserstate, 'WEBSOCKET CLOSED'); + detectBTTVEmoteSetChange(); + }; +} + +async function updateBTTVEmoteSet(table) { + if (table.url === '4x.webp') { return; } + + if (table.action === 'add') { + BTTVEmoteData.push({ + name: table.name, + url: table.url, + flags: table.flags, + site: table.site + }); + + await handleMessage(BTTVServerUserstate, `${table.user} ADDED ${table.name}`); + } else if (table.action === 'remove') { + if (table.name !== '') { + await handleMessage(BTTVServerUserstate, `${table.user} REMOVED ${table.name}`); + + BTTVEmoteData = BTTVEmoteData.filter(emote => emote.name !== table.name); + } else { + await handleMessage(BTTVServerUserstate, `EMOTE WAS REMOVED BUT WE ARE UNABLE TO FIND IT`); + } + } else if (table.action === 'update') { + const emoteFound = BTTVEmoteData.find(emote => emote.url === table.url); + + BTTVEmoteData.push({ + name: table.name, + url: table.url, + flags: table.flags, + site: table.site + }); + + await handleMessage(BTTVServerUserstate, `BTTV ${table.user} RENAMED ${emoteFound.name} TO ${table.name}`); + + BTTVEmoteData = BTTVEmoteData.filter(emote => emote.name !== emoteFound.name); + } + + await updateAllEmoteData(); +} + // FFZ async function loadFFZ() {