-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.js
227 lines (227 loc) · 8.06 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
(function() {
var bash, client, desc, descriptions, dispatch, error, fortune, github, handlers, hear, image, imdb, irc, irc_channels, irc_name, irc_server, isup, listen, say, seen, success, sys, time, weather;
irc = require("irc");
sys = require("sys");
bash = require("./bash");
fortune = require("./fortune");
github = require("./github");
image = require("./image");
imdb = require("./imdb");
isup = require("./isitup");
seen = require("./seen");
time = require("./time");
weather = require("./weather");
irc_server = process.env.IRC_SERVER;
irc_name = process.env.IRC_NAME;
irc_channels = process.env.IRC_CHANNELS.split(";");
handlers = [];
descriptions = [];
client = null;
dispatch = function(message) {
var handler, match, pair, pattern, _i, _len, _results;
_results = [];
for (_i = 0, _len = handlers.length; _i < _len; _i++) {
pair = handlers[_i];
pattern = pair[0], handler = pair[1];
_results.push(message.from !== irc_name && (match = message.message.match(pattern)) ? (message.match = match, handler(message)) : void 0);
}
return _results;
};
hear = function(pattern, callback) {
return handlers.push([pattern, callback]);
};
desc = function(phrase, functionality) {
return descriptions[phrase] = functionality;
};
say = function(channel, message) {
return client.say(channel, message);
};
success = function(message) {
return console.log("\033[01;32m" + message + "\033[0m");
};
error = function(message) {
return console.log("\033[01;31m" + message + "\033[0m");
};
listen = function() {
var opts;
opts = {
autoRejoin: true,
channels: irc_channels,
realName: irc_name,
userName: irc_name
};
client = new irc.Client(irc_server, irc_name, opts);
client.addListener("message", function(from, to, message) {
dispatch({
from: from,
to: to,
message: message
});
return success("" + to + ":" + from + " => " + message);
});
return client.addListener("error", function(message) {
return error(message);
});
};
hear(/^pickles: help/i, function(message) {
var functionality, phrase, _results;
say(message.from, "I listen for the following...");
_results = [];
for (phrase in descriptions) {
functionality = descriptions[phrase];
_results.push(say(message.from, "" + phrase + " => " + functionality));
}
return _results;
});
desc('weather me :place', 'Get the weather for now, today and tomorrow for :place');
hear(/^weather me (.*)/i, function(message) {
var location;
seen.setSeenUser(message.from, message.to);
location = message.match[1];
return weather.getWeather(location, function(err, weather) {
if (err || !weather) {
return say(message.to, "Could not find weather for '" + location + "'");
} else {
say(message.to, "Current: " + weather.current);
say(message.to, "Today: " + weather.today);
return say(message.to, "Tomorrow: " + weather.tomorrow);
}
});
});
desc('image me :phrase', 'Get a random image from Google Images for the search :phrase');
hear(/^image me (.*)$/i, function(message) {
var phrase;
seen.setSeenUser(message.from, message.to);
phrase = message.match[1];
return image.getImage(phrase, function(err, image) {
if (err || !image) {
return say(message.to, "Could not find an image for '" + phrase + "'");
} else {
return say(message.to, image);
}
});
});
desc('commit me :user/:project', 'Get the latest commit for the GitHub repo :user/:project');
hear(/^commit me (.*)\/(.*)/i, function(message) {
var proj, user;
seen.setSeenUser(message.from, message.to);
user = message.match[1];
proj = message.match[2];
return github.getLatestCommit(user, proj, function(err, commit) {
if (err || !commit) {
return say(message.to, "Could not get latest commit for '" + user + "/" + proj + "'");
} else {
return say(message.to, "" + commit.author + " commited '" + commit.message + "' to " + user + "/" + proj + " on " + commit.date);
}
});
});
desc('fortune me', 'Get a poorly formatted fortune');
hear(/fortune me/i, function(message) {
seen.setSeenUser(message.from, message.to);
return fortune.getFortune(function(err, fortune) {
if (err || !fortune) {
return say(message.to, "" + message.from + ": " + err);
} else {
return say(message.to, "" + message.from + ": " + fortune);
}
});
});
desc('bash me', 'Get a random quote link from bash.org');
hear(/^bash me/i, function(message) {
seen.setSeenUser(message.from, message.to);
return bash.getBash(function(err, bash) {
if (err || !bash) {
return say(message.to, "" + message.from + ": " + err);
} else {
return say(message.to, "" + message.from + ": " + bash);
}
});
});
desc('seen :nick', 'Get when I last saw :nick and in which channel');
hear(/^seen ([\w^_-|\{\}\[\]`\\]+)$/i, function(message) {
var user;
seen.setSeenUser(message.from, message.to);
user = message.match[1];
return seen.getSeenUser(user, function(err, msg) {
if (err || !msg) {
return say(message.to, "" + message.from + ": " + err);
} else {
return say(message.to, "" + message.from + ": " + msg);
}
});
});
desc('roll me :side', 'Get a random number based on a die roll with :sides or 6 sides');
hear(/^roll me ?(\d*)/i, function(message) {
var sides;
seen.setSeenUser(message.from, message.to);
sides = parseInt(message.match[1] || 6);
if (sides === 0) {
return say(message.to, "" + message.from + " I cannot make the warp core stabilizers divide by 0!");
} else {
return say(message.to, "" + message.from + " rolls a " + sides + " sided die and gets " + (Math.floor(Math.random() * sides) + 1));
}
});
desc('is :domain up', 'Get the status of the website hosted at :domain');
hear(/^is (.*) up/, function(message) {
var url;
seen.setSeenUser(message.from, message.to);
url = message.match[1];
return isup.isItUp(url, function(err, msg) {
if (err || !msg) {
return say(message.to, "" + message.from + ": " + err);
} else {
return say(message.to, "" + message.from + ": " + msg);
}
});
});
desc('what is the time in :place', 'Get the current time in :place');
hear(/^what is the time in (.*)/i, function(message) {
var place;
seen.setSeenUser(message.from, message.to);
place = message.match[1];
return time.getTime(place, function(err, msg) {
if (err || !msg) {
return say(message.to, "" + message.from + ": " + err);
} else {
return say(message.to, "" + message.from + ": " + msg);
}
});
});
desc('movie me :movie_or_tv_show', 'Get the IMDb link for :movie_or_tv_show');
hear(/^movie me (.*)/i, function(message) {
var query;
seen.setSeenUser(message.from, message.to);
query = message.match[1];
return imdb.getMovie(query, function(err, msg) {
if (err || !msg) {
return say(message.to, "" + message.from + ": " + err);
} else {
return say(message.to, "" + message.from + ": " + msg);
}
});
});
desc('what are the pulls on :user/:project', 'Get the latest pull requests for the GitHub repo :user/:project');
hear(/^what are the pulls on (.*)\/(.*)/i, function(message) {
var proj, user;
seen.setSeenUser(message.from, message.to);
user = message.match[1];
proj = message.match[2];
return github.getPullRequests(user, proj, function(err, msg) {
var pull, _i, _len, _results;
if (err || !msg) {
return say(message.to, "" + message.from + ": " + err);
} else {
_results = [];
for (_i = 0, _len = msg.length; _i < _len; _i++) {
pull = msg[_i];
_results.push(say(message.to, "" + pull));
}
return _results;
}
});
});
hear(/.*/, function(message) {
return seen.setSeenUser(message.from, message.to);
});
listen();
}).call(this);