/
bot.js
39 lines (34 loc) · 889 Bytes
/
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
var irc = require("irc")
, server = "irc.freenode.net"
, channel = "#ifit"
;
module.exports = bot;
function bot(name, onMessage) {
// Create the bot name
var bot = new irc.Client(server, 'bot-' + name, {
channels: [channel]
});
function say(msg) {
if (msg) {
console.log(name + ' says: ' + msg);
bot.say(channel, '-- ' + msg);
}
}
if (onMessage.length === 1) {
// Listen for a message
bot.addListener("message", function(from, to, text, message) {
if (from.match(/^bot-/)) return;
var response = onMessage(text);
say(response)
});
} else {
// Listen for a message
bot.addListener("message", function(from, to, text, message) {
if (from.match(/^bot-/)) return;
onMessage(text, function(err, response) {
if (err) return say(err.message);
say(response)
});
});
}
}