From 6635fe1414a18e1eb39b62e19cac1704d01ab9f1 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Tue, 3 Dec 2019 02:00:00 +0200 Subject: [PATCH] Make sure numeric options are numbers. Convert empty strings from config.ini to null. Some loading message tweaks. --- CHANGELOG.md | 6 ++++-- src/config.js | 40 ++++++++++++++++++++++++++++++---------- src/main.js | 6 ++++-- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5b4f9e..0f8cbdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,10 @@ ## v2.29.0 * **Change default configuration format to .ini** * Existing `config.json` files will continue to work and will not be deprecated -* New, rewritten instructions for setting up and using the bot -* Updated several package dependencies + * This makes the default configuration format for the bot much more approachable than JSON +* New rewritten instructions for setting up and using the bot +* New easy-to-use `start.bat` file for Windows +* Update several package dependencies * Fixed incompatibility with certain Node.js 10 versions ## v2.28.0 diff --git a/src/config.js b/src/config.js index 136a6bc..c9993c5 100644 --- a/src/config.js +++ b/src/config.js @@ -40,7 +40,7 @@ if (! foundConfigFile) { } // Load config file -console.log(`Loading configuration from ${foundConfigFile}`); +console.log(`Loading configuration from ${foundConfigFile}...`); try { if (foundConfigFile.endsWith('.js')) { userConfig = require(`../${foundConfigFile}`); @@ -123,6 +123,7 @@ const defaultConfig = { }; const required = ['token', 'mailGuildId', 'mainGuildId', 'logChannelId']; +const numericOptions = ['requiredAccountAge', 'requiredTimeOnServer', 'smallAttachmentLimit', 'port']; const finalConfig = Object.assign({}, defaultConfig); @@ -152,14 +153,6 @@ Object.assign(finalConfig['knex'], { } }); -// Make sure all of the required config options are present -for (const opt of required) { - if (! finalConfig[opt]) { - console.error(`Missing required config.json value: ${opt}`); - process.exit(1); - } -} - if (finalConfig.smallAttachmentLimit > 1024 * 1024 * 8) { finalConfig.smallAttachmentLimit = 1024 * 1024 * 8; console.warn('[WARN] smallAttachmentLimit capped at 8MB'); @@ -212,6 +205,33 @@ if (finalConfig.newThreadCategoryId) { delete finalConfig.newThreadCategoryId; } -console.log("Configuration ok"); +// Turn empty string options to null (i.e. "option=" without a value in config.ini) +for (const [key, value] of Object.entries(finalConfig)) { + if (value === '') { + finalConfig[key] = null; + } +} + +// Cast numeric options to numbers +for (const numericOpt of numericOptions) { + if (finalConfig[numericOpt] != null) { + const number = parseFloat(finalConfig[numericOpt]); + if (Number.isNaN(number)) { + console.error(`Invalid numeric value for ${numericOpt}: ${finalConfig[numericOpt]}`); + process.exit(1); + } + finalConfig[numericOpt] = number; + } +} + +// Make sure all of the required config options are present +for (const opt of required) { + if (! finalConfig[opt]) { + console.error(`Missing required config.json value: ${opt}`); + process.exit(1); + } +} + +console.log("Configuration ok!"); module.exports = finalConfig; diff --git a/src/main.js b/src/main.js index 16d7058..5a29e70 100644 --- a/src/main.js +++ b/src/main.js @@ -35,18 +35,20 @@ module.exports = { console.log('Connecting to Discord...'); bot.once('ready', async () => { - console.log('Connected, waiting for guilds to become available'); + console.log('Connected! Waiting for guilds to become available...'); await Promise.all([ ...config.mainGuildId.map(id => waitForGuild(id)), waitForGuild(config.mailGuildId) ]); - console.log('Initializing'); + console.log('Initializing...'); initStatus(); initBaseMessageHandlers(); initPlugins(); + console.log(''); console.log('Done! Now listening to DMs.'); + console.log(''); }); bot.connect();