From daf7cb5deb739251dc02d3b36cd6d9e2da4c8f4d Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sun, 22 Nov 2020 13:14:50 +0200 Subject: [PATCH] Install plugins before connecting to Discord This avoids unnecessarily connecting to the gateway if plugin installation fails. --- src/main.js | 63 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/src/main.js b/src/main.js index a220cb1..b7fcbfd 100644 --- a/src/main.js +++ b/src/main.js @@ -18,6 +18,9 @@ const {ACCIDENTAL_THREAD_MESSAGES} = require("./data/constants"); module.exports = { async start() { + console.log("Preparing plugins..."); + await installAllPlugins(); + console.log("Connecting to Discord..."); bot.once("ready", async () => { @@ -57,10 +60,11 @@ module.exports = { console.log("Initializing..."); initStatus(); initBaseMessageHandlers(); + initUpdateNotifications(); console.log("Loading plugins..."); - const pluginResult = await initPlugins(); - console.log(`Loaded ${pluginResult.loadedCount} plugins (${pluginResult.builtInCount} built-in plugins, ${pluginResult.externalCount} external plugins)`); + const pluginResult = await loadAllPlugins(); + console.log(`Loaded ${pluginResult.loadedCount} plugins (${pluginResult.baseCount} built-in plugins, ${pluginResult.externalCount} external plugins)`); console.log(""); console.log("Done! Now listening to DMs."); @@ -289,19 +293,14 @@ function initBaseMessageHandlers() { }); } -async function initPlugins() { - // Initialize command manager - const commands = createCommandManager(bot); - - // Register command aliases - if (config.commandAliases) { - for (const alias in config.commandAliases) { - commands.addAlias(config.commandAliases[alias], alias); - } +function initUpdateNotifications() { + if (config.updateNotifications) { + updates.startVersionRefreshLoop(); } +} - // Load plugins - const builtInPlugins = [ +function getBasePlugins() { + return [ "file:./src/modules/reply", "file:./src/modules/close", "file:./src/modules/logs", @@ -319,21 +318,43 @@ async function initPlugins() { "file:./src/modules/joinLeaveNotification", "file:./src/modules/roles", ]; +} - const plugins = [...builtInPlugins, ...config.plugins]; +function getExternalPlugins() { + return config.plugins; +} +function getAllPlugins() { + return [...getBasePlugins(), ...getExternalPlugins()]; +} + +async function installAllPlugins() { + const plugins = getAllPlugins(); await installPlugins(plugins); +} + +async function loadAllPlugins() { + // Initialize command manager + const commands = createCommandManager(bot); + + // Register command aliases + if (config.commandAliases) { + for (const alias in config.commandAliases) { + commands.addAlias(config.commandAliases[alias], alias); + } + } + + // Load plugins + const basePlugins = getBasePlugins(); + const externalPlugins = getExternalPlugins(); + const plugins = getAllPlugins(); const pluginApi = getPluginAPI({ bot, knex, config, commands }); - await loadPlugins(plugins, pluginApi); - - if (config.updateNotifications) { - updates.startVersionRefreshLoop(); - } + await loadPlugins([...basePlugins, ...externalPlugins], pluginApi); return { loadedCount: plugins.length, - builtInCount: builtInPlugins.length, - externalCount: plugins.length - builtInPlugins.length, + baseCount: basePlugins.length, + externalCount: externalPlugins.length, }; }