Add support for async plugin load functions

cshd
Dragory 2020-07-14 00:17:31 +03:00
parent 815825de94
commit 0c25afaec2
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
2 changed files with 16 additions and 8 deletions

View File

@ -45,7 +45,10 @@ module.exports = {
console.log('Initializing...'); console.log('Initializing...');
initStatus(); initStatus();
initBaseMessageHandlers(); initBaseMessageHandlers();
initPlugins();
console.log('Loading plugins...');
const pluginResult = await initPlugins();
console.log(`Loaded ${pluginResult.loadedCount} plugins (${pluginResult.builtInCount} built-in plugins, ${pluginResult.externalCount} external plugins)`);
console.log(''); console.log('');
console.log('Done! Now listening to DMs.'); console.log('Done! Now listening to DMs.');
@ -253,7 +256,7 @@ function initBaseMessageHandlers() {
}); });
} }
function initPlugins() { async function initPlugins() {
// Initialize command manager // Initialize command manager
const commands = createCommandManager(bot); const commands = createCommandManager(bot);
@ -265,7 +268,6 @@ function initPlugins() {
} }
// Load plugins // Load plugins
console.log('Loading plugins');
const builtInPlugins = [ const builtInPlugins = [
reply, reply,
close, close,
@ -293,11 +295,17 @@ function initPlugins() {
} }
const pluginApi = getPluginAPI({ bot, knex, config, commands }); const pluginApi = getPluginAPI({ bot, knex, config, commands });
plugins.forEach(pluginFn => loadPlugin(pluginFn, pluginApi)); for (const plugin of plugins) {
await loadPlugin(plugin, pluginApi);
console.log(`Loaded ${plugins.length} plugins (${builtInPlugins.length} built-in plugins, ${plugins.length - builtInPlugins.length} external plugins)`); }
if (config.updateNotifications) { if (config.updateNotifications) {
updates.startVersionRefreshLoop(); updates.startVersionRefreshLoop();
} }
return {
loadedCount: plugins.length,
builtInCount: builtInPlugins.length,
externalCount: plugins.length - builtInPlugins.length,
};
} }

View File

@ -24,7 +24,7 @@ module.exports = {
}; };
}, },
loadPlugin(plugin, api) { async loadPlugin(plugin, api) {
plugin(api); await plugin(api);
} }
}; };