-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugins.js
68 lines (58 loc) · 1.48 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
67
68
/**
* A collection of utility functions which load all <pluginName>.js files exported functions,
* as well as managing initialisation (init) and active-plugin-worker scripts (run),
* finally provides a route to core plugin functions such as (onMessage).
*/
const fs = require("fs");
function fetchPlugin(name) {
let path = "./plugins/" + name;
let plugin = require(path);
console.info("Loaded:", name);
return plugin;
}
// Stores each plugin's exported modules (array/function)
let pluginsArray = [];
exports.init = config_plugins => {
if (typeof config_plugins == "object") {
for (name in config_plugins) {
// Checks that plugin is enabled in config (true)
if (config_plugins[name]) {
pluginsArray.push(fetchPlugin(name));
}
}
}
// Returns all export functions in plugins.js
return exports;
};
exports.startPlugins = (helpers, jarvis) => {
pluginsArray.forEach(item => {
if (typeof item.run == "function") {
item.run(helpers, jarvis);
}
});
};
exports.onMessage = (msg, jarvis) => {
pluginsArray.forEach(item => {
if (typeof item.onMessage == "function") {
item.onMessage(msg, jarvis);
}
});
};
exports.getHelpMessage = () => {
let response = "[b]Jarvis Assistant Bot - Commands:[/b] \n";
[].concat
.apply(
[],
pluginsArray
.filter(item => {
return typeof item.help != "undefined";
})
.map(item => {
return item.help;
})
)
.map(item => {
response += `[b]${item[0]}[/b] - ${item[1]}\n`;
});
return response;
};