-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.js
119 lines (96 loc) · 2.94 KB
/
bot.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var EventEmitter = require('events').EventEmitter,
irc = require('irc'),
common = require('./common'),
async = common.async,
await = common.await,
Log = common.Log,
sprintf = common.sprintf,
Promise = common.Promise;
function Bot(username, oauthToken) {
var emitter = new EventEmitter();
emitter.setMaxListeners(0);
var client = new irc.Client('irc.twitch.tv', username, {
port: 6667,
showErrors: true,
password: 'oauth:' + oauthToken.replace(/^oauth:/i, ''),
userName: username,
realName: username,
autoConnect: false,
showErrors: true,
stripColors: true,
secure: false
});
client.on('error', function(message) {
Log.error(sprintf('IRC error: %s', message));
});
Log.info('Connecting to Twitch IRC servers.');
var connect = new Promise(function(resolve, reject) {
client.connect(5, async(function() {
Log.info('Connected to Twitch IRC servers.');
resolve();
}));
});
client.on('message#', async(function(user, channel, message) {
user = user.trim().toLowerCase();
if (user == 'jtv') {
return;
}
channel = channel.substr(1).trim().toLowerCase();
emitter.emit('message', channel, user, message, say(channel), unsafeSay(channel));
}));
client.on('join', async(function(channel, user) {
user = user.trim().toLowerCase();
if (user == 'jtv') {
return;
}
channel = channel.substr(1).trim().toLowerCase();
emitter.emit('join', channel, user, say(channel), unsafeSay(channel));
}));
client.on('part', async(function(channel, user) {
user = user.trim().toLowerCase();
if (user == 'jtv') {
return;
}
channel = channel.substr(1).trim().toLowerCase();
emitter.emit('part', channel, user, say(channel), unsafeSay(channel));
}));
this.join = function(channel) {
connect.then(function() {
Log.info(sprintf('Joining #%s.', channel));
client.join(sprintf('#%s', channel), function() {
Log.info(sprintf('Joined #%s.', channel));
emitter.emit('channel', channel, say(channel), unsafeSay(channel));
});
});
return this;
};
var say = function(channel) {
return function(format) {
if (arguments.length === 1) {
// No sprintf formatting, escape the escape character.
format = format.replace(/%/g, '%%');
}
var message = sprintf.apply(null, arguments).trim();
if (message[0] == '.' || message[0] == '/' || message[0] == '!') {
return;
}
client.say(sprintf('#%s', channel), message);
};
};
var unsafeSay = function(channel) {
return function() {
client.say(sprintf('#%s', channel), sprintf.apply(null, arguments));
};
};
this.plugin = function(plugin) {
if (plugin instanceof Array) {
for (var i = 0; i < plugin.length; ++i) {
this.plugin(plugin[i]);
}
} else {
plugin.load(emitter);
}
return this;
};
};
module.exports = Bot;