-
Notifications
You must be signed in to change notification settings - Fork 5
/
extension.js
238 lines (193 loc) · 5.66 KB
/
extension.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
'use strict';
const DEFAULT_CHEER = {
channel: 'none',
message: '',
userstate: {
bits: 0,
'display-name': 'N/A'
}
};
const EventEmitter = require('events');
const equal = require('deep-equal');
const clone = require('clone');
module.exports = function (nodecg) {
const socket = require('socket.io-client')(nodecg.bundleConfig.streen.url);
const self = new EventEmitter();
const channels = nodecg.bundleConfig.channels;
const topCheers = nodecg.Replicant('topCheers');
nodecg.listenFor('getChannels', (data, cb) => cb(channels));
socket.on('connect', () => {
nodecg.log.info('Connected to Streen');
socket.emit('authenticate', nodecg.bundleConfig.streen.secretKey, errorMsg => {
if (errorMsg) {
throw new Error(`Failed to authenticate with streen: "${errorMsg}"`);
}
});
channels.forEach(channel => {
socket.emit('join', channel, (err, alreadyJoined) => {
if (err) {
nodecg.log.error(`Failed to join ${channel}:`, err);
return;
}
if (alreadyJoined) {
nodecg.log.info('Streen already in channel:', alreadyJoined);
} else {
nodecg.log.info('Joined channel:', channel);
nodecg.sendMessage('join', channel);
self.emit('join', channel);
}
});
});
});
socket.on('disconnect', () => {
nodecg.log.warn('Disconnected from Streen');
self.emit('disconnect');
nodecg.sendMessage('disconnect');
});
socket.on('connected', () => {
nodecg.log.info('Streen connected to Twitch Chat');
});
socket.on('disconnected', () => {
nodecg.log.info('Streen disconnected from Twitch Chat');
});
socket.on('chat', data => {
if (channels.indexOf(data.channel) < 0) {
return;
}
nodecg.sendMessage('chat', data);
self.emit('chat', data);
});
socket.on('timeout', data => {
if (channels.indexOf(data.channel) < 0) {
return;
}
nodecg.sendMessage('timeout', data);
self.emit('timeout', data);
});
socket.on('clearchat', data => {
if (channels.indexOf(data.channel) < 0) {
return;
}
nodecg.sendMessage('clearchat', data.channel);
self.emit('clearchat', data.channel);
});
let lastSub;
socket.on('subscription', data => {
if (channels.indexOf(data.channel) < 0) {
return;
}
if (equal(lastSub, data)) {
return;
}
lastSub = data;
nodecg.sendMessage('subscription', data);
self.emit('subscription', data);
});
let lastCheer;
socket.on('cheer', cheer => {
if (channels.indexOf(cheer.channel) < 0) {
return;
}
// These come as a string for some reason???
// Also: very important that this conversion happens _before_ we compareTops!
cheer.userstate.bits = parseInt(cheer.userstate.bits, 10);
const newTops = compareTops(cheer, topCheers.value);
let top = null;
Object.keys(newTops).forEach(period => {
if (newTops[period] !== null) {
try {
topCheers.value[period] = newTops[period];
top = top ? top : period; // Don't touch top if it's already set
} catch (e) {
nodecg.log.error('Failed to assign top cheer for period "%s", cheer object: %o\n Error:',
period, newTops[period], e);
}
}
});
cheer.top = top;
if (equal(lastCheer, cheer)) {
return;
}
lastCheer = cheer;
nodecg.sendMessage('cheer', cheer);
self.emit('cheer', cheer);
});
socket.on('submysterygift', mysterygift => {
if (channels.indexOf(mysterygift.channel) < 0) {
return;
}
nodecg.sendMessage('submysterygift', mysterygift);
self.emit('submysterygift', mysterygift);
});
socket.on('subgift', subgift => {
if (channels.indexOf(subgift.channel) < 0) {
return;
}
nodecg.sendMessage('subgift', subgift);
self.emit('subgift', subgift);
});
socket.on('raided', raid => {
if (channels.indexOf(raid.channel) < 0) {
return;
}
nodecg.sendMessage('raided', raid);
self.emit('raided', raid);
});
let heartbeatTimeout = setTimeout(heartbeat, 5000);
let heartbeatResponseTimeout;
let lastHeartbeatInterval = 5000;
function heartbeat() {
// If we don't hear back from Streen in 1000ms, we assume Streen is down and keep trying.
heartbeatResponseTimeout = setTimeout(handleHeartbeatTimeout, 1000);
// Emit the heartbeat, and schedule the next one based on Streen's response.
socket.emit('heartbeat', channels, (err, interval) => {
if (err) {
nodecg.log.error('Heartbeat failed:', err);
}
const intervalDuration = interval || lastHeartbeatInterval;
clearTimeout(heartbeatResponseTimeout);
clearTimeout(heartbeatTimeout);
heartbeatTimeout = setTimeout(heartbeat, intervalDuration);
lastHeartbeatInterval = intervalDuration;
});
}
function handleHeartbeatTimeout() {
heartbeatTimeout = setTimeout(heartbeat, lastHeartbeatInterval);
}
self.say = function (channel, message, callback) {
socket.emit('say', channel, message, function () {
if (typeof callback === 'function') {
callback.apply(callback, arguments);
}
});
};
self.timeout = function (channel, username, seconds, callback) {
socket.emit('timeout', channel, username, seconds, function () {
if (typeof callback === 'function') {
callback.apply(callback, arguments);
}
});
};
self.mods = function (channel, callback) {
callback = callback || function () {};
socket.emit('mods', channel, function () {
if (typeof callback === 'function') {
callback.apply(callback, arguments);
}
});
};
self.resetPeriod = function (period) {
topCheers.value[period] = clone(DEFAULT_CHEER);
};
nodecg.listenFor('resetPeriod', self.resetPeriod);
function compareTops(cheer, tops) {
const ret = clone(tops);
Object.keys(tops).forEach(period => {
if (!tops[period] || !tops[period].userstate || cheer.userstate.bits > tops[period].userstate.bits) {
ret[period] = cheer;
}
});
return ret;
}
return self;
};