-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.js
64 lines (56 loc) · 1.81 KB
/
irc.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
var irc = require('tmi.js');
var _settings = require('./settings.js');
var _config = require('./ircConfig.js');
var clientOptions = {
options: {
debug: _settings.ircDebug
},
identity: {
username: _config.username,
password: _config.password
}
};
var client = new irc.client(clientOptions);
client.connect();
var sendDelayedMessage = function(channel, message, delay) {
var delayedMessage = new Promise(function(resolve, reject) {
setTimeout(function() {
client.say(channel, message)
.then(function() {
resolve();
})
.catch(function() {
reject();
});
}, delay);
});
return delayedMessage;
};
var makeAnnouncements = module.exports.makeAnnouncements = function(announcements) {
// NOTE: for the moment this makes all IRC announcements sequentially
// Ideally this would be changed to be simultaneous, but due to spam constraints this is impractical
var announcement = announcements.splice(0,1)[0];
var channel = '#'+announcement.channelName;
return client.join(channel)
.then(function() {
return sendDelayedMessage(channel, "Notice: this channel has "+announcement.falseViewers+" fake viewers ("+announcement.percentFalse+"% of total viewers)", _settings.ircChannelTimeout);
})
.then(function() {
return sendDelayedMessage(channel, "False positive? Want to learn more? GitHub link is in profile.", _settings.ircMessageTimeout);
})
.then(function() {
return client.part(channel);
})
.then(function() {
// Recursively re-sends remaining announcements until none remaining
if (announcements.length > 0) {
return makeAnnouncements(announcements);
}
})
.catch(function() {
console.log('Message sending failed, continuing...');
});
};
module.exports.disconnect = function() {
return client.disconnect();
};