This repository was archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
769 lines (664 loc) · 20.9 KB
/
app.js
File metadata and controls
769 lines (664 loc) · 20.9 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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
'use strict';
const config = require('./config');
const h = require('./helpers');
const pkg = require('./package');
const settings = config.settings;
const { Datastore } = require('@google-cloud/datastore');
const express = require('express');
const fs = require('fs');
const request = require('request');
const swig = require('swig');
const tmi = require('tmi.js');
const datastore = new Datastore(config.gcloud);
const client = new tmi.client(config.tmi);
const web = express();
const twitchSettings = config.settings.twitch || {};
const baseApi = request.defaults({
baseUrl: 'https://twitch-api-proxy.cactus.workers.dev',
method: 'GET',
json: true,
});
/**
* Channels the bot has joined (and will join on start).
*
* @type {Array}
*/
let channels = [];
/**
* Cache to map usernames to user IDs and vice versa.
*
* @type {Object}
*/
let cache = {
ids: {},
names: {},
};
/**
* Get user data based on username.
*
* @param {String} username
* @param {Function} callback
* @return {Void}
*/
const getUser = (username, callback) => {
if (typeof callback !== 'function') {
callback = () => {};
}
if (cache.names[username]) {
callback(cache.names[username]);
return;
}
baseApi({
'url': '/direct/users?login=' + username,
}, (err, response, body) => {
if (err) {
callback(false);
return console.error(err);
}
if (!body || !body.data || body.data.length === 0) {
callback(false);
return console.error(`User ${username} does not exist!`);
}
const user = body.data[0];
const {id, display_name, login} = user;
const name = login;
const _id = id;
const userToCache = {
display_name,
_id,
name,
};
cache.ids[_id] = userToCache;
cache.names[name] = userToCache;
console.log(`Loaded ${name} (${_id}) into cache.`);
callback(userToCache);
});
};
/**
* Which users to ignore in channels by the bot.
*
* @type {Object}
*/
let ignore = {};
/**
* Reads a file and parses it as JSON.
*
* @return {Mixed}
*/
const readJson = (filename) => {
try {
return JSON.parse(fs.readFileSync(filename, 'utf-8'));
} catch (error) {
console.log(error);
return false;
}
};
/**
* JSON-encodes the input and writes it to the specified file path.
*
* @param {String} filename
* @param {Object|Array} input
* @return {Void}
*/
const saveJson = (filename, input) => {
fs.writeFile(filename, JSON.stringify(input, null, 4), (error) => {
if (error) {
console.log(error);
}
});
};
/**
* Loads channels from file
*
* @return {Void}
*/
const loadChannels = () => {
let read = readJson(settings.channels || __dirname + '/channels.json');
if (read === false) {
return;
}
channels = read;
};
// Load channels on startup
loadChannels();
// Cache channel data
channels.forEach((chan) => {
getUser(chan);
});
/**
* Saves channels to file.
*
* @return {Void}
*/
const saveChannels = () => {
saveJson(settings.channels || __dirname + '/channels.json', channels);
};
/**
* Loads the ignore list from file.
*
* @return {Void}
*/
const loadIgnore = () => {
let read = readJson(settings.ignore || __dirname + '/ignore.json');
if (read === false) {
return;
}
ignore = read;
};
// Load ignore list on startup.
loadIgnore();
/**
* Saves the ignore list to file.
*
* @return {Void}
*/
const saveIgnore = () => {
saveJson(settings.ignore || __dirname + '/ignore.json', ignore);
};
/**
* Admin commands controlled by whispers.
*
* @type {Object}
*/
const cmds = {};
/**
* Adds a user to the ignore list for the specified channel, then saves the ignore list.
*/
cmds['ignore'] = (username, user, input) => {
if (!input[0] || !input[1]) {
client.whisper(username, 'Both channel and username has to be specified');
return;
}
let channel = h.fmtChannel(input[0]);
let name = h.fmtChannel(input[1]);
if (!ignore[channel]) {
ignore[channel] = [];
}
let list = ignore[channel];
if (list.indexOf(name) >= 0) {
client.whisper(username, `${name} is already ignored in ${channel}`);
return;
}
list.push(name);
saveIgnore();
client.whisper(username, `${name} has been added to the ignore list for ${channel}`);
};
/**
* Attempts to join the channel if it hasn't already been joined, then saves the channel list.
*/
cmds['join'] = (username, user, input) => {
if (input[0]) {
let channel = h.fmtChannel(input[0]);
if (channels.indexOf(channel) >= 0) {
client.whisper(username, `${channel} is already joined.`);
} else {
getUser(channel, (user) => {
if (!user) {
client.whisper(username, `Error occurred getting data on ${channel}.`);
return;
}
client.join(channel).then(() => {
channels.push(channel);
saveChannels();
client.whisper(username, `Successfully joined ${channel} and added the channel to the list.`);
}).catch((err) => {
client.whisper(username, `An error occurred joining ${channel}: ${err}`);
});
});
}
} else {
client.whisper(username, 'A channel name has to be specified.');
}
};
/**
* Leaves the specified channel and removes it from the channel list.
*/
cmds['leave'] = (username, user, input) => {
if (input[0]) {
let channel = h.fmtChannel(input[0]);
let index = channels.indexOf(channel);
if (index >= 0) {
client.part(channel).then(() => {
channels.splice(index, 1);
saveChannels();
client.whisper(username, `Successfully left ${channel} and removed the channel from the list.`);
}).catch((err) => {
client.whisper(username, `An error occurred leaving ${channel}: ${err}`);
});
} else {
client.whisper(username, `${channel} has not been added to the list yet.`);
}
} else {
client.whisper(username, 'A channel name has to be specified.');
}
};
/**
* Alias of cmds.leave().
*/
cmds['part'] = cmds['leave'];
/**
* Ping pong.
*/
cmds['ping'] = (username) => {
client.whisper(username, 'PONG');
};
/**
* Removes a user from the ignore list for the specified channel, then saves the ignore list.
*/
cmds['unignore'] = (username, user, input) => {
if (!input[0] || !input[1]) {
client.whisper(username, 'Both channel and username has to be specified');
return;
}
let channel = h.fmtChannel(input[0]);
let name = h.fmtChannel(input[1]);
if (!ignore[channel]) {
ignore[channel] = [];
}
let list = ignore[channel];
let index = list.indexOf(name);
if (index === -1) {
client.whisper(username, `${name} isn't ignored in ${channel}`);
return;
}
list.splice(index, 1);
saveIgnore();
client.whisper(username, `${name} has been removed from the ignore list for ${channel}`);
};
/**
* Returns the app version.
*/
cmds['version'] = (username) => {
client.whisper(username, pkg.version);
};
/**
* Handles chat messages
*/
const handleMessage = (channel, user, message) => {
channel = h.fmtChannel(channel);
/**
* Ignore legacy "twitchnotify" subscriber messages.
*
* These should no longer show up anyways.
*/
if (user.username === 'twitchnotify') {
return;
}
/**
* Ignore users in the ignore list.
*/
if (ignore[channel] && ignore[channel].indexOf(user.username) >= 0) {
return;
}
/**
* Formats data so we don't log _all_ the junk included in the userstate.
*
* @type {Object}
*/
const data = {
channel: channel,
channel_id: user['room-id'],
username: user.username,
user_id: user['user-id'],
user: {
display_name: (user['display-name'] || user.username),
type: user['user-type'],
color: user.color,
badges: user.badges,
subscriber: user.subscriber,
turbo: user.turbo,
},
message: message,
timestamp: user['tmi-sent-ts'],
};
const key = datastore.key([settings.kind, user.id]);
datastore
.save({
key: key,
data: data,
excludeFromIndexes: ['message'],
})
.catch(console.error);
};
/**
* Defines subscription plans/tiers.
*/
const subTiers = {
'1000': 'Tier 1',
'2000': 'Tier 2',
'3000': 'Tier 3',
};
/**
* Handles the different subscription events
*/
const handleSubs = (channel, username, m, msg, state, methods) => {
channel = h.fmtChannel(channel);
if (ignore[channel] && ignore[channel].indexOf(username) >= 0) {
return;
}
const ts = Date.now().toString();
getUser(username, (cachedUser) => {
let user_id = null;
if (cachedUser) {
user_id = cachedUser._id;
}
/**
* 'resub' event: This will be the amount of months the user has been subbed for
* https://github.com/tmijs/docs/blob/gh-pages/_posts/v1.4.2/2019-03-03-Events.md#resub
*
* 'subscription' event: This will be an object with the plan/method information
* https://github.com/tmijs/docs/blob/gh-pages/_posts/v1.4.2/2019-03-03-Events.md#subscription
*/
const isNumber = typeof m === 'number';
/**
* Plan information will be part of `m` if it's a new subscription.
*
* Resubs pass another parameter `methods`,
* which is normally undefined for new subscriptions.
*/
const {plan, planName} = (isNumber ? methods : m);
/**
* Tier 1/2/3 are predefined. If this changes or new tiers show up
* it should just fall back to whatever Twitch sends via TMI.
*/
const tier = subTiers[plan] || plan;
const months = state['msg-param-cumulative-months'] || (isNumber ? m : 0);
const prefix = isNumber ?
`Resub (${months} months) - Plan: [${tier}] ${planName}` :
`Subscription - Plan: [${tier}] ${planName}`;
/**
* Check if a resubscription message is included.
*/
const hasMessage = typeof msg === 'string';
const data = {
channel: channel,
channel_id: cache.names[channel]._id,
username: username,
user_id: user_id,
user: {
display_name: username,
},
message: prefix + (hasMessage ? `- Message: ${msg}` : ''),
timestamp: ts,
};
const key = datastore.key([settings.kind, username + '_' + ts]);
datastore
.save({
key: key,
data: data,
})
.catch(console.error);
});
};
/**
* Handles timeouts and bans in a channel.
*
* @param {String} channel Channel name where the ban/timeout occurred.
* @param {String} username Username of user that got banned/timed out.
* @param {null} reason Historically this contained the ban/timeout reason, but Twitch no longer sends this via TMI.
* @param {Number|Object|undefined} length Length of time someone got timed out (in seconds).
*/
const handleTimeoutAndBan = (channel, username, timeout, length) => {
channel = h.fmtChannel(channel);
const isBan = length === undefined || typeof length === 'object';
const type = isBan ? 'BAN' : 'TIMEOUT';
const suffix = isBan ? '' : ` - Length (seconds): ${length}`;
const ts = Date.now().toString();
getUser(username, (cachedUser) => {
let userId = null;
if (cachedUser) {
userId = cachedUser._id;
}
const data = {
channel: channel,
channel_id: cache.names[channel]._id,
username: username,
user_id: userId,
user: {
display_name: username,
},
message: `* ${type + suffix}`,
timestamp: ts,
};
const key = datastore.key([settings.kind, `${channel}_${username}_${ts}`]);
datastore
.save({
key,
data,
})
.catch(console.error);
});
};
/**
* Register all chat events with prepared handlers.
*/
client.on('action', handleMessage);
client.on('ban', handleTimeoutAndBan);
client.on('chat', handleMessage);
client.on('cheer', handleMessage);
client.on('resub', handleSubs);
client.on('subscription', handleSubs);
client.on('timeout', handleTimeoutAndBan);
client.on('connected', () => {
console.log(`[${h.now()}] Successfully connected.`);
settings.autoconnect = settings.autoconnect || {};
if (channels.length > 0 && settings.autoconnect.enabled !== false) {
// concat channels array to get a new array instance.
let temp = channels.concat();
let queue = setInterval(() => {
const chan = temp[0];
client.join(chan);
temp.shift();
if (temp.length === 0) {
clearInterval(queue);
}
}, settings.autoconnect.delay || 1000);
}
});
client.on('join', (channel, user, self) => {
if (!self) {
return;
}
console.log(`[${h.now()}] ${user} joined ${channel}`);
});
client.on('whisper', (username, user, message) => {
username = h.fmtChannel(username);
/**
* Handles admin commands through whispers
*/
if (settings.admins.indexOf(username) >= 0 && message.startsWith(settings.prefix)) {
let input = message.split(' ');
// remove prefix from command name
let command = input[0].slice(settings.prefix.length).toLowerCase();
if (cmds[command]) {
// remove the command name from input, because the methods don't need it
input.shift();
cmds[command](username, user, input);
}
}
});
if (settings.express.enabled) {
web.engine('html', swig.renderFile);
web.set('view engine', 'html');
web.set('views', __dirname + '/views');
web.get('/', (req, res) => {
res.render('home');
});
web.get('/api/channels', (req, res) => {
res.send({
success: true,
channels: channels,
});
});
/**
* Blacklisted user agents.
*/
const blacklistedUserAgents = config.settings.userAgentBlacklist || [];
web.get('/api/messages', (req, res) => {
let channel = (h.requestValue(req, 'channel') || '');
let user = (h.requestValue(req, 'user') || '');
user = user.trim();
channel = channel.trim();
let limit = (parseInt(h.requestValue(req, 'limit')) || 25);
let offset = (parseInt(h.requestValue(req, 'offset')) || 0);
let plain = (typeof h.requestValue(req, 'plain') !== 'undefined' ? true : false);
let useUserId = (typeof h.requestValue(req, 'userid') !== 'undefined' ? true : false);
let max = (settings.queryLimit || 200);
if (limit > max) {
limit = max;
}
/**
* Allow blacklisting of user agents, such as "Discordbot", to prevent their embeds
* requesting messages.
*/
const userAgent = req.get('User-Agent');
for (let i in blacklistedUserAgents) {
const blacklisted = blacklistedUserAgents[i];
if (userAgent.includes(blacklisted)) {
console.log(`Blacklisted user agent: ${userAgent} matching ${blacklisted}`);
res
.status(403)
.send({
success: false,
message: 'Your user agent has been blacklisted.',
});
return;
}
}
if ((!channel || channel.length === 0) && (!user || user.length === 0)) {
let message = 'No channel or user specified';
if (plain) {
h.response(res, message);
return;
}
h.response(res, {
success: false,
error: message,
});
return;
}
let query = datastore.createQuery(settings.kind);
if (channel && channel.length > 0) {
query = query.filter('channel_id', '=', cache.names[channel]._id)
.order('channel_id');
}
if (user && user.length > 0) {
user = user.toLowerCase();
}
const userGet = (user && user.length > 0) ? user : channel;
const handleUser = (cachedUser) => {
if (!useUserId) {
if (user && user.length > 0) {
/**
* User does not "exist", but may just be sitewide suspended/deleted.
* Fallback to search by username.
*
* Eventually I wanna get the user_id from one of the resulting
* messages in the query and use that to query more messages by
* the user, but as of right now this is just a quickfix.
*
* This does not account for channels that are banned, which is
* something else I'll have to "fix" at a later date.
* For now: Don't get suspended.
*/
if (cachedUser) {
query = query.filter('user_id', '=', cachedUser._id);
} else {
query = query.filter('username', '=', user);
}
}
} else {
query = query.filter('user_id', '=', cachedUser);
}
query = query
.offset(offset)
.limit(limit);
query = query.order('timestamp', {
descending: true,
});
datastore
.runQuery(query)
.then((messages) => {
messages = messages[0];
if (plain) {
if (messages.length > 0) {
let result = '';
for (let index in messages) {
let msg = messages[index];
result += `[#${msg.channel}][${msg.user.display_name}][${h.formatDate(msg.timestamp)}] - ${msg.message}\r\n`;
}
h.response(res, result);
} else {
h.response(res, 'No messages found.');
}
return;
}
h.response(res, {
success: true,
count: messages.length,
messages: messages,
});
})
.catch((err) => {
const message = 'Unable to retrieve messages for this user/channel.';
console.error(err);
res.status(404);
if (plain) {
h.response(res, message);
return;
}
h.response(res, {
success: false,
error: message,
});
});
};
if (useUserId) {
handleUser(user);
} else {
getUser(userGet, handleUser);
}
});
web.get('/api/status', (req, res) => {
const state = client.readyState();
// Set status code based on readyState status.
const status = (state === 'OPEN' || state === 'CONNECTING') ? 200 : 500;
res
.status(status)
.send({
success: status === 200,
state,
});
});
web.get('/api/*', function(req, res) {
res.send({
success: false,
error: '404 not found',
});
});
/**
* Basic error handling.
*/
web.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
if (err.status === 404) {
res.send('Page not found.');
}
res.send('Internal server error');
});
let webport = settings.express.port || 8000;
web.listen(webport, () => {
console.log(`Web interface listening on port ${webport}`);
});
}
client.connect();
process.on('SIGINT', () => {
client.disconnect()
.then(() => {
console.log('Successfully disconnected from Twitch chat server.');
process.exit(0);
})
.catch((err) => {
console.error(`Error occurred disconnecting from Twitch chat server: ${err}`);
process.exit(1);
});
});