From 00af710ebd8d35b5c27346ab1bb119e084679c00 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Wed, 9 Nov 2022 16:00:54 -0700 Subject: [PATCH] fix error handling for connections --- bot.js | 1 + src/db/connect.js | 8 +++++--- src/handle/startup/run/login.js | 8 +++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/bot.js b/bot.js index 8592c53..ab0488a 100644 --- a/bot.js +++ b/bot.js @@ -18,6 +18,7 @@ const startBot = async () => { client.config.randResp = randresp; require('./src/util/misc/setutils')(client); // add some basic swiss army knife utils + const loggers = log(client); Object.keys(loggers).forEach(logger => client[logger] = loggers[logger]); diff --git a/src/db/connect.js b/src/db/connect.js index 051f58b..1c4bd42 100644 --- a/src/db/connect.js +++ b/src/db/connect.js @@ -7,13 +7,15 @@ const ora = require('../util/log/ora'); module.exports = async client => { const auth = client.auth; const t = Date.now(); - client.misc.dbconnected = false; + client.misc.dbconnected = true; await ora(chalk.blueBright.bold.underline("Connecting to MongoDB..."), mongoose.connect(`mongodb+srv://${auth.database.user}:${auth.database.password}@${auth.database.cluster}.3jpp4.mongodb.net/test`, { useFindAndModify: false, useNewUrlParser: true, dbName: auth.database.name, useUnifiedTopology: true, useCreateIndex: true }) - ).catch((e) => client.error("Failed to connect to mongoose!! Error below.", 0, true, true, e)) - .then(() => {client.misc.dbconnected = true;}); + ).catch((e) => { + client.error("Failed to connect to mongoose!! Error below.", 0, 0, true, e); + client.misc.dbconnected = false; + }); if (!client.misc.dbconnected) { client.warn("Database not connected, considering runtime to be unusable and exiting.", 0, true, true); throw new Error(); diff --git a/src/handle/startup/run/login.js b/src/handle/startup/run/login.js index 8551ef5..89c91a9 100644 --- a/src/handle/startup/run/login.js +++ b/src/handle/startup/run/login.js @@ -5,12 +5,14 @@ const ora = require('../../../util/log/ora'); module.exports = async (client) => { const t = Date.now(); client.misc = {}; - client.misc.dscconnected = false; + client.misc.dscconnected = true; await ora(`Waking up Natsuki... ${chalk.blueBright.bold.underline("(Connecting to Discord...)")}`, client.login(client.auth.token) - ).catch((e) => client.error("Failed to connect to Discord!! Error below.", 0, true, true, e)) - .then(() => {client.misc.dscconnected = true;}); + ).catch((e) => { + client.error("Failed to connect to Discord!! Error below.", 0, 0, true, e); + client.misc.dscconnected = false; + }); if (!client.misc.dscconnected) { client.warn("Discord not connected, considering runtime to be unusable and exiting.", 0, true, true); throw new Error();