forked from qmx/aerobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
86 lines (80 loc) · 3.61 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
var irc = require('irc');
var Bot = require('./lib/bot').Bot;
var util = require('./lib/util');
var redis = require('redis');
var _ = require('lodash');
if (process.env.REDIS_URL) {
var redisURL = require('url').parse(process.env.REDIS_URL);
var client = redis.createClient(redisURL.port, redisURL.hostname);
client.auth(redisURL.auth.split(":")[1]);
} else {
var client = redis.createClient();
}
if (process.env.AEROBOT_CONFIG) {
var config = JSON.parse(process.env.AEROBOT_CONFIG);
} else {
var config = {
irc:{
host:'irc.freenode.net',
nick:'aerobot33',
channels: ['#aerobot-test']
}
};
}
var ircConnection = new irc.Client(config.irc.host, config.irc.nick, {
channels: config.irc.channels
});
var bot = new Bot(config.irc.nick);
ircConnection.addListener('message', function (from, to, message) {
if(bot.isStatusMessage(message)) {
var key = "aerobot:status:" + config.irc.host + ":" + util.normalizeChannelName(to) + ":" + from;
client.hset(key, new Date().toISOString(), bot.normalizeMessage(message), function (err, reply){
ircConnection.say(to, from + ': recorded your slacking at <dashboard link coming soon>');
});
} else if(bot.isFactoidStoreRequest(message)) {
var key = "aerobot:factoid:" + config.irc.host + ":" + util.normalizeChannelName(to);
var request = bot.parseFactoidStoreRequest(message);
client.hset(key, request.key, request.value, function (err, reply) {
ircConnection.say(to, from + ': kk');
});
} else if(bot.isFactoidRemovalRequest(message)) {
var key = "aerobot:factoid:" + config.irc.host + ":" + util.normalizeChannelName(to);
var request = bot.parseFactoidRemovalRequest(message);
client.hdel(key, request, function (err, reply) {
ircConnection.say(to, from + ': never heard of it!');
});
} else if(bot.isFactoidRetrievalRequest(message)) {
var key = "aerobot:factoid:" + config.irc.host + ":" + util.normalizeChannelName(to);
var request = bot.parseFactoidRetrievalRequest(message);
client.hget(key, request, function (err, reply) {
if(reply) {
ircConnection.say(to, from + ': ' + reply);
} else {
ircConnection.say(to, from + ': wat?');
}
});
} else if(bot.isKarmaRequest(message)) {
var key = "aerobot:karma:" + config.irc.host + ":" + util.normalizeChannelName(to);
var request = bot.parseKarmaRequest(message);
client.zincrby(key, request.direction, request.user, function (err, reply) {
var actionText = request.direction === 1 ? 'gained' : 'lost';
ircConnection.say(to, request.user + ' ' + actionText + ' a level! (Karma: ' + reply + ')');
});
} else if (bot.isKarmaBestRequest(message)) {
var key = "aerobot:karma:" + config.irc.host + ":" + util.normalizeChannelName(to);
client.zrevrange(key, 0, 4, 'WITHSCORES', function(err, reply) {
if (!err) {
var scores = util.normalizeKarmaScores(reply);
ircConnection.say(to, util.prettyPrintKarmaScores(scores));
}
});
} else if (bot.isKarmaWorstRequest(message)) {
var key = "aerobot:karma:" + config.irc.host + ":" + util.normalizeChannelName(to);
client.zrange(key, 0, 4, 'WITHSCORES', function(err, reply) {
if (!err) {
var scores = util.normalizeKarmaScores(reply);
ircConnection.say(to, util.prettyPrintKarmaScores(scores));
}
});
}
});