-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
539 lines (364 loc) · 9.49 KB
/
index.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
const Bot = require('slackbots');
const fs = require('fs');
const config = require('./bot.json');
const megahal = require('jsmegahal');
const bukkit = require('./modules/bukkit.js');
const pokemon = require('./modules/pokemon.js');
const github = require('./modules/github.js');
const doggos = require('./modules/doggo.js');
// const spotify = require('./modules/spotify.js');
const weather = require('./modules/weather.js');
const fork = require('child_process').fork;
const cronrunner = fork(__dirname + '/cronrunner.js');
cronrunner.on('message', function (task) {
switch (task.task) {
case 'say':
warmUp();
say(task.args, task.groups);
cooldown();
break;
case 'weather':
warmUp();
weather.get(config.config.weather.lat, config.config.weather.lon).then(function (weather) {
say(weather, task.groups);
cooldown();
});
break;
case 'alerts':
weather.getAlerts(config.config.weather.lat, config.config.weather.lon, false).then(function (alerts) {
say(alerts, task.groups);
cooldown();
});
break;
default:
break;
}
});
let cooldownFlag = false;
let requestInMotionFlag = false;
// https://github.com/mishk0/slack-bot-api
// create a bot
const settings = config.env;
const bot = new Bot(settings);
let __channels = [];
let __groups = [];
let __users = [];
// The Brain where he gets his words from. You may need to create this file. Catbot does not add to this file.
let markov = __dirname + '/markov.txt';
const hal = new megahal(4);
bot.on('start', function () {
// Public channels
let _channels = bot.getChannels();
let channels = _channels._value.channels;
for (let i in channels) {
if (channels.hasOwnProperty(i)) {
__channels[channels[i]['id']] = channels[i]['name'];
}
}
// Private channels
let _groups = bot.getGroups();
let groups = _groups._value.groups;
for (let i in groups) {
if (groups.hasOwnProperty(i)) {
__groups[groups[i]['id']] = groups[i]['name'];
}
}
// Users
let _users = bot.getUsers();
let users = _users._value.users;
for (let i in users) {
if (users.hasOwnProperty(i)) {
__users[users[i]['id']] = users[i]['name'];
}
}
// Load the brain into MegaHAL
fs.readFile(markov, 'utf8', function (err, data) {
hal.addMass(data);
});
weather.prefillAlerts(config.config.weather.lat, config.config.weather.lon).then(function (weather) {
cooldown();
});
debug('ping? pong!');
});
bot.on('message', function (data) {
if (typeof data.text !== 'undefined') {
let message = data.text;
let whom = data.user;
let msg = message.split(' ');
if (meetsCriteria('weather', data)) {
if (canDo()) {
warmUp();
let channel = data.channel;
weather.getAlerts(config.config.weather.lat, config.config.weather.lon, true).then(function (weather) {
say(weather, channel);
cooldown();
}, function (error) {
say(error, channel);
cooldown();
});
}
}
// Pokemon
if (meetsCriteria('pokemon', data)) {
if (msg[0] === 'pokemon' &&
typeof msg[2] === 'undefined') {
warmUp();
if (msg[1] !== 'help') {
say([':pikachu: Looking up: ' + msg[1]], data.channel);
}
pokemon.retrieve(msg[1]).then(function (res) {
say(res, data.channel);
cooldown();
}.bind(this), function (error) {
let rr = [':shrug: [whoopsie: ' + error + ']'];
say(rr, data.channel);
console.error(error);
cooldown();
}.bind(this));
}
}
if (meetsCriteria('speak', data)) {
if (canDo()) {
warmUp();
let msg = message.split(' ');
let channel = data.channel;
if (msg[0] === 'catbot' && (msg[1] === 'help' || msg[1] === '?')) {
let response = [
'I am catbot! You can talk to me and I might reply. I like hearing my name.',
'Other commands: `pokemon`, `gh`, `bukkit`, `!doggo`, `!weather`'
];
say(response, channel);
} else {
let room = channelOrGroup(channel);
let messageSansName = message.replace(/catbot/i, '');
messageSansName = fixMessage(messageSansName);
if (room === 'room') { // Only log public rooms
writeToLog(__dirname + '/markov.txt', messageSansName);
hal.addMass(messageSansName);
}
setTimeout(function () {
say([hal.getReplyFromSentence(messageSansName)], data.channel);
}.bind(this), 1500);
}
cooldown();
}
}
if (meetsCriteria('github', data)) {
if (canDo()) {
warmUp();
let channel = data.channel;
let msg = message.split(' ');
let clean = msg.shift();
msg = msg.join(' ');
github.call(msg).then(function (data) {
say(data, channel);
cooldown();
}, function (error) {
say(error, channel);
cooldown();
});
}
}
if (meetsCriteria('doggo', data)) {
if (canDo()) {
warmUp();
let channel = data.channel;
doggos.get().then(function (res) {
say(res, channel);
cooldown();
}, function (error) {
say(error, channel);
cooldown();
});
}
}
// if (meetsCriteria('spotify', data)) {
//
// if (canDo()) {
//
// warmUp();
//
// let channel = data.channel;
//
// let msg = message.split(' ');
// let clean = msg.shift();
// msg = msg.join(' ');
//
// spotify.call(msg, whom).then(function (res) {
// say(res, channel);
// cooldown();
// }, function (error) {
// say(error, channel);
// cooldown();
// });
//
// }
//
// }
if (meetsCriteria('bukkit', data)) {
if (canDo()) {
warmUp();
let msg = message.split(' ');
let channel = data.channel;
let gif = '';
if (message[0] === '!') {
gif = msg[0].replace('!', '');
} else {
gif = msg[1];
}
if (gif === 'help' || gif === '?') {
let response = [
'Output a gif from a Bukkit site. (' + config.config.bukkit.bukkit + ')',
'Format: `!name.gif` or `bukkit name.gif`'
];
say(response, channel);
cooldown();
} else {
bukkit.get(gif).then(function (data) {
say(data, channel);
cooldown();
}, function (error) {
cooldown();
});
}
}
}
if (canDo()) {
let msg = message.split(' ');
if (msg[0] !== 'pokemon') {
let channel = data.channel;
let room = channelOrGroup(channel);
if (room === 'room') { // Only log public rooms
let messageSansName = message.replace(/catbot/i, '');
messageSansName = fixMessage(messageSansName);
writeToLog(__dirname + '/markov.txt', messageSansName);
hal.addMass(messageSansName);
}
}
}
}
});
function channelOrGroup(channel) {
let ret = '';
if (typeof channel !== 'undefined') {
if (typeof __channels[channel] !== 'undefined') {
ret = 'channel';
} else if (typeof __groups[channel] !== 'undefined') {
ret = 'group';
} else if (typeof __users[channel] !== 'undefined') {
ret = 'user';
} else {
console.log('what is this?', channel);
}
}
return ret;
}
function meetsCriteria(tool, data) {
let toolConfig = config['config'][tool];
let validMatch = false;
let validRoom = false;
let validGroup = false;
let type = channelOrGroup(data.channel);
let message = data.text;
if (typeof type === 'undefined') {
type = 'room';
}
if (typeof toolConfig !== 'undefined' && toolConfig !== '' && toolConfig !== null) {
if (typeof toolConfig['room'] !== 'undefined') {
let rooms = toolConfig['room'];
for (let i in rooms) {
if (rooms.hasOwnProperty(i)) {
if (rooms[i] === __channels[channel]) {
validRoom = true;
}
}
}
for (let i in groups) {
if (groups.hasOwnProperty(i)) {
if (groups[i] === __groups[channel]) {
validGroup = true;
}
}
}
} else {
validRoom = true;
}
if (typeof toolConfig['regex'] !== 'undefined') {
let regex = new RegExp(toolConfig['regex'], 'i');
if (message.match(regex) !== null) {
validMatch = true;
}
}
}
return (validMatch && validRoom);
}
function validRegex(regex, message) {
let rgx = new RegExp(regex, 'i');
return message.match(rgx) !== null;
}
function sayMulti(response, channels) {
if (channels.length > 0) {
for (let i in channels) {
if (channels.hasOwnProperty(i)) {
say(response, channels[i]);
}
}
}
}
function say(response, channel) {
if (typeof channel === 'object') {
sayMulti(response, channel);
} else {
let room = channelOrGroup(channel);
// const message = response.reverse();
if (room === 'channel') {
sayChannel(response, __channels[channel]);
}
if (room === 'group') {
sayGroup(response, __groups[channel]);
}
if (room === 'user') {
sayUser(response, __users[channel]);
}
}
}
function sayGroup(response, channel) {
bot.postMessageToGroup(channel, response.join('\n'));
}
function sayChannel(response, channel) {
bot.postMessageToChannel(channel, response.join('\n'));
}
function sayUser(response, channel) {
bot.postMessageToUser(channel, response.join('\n'));
}
function canDo() {
return !requestInMotionFlag && !cooldownFlag;
}
function warmUp() {
requestInMotionFlag = true;
}
function cooldown() {
requestInMotionFlag = false;
cooldownFlag = true;
setTimeout(function () {
cooldownFlag = false;
}, 2000);
}
function fixMessage(message) {
if (message.search(/[.!?,;:]$/) === -1) {
message += '.';
}
message.replace(' .', '.');
return message;
}
function writeToLog(file, text) {
text = fixMessage(text);
fs.appendFile(file, text + '\r\n', function (err) {
if (err) return console.log(err);
});
}
function debug(message) {
if (config.debug.length > 0) {
say([message], config.debug);
}
}