-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.js
95 lines (73 loc) · 2.28 KB
/
client.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
/*
This singlettoni (singleton written in sphagetti code) establishes the connection to the irc server and allows
channels to register themselves to receive events.
*/
"use strict";
var irc = require('irc');
var log = require('./log.js');
var channel = require('./lib/channel.js');
var config = require('./config.js').irc;
//We setup the options object and import the oauth token.
var token = require('./secrets.js').twitchToken;
var options = {
'userName': config.userName,
'realName': config.userName,
'password': token,
};
var client = module.exports.client = new irc.Client("irc.twitch.tv", config.userName, options);
//We store Channel objects that we pass messages to
var channels = {};
//This will get thrown a bunch of channel models that we should enter
//and then start throwing messages at.
module.exports.joinChannels = function(channels){
channels.forEach(joinChannel);
}
var joinChannel = module.exports.joinChannel = function(channel){
if(channels[channel.hashtag]){
return;
}
log.info('joining', channel.hashtag);
channels[channel.hashtag] = channel;
client.join(channel.hashtag);
}
client.on('disconnected', function(){
log.info("DISCONNECTED", arguments);
});
client.on('connect', function(){
//this is a dumb hack, but it keeps us working without having
//to rewrite the moderator code.
client.conn.write("CAP REQ :twitch.tv/membership\r\n");
});
//We add a bunch of listeners to the IRC client that forward the events ot the appropriate Channel objects.
client.on('message', function(user, channel, message){
var channel = channels[channel];
channel && channel.onMessage(user, message);
});
//We use this to parse op status updates
function parseMode(channel, by, mode, argument, message){
//What we need is an obscure part in the message object :(
var args = message.args;
var user = args[2];
var channel = channels[args[0]];
//we do not care about anything other than O (giggity)
if(mode != 'o'){
return;
}
if(!channel){
return;
}
if(args[1] == '+o'){
channel.onUserModded(user);
}
else if(args[1] == '-o'){
channel.onUserUnmodded(user);
}
}
client.on('+mode', parseMode);
client.on('-mode', parseMode);
client.on('ping', function(){
log.info("Got PING from twitch");
});
client.on('netError', function(e){
log.error('IRC lib netError:', e);
});