-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·76 lines (68 loc) · 2.71 KB
/
app.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
#!/usr/bin/env node
require('dotenv').config();
const {App: SlackApp, LogLevel} = require('@slack/bolt');
const glob = require('glob');
const path = require('path');
const log = require('./logging');
const semver = require('semver');
const slackApp = new SlackApp({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
socketMode: true,
appToken: process.env.SLACK_APP_TOKEN,
port: process.env.PORT || 3000,
deferInitialization: true,
logLevel: LogLevel.WARN
});
const orionVersion = require('./package.json').version;
const loadPlugins = async () => {
let loadedPlugins = [];
const pluginPath = './plugins/';
log.internal(`Scanning ${pluginPath} for plugins.`);
let pluginFiles = glob.sync(path.join(pluginPath, '*/index.js'));
for (let i = 0; i < pluginFiles.length; i++) {
let loadedPlugin = require('./' + pluginFiles[i]);
if(!loadedPlugin.WrittenForVersion || !semver.valid(loadedPlugin.WrittenForVersion)){
log.error(`Plugin "${loadedPlugin.PluginName}" was not written for this version of Orion.`);
continue;
}
if(semver.diff(orionVersion, loadedPlugin.WrittenForVersion) == 'major'){
log.error(`Plugin "${loadedPlugin.PluginName}" was written for a different major version of Orion (${loadedPlugin.WrittenForVersion}). Attempting to load regardless.`);
}
if(!loadedPlugin.init){
loadedPlugins.push(loadedPlugin);
continue;
}
try {
loadedPlugin.init();
loadedPlugins.push(loadedPlugin);
} catch(initErr){
log.error(`Plugin "${loadedPlugin.PluginName}" not loaded. Reason:\n${initErr}`);
}
}
let pluginLoadReport = [`Loaded the following ${loadedPlugins.length} plugins:`];
pluginLoadReport = pluginLoadReport.concat(...loadedPlugins.map((a) => {return ` ${a.PluginName} Version ${a.Version}`}));
log.internal(pluginLoadReport.join('\n'));
return loadedPlugins;
};
const registerMessageHandlers = async (slackApp, plugins) => {
log.internal("Registering message handlers");
let c = 0;
for (let i = 0; i < plugins.length; i++) {
const plugin = plugins[i];
for (let j = 0; j < plugin.MessageHandlers.length; j++) {
const handler = plugin.MessageHandlers[j];
slackApp.message(handler.syntax, handler.handler);
c++;
}
}
log.internal(`Registered ${c} message handlers.`);
};
(async () => {
log.startup();
let plugins = await loadPlugins();
await slackApp.init();
await slackApp.start();
log.internal("Connected to Slack.");
await registerMessageHandlers(slackApp, plugins);
})();