Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Fiszh authored Sep 28, 2024
1 parent 00ecc46 commit 6ad3ccb
Showing 1 changed file with 249 additions and 0 deletions.
249 changes: 249 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,116 @@ async function fetch7TVEmoteData(emoteSet) {
}
}

// 7TV WEBSOCKET

async function detect7TVEmoteSetChange() {
SevenTVWebsocket = new WebSocket(`wss://events.7tv.io/v3@emote_set.update<object_id=${SevenTVemoteSetId}>`);

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() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 6ad3ccb

Please sign in to comment.