forked from FCoQ/dobby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.js
67 lines (54 loc) · 1.53 KB
/
plugins.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
var fs = require('fs');
function fetchPlugin(name, config) {
var path = "./plugins/" + name;
if (!fs.existsSync(path + '.js')) {
path = "./plugins/contrib/" + name;
}
var plugin = require(path);
if (typeof plugin.config == "function") {
plugin.config(config);
}
return plugin;
}
var plugins = [];
exports.init = function(config_plugins) {
if (typeof config_plugins == "object") {
for (name in config_plugins) {
if (typeof config_plugins[name] == "object") {
var plugin = config_plugins[name];
if (plugin.on === true) {
delete plugin.on;
plugins.push(fetchPlugin(name, plugin));
}
} else if (config_plugins[name]) {
plugins.push(fetchPlugin(name, plugin));
}
}
}
return exports;
}
exports.startPlugins = function(dobby) {
plugins.forEach(function(item) {
if (typeof item.init == "function") {
item.init(dobby);
}
})
}
exports.getHelpMessage = function() {
var res = "";
([].concat.apply([], plugins.filter(function(item) {
return (typeof item.help) != "undefined";
}).map(function(item) {
return item.help;
}))).map(function(item) {
res += "[I]" + item[0] + "[/I]: " + item[1] + "\n";
});
return res;
}
exports.onMessage = function(msg, dobby) {
plugins.forEach(function(item) {
if (typeof item.onMessage == "function") {
item.onMessage(msg, dobby);
}
})
}