Install plugins before connecting to Discord

This avoids unnecessarily connecting to the gateway if plugin
installation fails.
cshd
Dragory 2020-11-22 13:14:50 +02:00
parent 994a07843a
commit daf7cb5deb
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
1 changed files with 42 additions and 21 deletions

View File

@ -18,6 +18,9 @@ const {ACCIDENTAL_THREAD_MESSAGES} = require("./data/constants");
module.exports = { module.exports = {
async start() { async start() {
console.log("Preparing plugins...");
await installAllPlugins();
console.log("Connecting to Discord..."); console.log("Connecting to Discord...");
bot.once("ready", async () => { bot.once("ready", async () => {
@ -57,10 +60,11 @@ module.exports = {
console.log("Initializing..."); console.log("Initializing...");
initStatus(); initStatus();
initBaseMessageHandlers(); initBaseMessageHandlers();
initUpdateNotifications();
console.log("Loading plugins..."); console.log("Loading plugins...");
const pluginResult = await initPlugins(); const pluginResult = await loadAllPlugins();
console.log(`Loaded ${pluginResult.loadedCount} plugins (${pluginResult.builtInCount} built-in plugins, ${pluginResult.externalCount} external plugins)`); console.log(`Loaded ${pluginResult.loadedCount} plugins (${pluginResult.baseCount} 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.");
@ -289,19 +293,14 @@ function initBaseMessageHandlers() {
}); });
} }
async function initPlugins() { function initUpdateNotifications() {
// Initialize command manager if (config.updateNotifications) {
const commands = createCommandManager(bot); updates.startVersionRefreshLoop();
// Register command aliases
if (config.commandAliases) {
for (const alias in config.commandAliases) {
commands.addAlias(config.commandAliases[alias], alias);
} }
} }
// Load plugins function getBasePlugins() {
const builtInPlugins = [ return [
"file:./src/modules/reply", "file:./src/modules/reply",
"file:./src/modules/close", "file:./src/modules/close",
"file:./src/modules/logs", "file:./src/modules/logs",
@ -319,21 +318,43 @@ async function initPlugins() {
"file:./src/modules/joinLeaveNotification", "file:./src/modules/joinLeaveNotification",
"file:./src/modules/roles", "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); 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 }); const pluginApi = getPluginAPI({ bot, knex, config, commands });
await loadPlugins(plugins, pluginApi); await loadPlugins([...basePlugins, ...externalPlugins], pluginApi);
if (config.updateNotifications) {
updates.startVersionRefreshLoop();
}
return { return {
loadedCount: plugins.length, loadedCount: plugins.length,
builtInCount: builtInPlugins.length, baseCount: basePlugins.length,
externalCount: plugins.length - builtInPlugins.length, externalCount: externalPlugins.length,
}; };
} }