-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
241 lines (200 loc) · 7.08 KB
/
Copy pathscript.js
File metadata and controls
241 lines (200 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Settings configuration
const urlParams = new URLSearchParams(window.location.search);
const sbAddress = urlParams.get("address") || "127.0.0.1";
const sbPort = urlParams.get("port") || "8080";
// Global variables
const streamerbot = "streamerbot";
const tikfinity = "tikfinity";
const sbfinity = "SBfinity";
let streamerbotConnected = false;
let tikfinityConnected = false;
let notifications = document.querySelector('.notifications');
// Streamer.Bot setup
const sbClient = new StreamerbotClient({
host: sbAddress,
port: sbPort,
onConnect: (data) => {
if (!streamerbotConnected){
streamerbotConnected = true;
console.debug(`✅ Streamer.bot connected to ${sbAddress}:${sbPort}`)
console.debug(data);
createToast('success', 'fa-solid fa-circle-check', sbfinity, 'Connected to Streamer.bot', streamerbot);
}
},
onDisconnect: () => {
if (streamerbotConnected) {
streamerbotConnected = false;
console.warn("❌ Streamer.bot disconnected");
createToast('warning', 'fa-solid fa-triangle-exclamation', sbfinity, 'Disconnected from Streamer.bot', streamerbot);
}
}
});
// sbClient.on('Custom.CodeEvent', (payload) => {
// const eventName = payload.data.eventName;
// if (eventName === "TikTokChatMessage") {
// console.log("CHAT PAYLOAD:", payload);
// } else {
// console.debug("FULL PAYLOAD:", payload);
// }
// // console.log('Custom.CodeEvent payload:', payload);
// });
// sbClient.on('Custom.CodeEvent', (payload) => {
// console.debug("FULL PAYLOAD:", payload);
// // console.log('Custom.CodeEvent payload:', payload);
// });
// sbClient.on("General.Custom", (payload) => {
// const eventName = payload.data.eventName;
// if (eventName === "TikTokChatMessage") {
// console.log("CHAT PAYLOAD:", payload);
// } else {
// console.debug("FULL PAYLOAD:", payload);
// }
// });
// TikFinity setup
function connectTikFinity() {
const socket = new WebSocket("ws://localhost:21213");
socket.onopen = () => {
if (!tikfinityConnected) {
tikfinityConnected = true;
console.log("✅ Connected to TikFinity");
createToast('success', 'fa-solid fa-circle-check', sbfinity, 'Connected to Tikfinity', tikfinity);
}
};
socket.onclose = () => {
if (tikfinityConnected) {
tikfinityConnected = false;
console.warn("❌ Disconnected from TikFinity");
createToast('warning', 'fa-solid fa-triangle-exclamation', sbfinity, 'Disconnected from Tikfinity', tikfinity);
}
setTimeout(connectTikFinity, 3000);
};
socket.onerror = (err) => {
console.error("TikFinity WebSocket error:", err);
};
socket.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
switch (data.event) {
case "gift": {
const gift = data.data;
// TikTok streak handling
if (gift.giftType === 1 && !gift.repeatEnd) return;
// ⭐ ALWAYS fire general gift trigger
sbClient.executeCodeTrigger("tikfinity.gift", gift);
// const rawName = gift.giftName || "";
// // ⭐ Step 1 — Check Allowed List
// const setKey = normalizeGiftForSet(rawName);
// if (!allowedGifts.has(setKey)) return;
// ⭐ Step 2 — Build Trigger Name
// const normalized = normalizeGiftTriggerName(rawName);
// const amount = gift.diamondCount;
// const eventName = `tikfinity.gift.${normalized}_${amount}`;
sbClient.executeCodeTrigger(`TikTokGift_${gift.giftId}`, gift);
console.debug(
`${gift.nickname || gift.uniqueId} sent ${gift.giftName} [${gift.giftId}] (${gift.diamondCount})`
);
break;
}
case "connected": {
const connected = data;
console.debug("SBfinity successfully connected to the live stream!");
sbClient.executeCodeTrigger("tikfinity.connected", connected);
break;
}
case "follow": {
const follow = data.data;
console.debug(`${follow.nickname || follow.uniqueId} followed!`);
sbClient.executeCodeTrigger("tikfinity.follow", follow);
break;
}
case "subscribe": {
const sub = data.data;
console.debug(`${sub.nickname || sub.uniqueId} subscribed!`);
sbClient.executeCodeTrigger("tikfinity.subscribe", sub);
break;
}
case "superFan": {
const sub = data.data;
console.debug(`${sub.nickname || sub.uniqueId} became a super fan!`, sub);
sbClient.executeCodeTrigger("tikfinity.superfan", sub);
break;
}
case "like": {
const like = data.data;
console.debug(`${like.nickname || like.uniqueId} sent ${like.likeCount} likes`, like);
sbClient.executeCodeTrigger("tikfinity.like", like);
break;
}
case "share": {
const share = data.data;
console.debug(`${share.nickname || share.uniqueId} shared the stream!`);
sbClient.executeCodeTrigger("tikfinity.share", share);
break;
}
case "chat": {
const chat = data.data;
console.debug("New Chat:", chat);
sbClient.executeCodeTrigger("tikfinity.chat", chat);
break;
}
case "envelope": {
const envelope = data.data;
console.debug('envelope received', data);
sbClient.executeCodeTrigger("tikfinity.envelope", envelope);
break;
}
case "roomPin": {
const roomPin = data.data;
console.debug('A message has been pinned.');
sbClient.executeCodeTrigger("tikfinity.roomPin", roomPin);
break;
}
case "streamEnd": {
const streamEnd = data.data;
console.log('Stream Ended.');
sbClient.executeCodeTrigger("tikfinity.streamEnd", streamEnd);
break;
}
default:
break;
}
} catch (err) {
console.error("Failed to process TikFinity event:", err);
}
};
}
// Toast notifications for connections
function createToast(type, icon, title, text, source){
let newToast = document.createElement('div');
let logo;
if (source === "streamerbot") {
logo = 'assets/images/streamerbot-logo.png';
} else if (source === "tikfinity") {
logo = "assets/images/tikfinity-logo.png";
}
newToast.innerHTML = `
<div class="toast ${type}">
<i class="${icon}"></i>
<div class="content">
<div class="title">${title}</div>
<span>${text}</span>
</div>
<img class="toast-logo" src="${logo}" alt="${source} logo">
</div>`;
notifications.appendChild(newToast);
newToast.timeOut = setTimeout(
()=>newToast.remove(), 3000
)
}
function normalizeGiftTriggerName(name = "") {
return name
.toLowerCase()
.trim()
.replace(/\+/g, " plus ")
.replace(/[^a-z0-9]+/g, "_")
.replace(/^_+|_+$/g, "");
}
function normalizeGiftForSet(name = "") {
return name.toLowerCase().trim();
}
connectTikFinity();