-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbot.js
80 lines (65 loc) · 1.87 KB
/
dbot.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
var irc = require('irc')
, jsonfile = require('jsonfile')
, deferred = require('deferred')
, cp = require('child_process')
, npmls = function () {
var d = deferred();
cp.exec('npm ls --json', function(err, stdout, stderr) {
d.resolve(JSON.parse(stdout));
});
return d.promise;
}
, dbot = {
config: jsonfile.readFileSync('./config.json'),
client: null,
plugins: [],
plug: function (plugin) {
var p;
try {
p = require('dbot-' + plugin);
this.plugins.push(p);
} catch (e) {
console.log('** ' + plugin + ' plugin was not found! **');
}
},
match: function (message, regex, callback) {
var matches = message.match(regex);
if (matches) {
return callback.call(this, matches);
}
},
init: function () {
var bot = this;
npmls().then(function (packages) {
for (pkg in packages.dependencies) {
if (pkg.match(/dbot-(.+)/)) {
require(pkg);
}
}
});
bot.client = new irc.Client(
bot.config.server,
bot.config.nick,
{
channels: bot.config.channels,
debug: bot.config.debug
}
);
// Print errors to console
bot.client.addListener('error', function (message) {
console.log('error: ', message);
});
// Listen to PMs
bot.client.addListener('pm', function (from, message) {
bot.plugins.forEach(function (plugin) {
var response = bot.match(message, plugin.regex, plugin.callback);
if (deferred.isPromise(response)) {
response.then(function (result) {
bot.client.say(result);
});
}
});
});
}
};
dbot.init();