-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshoutouts.js
63 lines (52 loc) · 2.2 KB
/
shoutouts.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
const MiniDb = require('./minidb');
const moment = require('moment');
class ShoutOuts {
static init() {
console.log(`[Shoutouts]`, `Shoutout class instantiated.`);
this._userDb = new MiniDb("twitch-users");
this._watchData = this._userDb.get("watch-list") || { };
this._shoutouts = this._userDb.get("shout-outs") || { };
}
static inList(username) {
let watchedUsers = this._watchData['usernames'] || [ ];
return (watchedUsers.indexOf(username) >= 0);
}
// See if a user was already shouted out this stream
// by using the stream's start time as the discriminator
// then update the object
static shouldShoutOut(username, started_at) {
username = username.trim().toLowerCase();
console.log('[ShoutOuts]', `Checking shout out timing for @${username}`);
// Don't shout out people not on the list
if (!this.inList(username)) return false;
let now = moment();
let startedAt = moment(started_at);
let shouts = this._shoutouts;
let shouldShout = false;
// Has it been the right amount of time since
// stream has started
let waitTime = process.env.MIN_TO_WAIT_BEFORE_SHOUTOUT;
let streamUptime = moment().diff(started_at, 'minutes');
let rightTime = (streamUptime > waitTime) || false;
console.log('[ShoutOuts]', `Stream uptime: ${streamUptime}; Wait time: ${waitTime}`);
if(rightTime) {
// Check last shout out vs this stream's start time
// Either add them to the list or update their record
let existingHistory = shouts[username] || null;
if (existingHistory) { //has had a shoutout before.
// Check against the last start time
let lastShout = moment(existingHistory);
shouldShout = !lastShout.isSame(startedAt);
shouts[username] = startedAt;
} else { //has NOT had a shoutout before.
// Add them to the list
shouts[username] = startedAt;
shouldShout = true;
}
this._userDb.put('shout-outs', shouts);
}
console.log('[ShoutOuts]', `Should @${username} get a shout? ${shouldShout}`);
return shouldShout;
}
}
module.exports = ShoutOuts;