-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
383 lines (349 loc) · 13.8 KB
/
app.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright (c) 2020-2022 Mitchell Adair
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
require("dotenv").config();
const tmi = require("tmi.js");
const TES = require("tesjs");
const APIServer = require("./API Server/apiserver");
const DBService = require("./dbservice");
const ChannelManager = require("./ChannelManager/channelManager");
const twitchAPI = require("./External Data APIs/twitch");
const { timedLog, getUserLevel } = require("./utils");
const TimerEmitter = require("./timerEmitter");
const DATA_TAGS = require("./DataTags/datatags");
const { getNewAppAccessToken } = require("./External Data APIs/twitch");
const { BOT_USERNAME, OAUTH_TOKEN, CLIENT_ID, CLIENT_SECRET, TES_SECRET, TES_BASE_URL } = process.env;
// ===================== HELPER FUNCTIONS =====================
// extend Array to include a 'chunk' function
// use function rather than arrow func to access 'this'
// makes shallow copy of current array, then splits the array into chunks of the given max chunk size and returns it
Array.prototype.chunk = function (maxChunkSize) {
let copy = [...this];
let chunks = [];
while (copy.length) {
const size = copy.length > maxChunkSize ? maxChunkSize : copy.length;
chunks.push(copy.splice(0, size));
}
return chunks;
};
// ===================== EVENT HANDLERS =====================
const onConnected = async (address, port) => {
timedLog(`Connected to ${address}:${port}`);
timedLog(`Joining all serviced channels...`);
try {
const channels = await DBService.getEnabledChannels();
const channelData = await twitchAPI.getBatchUsersByID(channels);
const batches = channelData.chunk(50);
for (const [i, batch] of batches.entries()) {
await new Promise((resolve) => {
setTimeout(() => {
let joinPromises = [];
batch.forEach((user) => {
joinPromises.push(client.join(user.name));
});
Promise.all(joinPromises).then(resolve);
}, i * 15000);
});
}
timedLog(`All channels joined`);
} catch (err) {
timedLog(`ERROR joining channels: ${err.message}`);
}
};
const onChat = async (channelKey, userstate, message, self) => {
if (self) return;
const channelName = channelKey.substring(1);
try {
const channel = await ChannelManager.processChannel(channelName);
channel.incrementTimers();
const full = message.trim();
if (full.startsWith("!")) {
const userLevel = getUserLevel(userstate);
const args = full.split(" ");
const alias = args.shift().substring(1);
const command = channel.getCommand(alias);
if (command && !command.isOnCooldown && userLevel >= command.user_level) {
command.isOnCooldown = true;
setTimeout(() => {
command.isOnCooldown = false;
}, command.cooldown * 1000);
let message = command.message
.replace(new RegExp("{{sender}}", "g"), userstate["display-name"])
.replace(new RegExp("{{channel}}", "g"), channelName)
.replace(
new RegExp("{{commands}}", "g"),
channel
.getCommands()
.filter((c) => c.user_level === 0)
.map((c) => `!${c.alias}`)
.join(", ")
);
for (const { tag, dataFetch } of DATA_TAGS) {
if (message.includes(tag)) {
try {
const value = await dataFetch(channelName, userstate);
message = message.replace(new RegExp(tag, "g"), value);
} catch (err) {
message = message.replace(new RegExp(tag, "g"), err.message);
}
}
}
client.say(channelKey, message);
}
}
} catch (err) {
timedLog(`ERROR on channel ${channelName}: ${err.message}`);
}
};
const onHost = async (channelKey, username, viewers, autohost) => {
if (!autohost) {
const channelName = channelKey.substring(1);
try {
const channel = await ChannelManager.processChannel(channelName);
const { enabled, message } = channel.getEvents().host;
if (enabled) {
let msg = message
.replace(new RegExp("{{user}}", "g"), username)
.replace(new RegExp("{{viewers}}", "g"), viewers);
client.say(channelKey, msg);
}
} catch (err) {
timedLog(`ERROR on channel ${channelName}: ${err.message}`);
}
}
};
const onRaid = async (channelKey, username, viewers) => {
const channelName = channelKey.substring(1);
try {
const channel = await ChannelManager.processChannel(channelName);
const { enabled, message } = channel.getEvents().raid;
if (enabled) {
let msg = message
.replace(new RegExp("{{user}}", "g"), username)
.replace(new RegExp("{{viewers}}", "g"), viewers);
client.say(channelKey, msg);
}
} catch (err) {
timedLog(`ERROR on channel ${channelName}: ${err.message}`);
}
};
const onResub = async (channelKey, username, monthStreak, _message, userstate, methods) => {
const channelName = channelKey.substring(1);
try {
const channel = await ChannelManager.processChannel(channelName);
const { enabled, message } = channel.getEvents().resub;
if (enabled) {
const months = ~~userstate["msg-param-cumulative-months"];
let msg = message
.replace(new RegExp("{{user}}", "g"), username)
.replace(new RegExp("{{months}}", "g"), months)
.replace(new RegExp("{{streak}}", "g"), monthStreak)
.replace(new RegExp("{{type}}", "g"), methods.planName);
client.say(channelKey, msg);
}
} catch (err) {
timedLog(`ERROR on channel ${channelName}: ${err.message}`);
}
};
const onSubGift = async (channelKey, username, monthStreak, recipient, methods, userstate) => {
const channelName = channelKey.substring(1);
try {
const channel = await ChannelManager.processChannel(channelName);
const { enabled, message } = channel.getEvents().subgift;
if (enabled) {
const total = ~~userstate["msg-param-sender-count"];
let msg = message
.replace(new RegExp("{{user}}", "g"), username)
.replace(new RegExp("{{total}}", "g"), total)
.replace(new RegExp("{{streak}}", "g"), monthStreak)
.replace(new RegExp("{{recipient}}", "g"), recipient)
.replace(new RegExp("{{type}}", "g"), methods.planName);
client.say(channelKey, msg);
}
} catch (err) {
timedLog(`ERROR on channel ${channelName}: ${err.message}`);
}
};
const onSubMysteryGift = async (channelKey, username, numbOfSubs, methods, userstate) => {
const channelName = channelKey.substring(1);
try {
const channel = await ChannelManager.processChannel(channelName);
const { enabled, message } = channel.getEvents().mysterygift;
if (enabled) {
const total = ~~userstate["msg-param-sender-count"];
let msg = message
.replace(new RegExp("{{user}}", "g"), username)
.replace(new RegExp("{{total}}", "g"), total)
.replace(new RegExp("{{count}}", "g"), numbOfSubs)
.replace(new RegExp("{{type}}", "g"), methods.planName);
client.say(channelKey, msg);
}
} catch (err) {
timedLog(`ERROR on channel ${channelName}: ${err.message}`);
}
};
const onSub = async (channelKey, username, methods, _message, _userstate) => {
const channelName = channelKey.substring(1);
try {
const channel = await ChannelManager.processChannel(channelName);
const { enabled, message } = channel.getEvents().sub;
if (enabled) {
let msg = message
.replace(new RegExp("{{user}}", "g"), username)
.replace(new RegExp("{{type}}", "g"), methods.planName);
client.say(channelKey, msg);
}
} catch (err) {
timedLog(`ERROR on channel ${channelName}: ${err.message}`);
}
};
const onAnonGiftUpgrade = async (channelKey, username, _userstate) => {
const channelName = channelKey.substring(1);
try {
const channel = await ChannelManager.processChannel(channelName);
const { enabled, message } = channel.getEvents().anongiftupgrade;
if (enabled) {
let msg = message.replace(new RegExp("{{user}}", "g"), username);
client.say(channelKey, msg);
}
} catch (err) {
timedLog(`ERROR on channel ${channelName}: ${err.message}`);
}
};
const onGiftUpgrade = async (channelKey, username, sender, _userstate) => {
const channelName = channelKey.substring(1);
try {
const channel = await ChannelManager.processChannel(channelName);
const { enabled, message } = channel.getEvents().giftupgrade;
if (enabled) {
let msg = message
.replace(new RegExp("{{user}}", "g"), username)
.replace(new RegExp("{{gifter}}", "g"), sender);
client.say(channelKey, msg);
}
} catch (err) {
timedLog(`ERROR on channel ${channelName}: ${err.message}`);
}
};
const onCheer = async (channelKey, userstate, _message) => {
const channelName = channelKey.substring(1);
try {
const channel = await ChannelManager.processChannel(channelName);
const { enabled, message } = channel.getEvents().cheer;
if (enabled) {
let msg = message
.replace(new RegExp("{{user}}", "g"), username)
.replace(new RegExp("{{amount}}", "g"), userstate.bits);
client.say(channelKey, msg);
}
} catch (err) {
timedLog(`ERROR on channel ${channelName}: ${err.message}`);
}
};
const onFollow = async ({ broadcaster_user_login, user_name }) => {
try {
const channel = await ChannelManager.processChannel(broadcaster_user_login);
const { enabled, message } = channel.getEvents().follow;
if (enabled) {
let msg = message.replace(new RegExp("{{user}}", "g"), user_name);
client.say(`#${broadcaster_user_login}`, msg);
}
} catch (err) {
timedLog(`ERROR on channel ${broadcaster_user_login}: ${err.message}`);
}
};
// ===================== INIT CHAT BOT =====================
const tmiConfig = {
identity: {
username: BOT_USERNAME,
password: OAUTH_TOKEN,
},
channels: ["MtheB_"],
connection: {
reconnect: true,
secure: true,
},
};
const client = new tmi.client(tmiConfig);
// EVENT HANDLER REGISTRATION
client.on("connected", onConnected);
client.on("chat", onChat);
client.on("hosted", onHost);
client.on("raided", onRaid);
client.on("resub", onResub);
client.on("subgift", onSubGift);
client.on("submysterygift", onSubMysteryGift);
client.on("subscription", onSub);
client.on("paidgiftupgrade", onGiftUpgrade);
client.on("anonpaidgiftupgrade", onAnonGiftUpgrade);
client.on("cheer", onCheer);
client.connect();
new TimerEmitter(client);
// ===================== INIT API SERVER =====================
const actions = {
refreshChannelData: async (channelID) => {
try {
const channel = await DBService.getChannel(channelID);
if (channel && ChannelManager.getChannel(channel.name)) {
const { name } = channel;
timedLog(`Refreshing data for channel ${name}...`);
ChannelManager.deleteChannel(name);
await ChannelManager.fetchChannelData(name);
timedLog(`Refreshed channel ${name}`);
}
} catch (err) {
timedLog(`ERROR refreshing channel ${channelID}: ${err.message}`);
}
},
joinChannel: async (channelID) => {
try {
const { login } = await twitchAPI.getUser(channelID);
if (login) {
await client.join(login);
}
} catch (err) {
const { message } = err;
timedLog(`ERROR joining channel ${channelID}: ${message || err}`);
throw err;
}
},
leaveChannel: async (channelID) => {
try {
const { login } = await twitchAPI.getUser(channelID);
if (login) {
await client.part(login);
}
} catch (err) {
const { message } = err;
timedLog(`ERROR leaving channel ${channelID}: ${message || err}`);
throw err;
}
},
subscribeFollow: (channelID) => {
const condition = { broadcaster_user_id: channelID, moderator_user_id: channelID };
return tes.subscribe("channel.follow", condition, "2");
},
unsubscribeFollow: (channel) => {
const condition = { broadcaster_user_id: channel, moderator_user_id: channelID };
return tes.unsubscribe("channel.follow", condition);
},
};
const server = APIServer(actions);
// ===================== INIT TES =====================
const tesConfig = {
identity: {
id: CLIENT_ID,
secret: CLIENT_SECRET,
onAuthenticationFailure: getNewAppAccessToken,
},
listener: {
type: "webhook",
baseURL: TES_BASE_URL,
secret: TES_SECRET,
server: server,
},
options: { debug: true },
};
const tes = new TES(tesConfig);
tes.on("channel.follow", onFollow);