-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
274 lines (238 loc) · 8.99 KB
/
index.js
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Webcam Dimensions
X: 640px
Y: 360px
Main Source takes up 7/8 of x/y for 1440p
This leaves 1/8 of screen space on two sides of the main source
*/
import * as Config from "./config.js";
//import * as _TES from
// "https://cdn.jsdelivr.net/gh/mitchwadair/[email protected]/dist/tes.min.js";
/*************
* Utilities *
*************/
function getTicker(SubType) {
return document.getElementById(SubType);
}
function updateTicker(SubType, text) {
getTicker(SubType).innerHTML = text;
}
function bulkUpdateTickers() {
for (let ticker in ACTIVITY_LOGS) {
updateTicker(ticker, FormatText[ticker](ACTIVITY_LOGS[ticker]));
}
}
function getValuesFromArray(ArrayOfObjects, KeyName) {
let results = [];
function getValues(Obj) {
results.push(Obj[KeyName]);
}
ArrayOfObjects.forEach(getValues);
return results;
}
function getKeysFromArray(ArrayOfObjects) {
let results = [];
function getKeys(Obj) {
results.push(Obj.keys());
}
ArrayOfObjects.forEach(getKeys);
return results;
}
function clearActivityLogs() {
for (let log in ACTIVITY_LOGS) {
ACTIVITY_LOGS[log] = [];
}
}
function setTestData() {
for (let log in TEST_DATA_ACTIVITY_LOGS) {
ACTIVITY_LOGS[log] = TEST_DATA_ACTIVITY_LOGS[log];
}
}
const FormatText = {
"channel.follow": (string) => {
return string.join(" | ");
},
"channel.subscribe": (string) => {
return string;
},
"channel.subscription.gift": (string) => {
return string;
},
"channel.subscription.message": (string) => {
return string;
},
"channel.cheer": (string) => {
return string;
},
"channel.raid": (string) => {
return string;
},
}
/******************
* Initialization *
******************/
const _TES = new TES({
identity: {
id: Config.TWITCH_CLIENT_ID,
accessToken: Config.TWITCH_AUTH_TOKEN(),
},
listener: {
type: "websocket",
}
});
let ActiveKeyboardListening = 0;
addEventListener("keydown", (event) => {
if (event.isComposing || event.keyCode === 229 || ActiveKeyboardListening === 0) {
if (event.code === "Slash") { ActiveKeyboardListening = 1; console.log("listening to keyevents"); }
return;
}
console.log(event);
if (event.code === "Slash") { ActiveKeyboardListening = 0; console.log("ignoring keyevents"); }
if (event.code === "KeyR") {
clearActivityLogs();
bulkUpdateTickers();
}
if (event.code === "KeyT") {
setTestData();
bulkUpdateTickers();
}
});
/***********************
* Data Drives the Bus *
***********************/
const TEST_DATA_ACTIVITY_LOGS = {
"channel.follow": ["Stapleton", "alloybot", "only_cans", "spiteinc", "t__lk__t", "thisisareallylongusernameandithinkitmightbelongenoughforsomecssstyling"],
"channel.subscribe": ["Stapleton", "alloybot", "only_cans", "spiteinc", "t__lk__t", "thisisareallylongusernameandithinkitmightbelongenoughforsomecssstyling"],
"channel.subscription.gift": ["Stapleton", "alloybot", "only_cans", "spiteinc", "t__lk__t", "thisisareallylongusernameandithinkitmightbelongenoughforsomecssstyling"],
"channel.subscription.message": ["Stapleton", "alloybot", "only_cans", "spiteinc", "t__lk__t", "thisisareallylongusernameandithinkitmightbelongenoughforsomecssstyling"],
"channel.cheer": ["Stapleton", "alloybot", "only_cans", "spiteinc", "t__lk__t", "thisisareallylongusernameandithinkitmightbelongenoughforsomecssstyling"],
"channel.raid": ["Stapleton", "alloybot", "only_cans", "spiteinc", "t__lk__t", "thisisareallylongusernameandithinkitmightbelongenoughforsomecssstyling"]
}
const ACTIVITY_LOGS = {
"channel.follow": [],
"channel.subscribe": [],
"channel.subscription.gift": [],
"channel.subscription.message": [],
"channel.cheer": [],
"channel.raid": []
}
const SUBSCRIPTIONS_AND_CONDITIONS = {
// SubscriptionName: [ {condition}, versionNumber ]
"channel.follow": [{ "broadcaster_user_id": Config.CASTER_USER_ID, "moderator_user_id": Config.CASTER_USER_ID }, 2],/*
"channel.subscribe": [{ "broadcaster_user_id": Config.CASTER_USER_ID }, 1],
"channel.subscription.gift": [{ "broadcaster_user_id": Config.CASTER_USER_ID }, 1],
"channel.subscription.message": [{ "broadcaster_user_id": Config.CASTER_USER_ID }, 1],
"channel.cheer": [{ "broadcaster_user_id": Config.CASTER_USER_ID }, 1],
"channel.raid": [{ "to_broadcaster_user_id": Config.CASTER_USER_ID }, 1]*/
};
const EVENTTYPES_AND_HANDLERS = {
"revocation": (subscriptionData) => {
console.log(`Subscription ${subscriptionData.id} has been revoked`);
// perform necessary cleanup here
},
"connection_lost": (subscriptions) => {
// if your subscriptions are important to you, resubscribe to them
// Object.values(subscriptions).forEach((subscription) => {
//tes.subscribe(subscription.type, subscription.condition);
//});
console.log(`Connection Lost. Wait at least 1 minute before reconnecting.\nLost Subscriptions: ${subscriptions}`)
},
"channel.follow": (subscription, event) => {
let clean = {
// use the user id for random coloring of something in the Overlay
"follower_name": event.user_name,
"caster_name": event.broadcaster_user_name,
"followed_at": event.followed_at
}
ACTIVITY_LOGS[subscription.type].unshift(clean);
let txt = FormatText[subscription.type](getValuesFromArray(ACTIVITY_LOGS[subscription.type], subscription.type));
updateTicker(subscription.type, txt);
},
"channel.subscribe": (subscription, event) => {
let clean = {
// use the user id for random coloring of something in the Overlay
"subscriber_name": event.user_name,
"caster_name": event.broadcaster_user_name,
"tier": event.tier,
"is_gift": event.is_gift
}
ACTIVITY_LOGS[subscription.type].unshift(clean);
},
"channel.subscription.gift": (subscription, event) => {
let clean = {
// use the user id for random coloring of something in the Overlay
"gifter_name": event.user_name,
"caster_name": event.broadcaster_user_name,
"total_gifts": event.total,
"gift_tier": event.tier,
"gifter_total": event.cumulative_total,
"is_anonymous": event.is_anonymous
}
ACTIVITY_LOGS[subscription.type].unshift(clean);
},
"channel.subscription.message": (subscription, event) => {
let clean = {
"subscriber_name": event.user_name,
"caster_name": event.broadcaster_user_name,
"tier": event.tier,
"message": event.message,
"cumulative_months": event.cumulative_months,
"streak_months": event.streak_months,
"duration_months": event.duration_months
}
ACTIVITY_LOGS[subscription.type].unshift(clean);
},
"channel.cheer": (subscription, event) => {
let clean = {
"cheerer_name": event.is_anonymous ? "Anonymous" : event.user_name,
"caster_name": event.broadcaster_user_name,
"message": event.message,
"bits": event.bits
}
ACTIVITY_LOGS[subscription.type].unshift(clean);
},
"channel.raid": (subscription, event) => {
let clean = {
"raider_name": event.from_broadcaster_user_name,
"caster_name": event.to_broadcaster_user_name,
"raidsize": event.viewers
}
ACTIVITY_LOGS[subscription.type].unshift(clean);
},
}
/*************
* Registers *
*************/
function RegisterSubscriptions(SubsAndConditions) {
let Subs = [];
for (let SubscriptionName in SubsAndConditions) {
let a = { cond: SubsAndConditions[SubscriptionName][0], version: SubsAndConditions[SubscriptionName][1] }
let b = {
Sub: _TES.subscribe(SubscriptionName, a.cond, a.version),
Resolve: (value) => {
console.log(`Created subscription to ${value.type}, subscription id ${value.id}`);
},
Reject: (err) => {
console.error(`Failed to create Subscription: ${SubscriptionName}\n\n${err}`);
}
}
Subs.push(b.Sub);
}
return Subs;
}
function RegisterEventHandlers(SettledPromises) {
for (let promise in SettledPromises) {
let p = SettledPromises[promise];
if (p.status === "rejected") {
console.log(`Failed to register Event Handler for ${p.value.type}\nPromise Failure Reason: ${p.value.reason}`);
continue;
}
console.log(p);
_TES.on(p.value.type, EVENTTYPES_AND_HANDLERS[p.value.type]);
updateTicker(p.value.type, ACTIVITY_LOGS[p.value.type].join(" | "));
}
}
/*****************************
* One Line To Rule Them All *
*****************************/
RegisterEventHandlers(await Promise.allSettled(RegisterSubscriptions(SUBSCRIPTIONS_AND_CONDITIONS)));