-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.js
92 lines (73 loc) · 2.84 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
/* jshint camelcase: false */
'use strict';
var BarryDonations = require('barry-donations');
var events = require('events');
var util = require('util');
var bd = null;
function DonCorleone(nodecg) {
if (!Object.keys(nodecg.bundleConfig).length) {
throw new Error('[lfg-doncorleone] No config found in cfg/lfg-doncorleone.json, aborting!');
}
var bdConfig = nodecg.bundleConfig;
if (!bdConfig.hostname) {
bdConfig.hostname = nodecg.config.host;
}
var self = this;
events.EventEmitter.call(this);
bd = new BarryDonations(nodecg.bundleConfig);
bd.on('connectfail', function connectfail(e) {
nodecg.log.error('Connectfail:', e.message);
});
bd.on('error', function error(e) {
nodecg.log.error('Error:', e.stack);
});
bd.on('disconnected', function disconnected(e) {
nodecg.log.error('Disconnected:', e.stack);
});
bd.on('reconnecting', function reconnecting(interval) {
nodecg.log.info('reconnecting in %d seconds', interval);
});
bd.on('initialized', function initialized(data) {
nodecg.log.info('Listening for donations to', bd.options.username);
convertNamelessTopDonorsToAnonymous(data.totals);
totals.value = data.totals;
nodecg.sendMessage('initialized', data);
self.emit('initialized', data);
});
bd.on('newdonations', function gotDonations(data) {
convertNamelessTopDonorsToAnonymous(data.totals);
totals.value = data.totals;
// If the name is blank, change it to "Anonymous"
data.Completed.forEach(function(donation) {
if (donation.twitch_username === '') {
donation.twitch_username = 'Anonymous';
}
});
nodecg.sendMessage('tip', data);
self.emit('tip', data);
});
var totals = nodecg.Replicant('totals', { defaultValue: {}, persistent: false });
nodecg.listenFor('resetCategory', function resetCategory(category) {
bd.resetCategory(category)
.then(function(cat) {
nodecg.log.info('Successfully reset:', cat);
})
.fail(function(e) {
nodecg.log.error('Failed to reset %s:', category, e.message) ;
});
});
function convertNamelessTopDonorsToAnonymous(totals) {
if (totals) {
if (totals.day_top_packet
&& totals.day_top_packet.twitch_username === '') {
totals.day_top_packet.twitch_username = 'Anonymous';
}
if (totals.month_top_packet
&& totals.month_top_packet.twitch_username === '') {
totals.month_top_packet.twitch_username = 'Anonymous';
}
}
}
}
util.inherits(DonCorleone, events.EventEmitter);
module.exports = function(extensionApi) { return new DonCorleone(extensionApi); };