From 3182903c77eb9e7636ae3d8ec31de3f8178b101a Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Tue, 8 Nov 2022 02:20:04 -0600 Subject: [PATCH] login + connect --- bot.js | 9 +++++++-- src/db/connect.js | 7 +++---- src/handle/startup/run/login.js | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/handle/startup/run/login.js diff --git a/bot.js b/bot.js index 9641b8f..8592c53 100644 --- a/bot.js +++ b/bot.js @@ -21,9 +21,14 @@ const startBot = async () => { const loggers = log(client); Object.keys(loggers).forEach(logger => client[logger] = loggers[logger]); - client.log(client.utils.gr(client.config.randResp.clistart), {color: "#78d9f8", source: "NATS"}, true); //natsuki's wakeup log + client.log(client.utils.gr(client.config.randResp.clistart), {color: "#78d9f8", source: "NATS"}, true, true); //natsuki's wakeup log + + await require('./src/handle/startup/run/login')(client); //log in to discord + await require('./src/db/connect')(client); //connect to database }; -startBot(); +startBot().catch(() => { + console.log("\nWell this is awkward.\n"); +}); // TODO add a .catch() and flag to recover the process // feels like there isn't a function name to do this justice :joy: // to do list: diff --git a/src/db/connect.js b/src/db/connect.js index 76e34b6..051f58b 100644 --- a/src/db/connect.js +++ b/src/db/connect.js @@ -7,17 +7,16 @@ const ora = require('../util/log/ora'); module.exports = async client => { const auth = client.auth; const t = Date.now(); - client.misc = {}; client.misc.dbconnected = false; 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) => log(client).error("Failed to connect to mongoose!! Error below.", 0, true, true, e)) + ).catch((e) => client.error("Failed to connect to mongoose!! Error below.", 0, true, true, e)) .then(() => {client.misc.dbconnected = true;}); if (!client.misc.dbconnected) { - log(client).warn("Database not connected, considering runtime to be unusable and exiting.", 0, true, true); + client.warn("Database not connected, considering runtime to be unusable and exiting.", 0, true, true); throw new Error(); } - return log(client).success(`Connected to Mongo Database in ${chalk.white(`${Date.now() - t}ms`)}.`); + return client.success(`Connected to Mongo Database in ${chalk.white(`${Date.now() - t}ms`)}.`); }; \ No newline at end of file diff --git a/src/handle/startup/run/login.js b/src/handle/startup/run/login.js new file mode 100644 index 0000000..8551ef5 --- /dev/null +++ b/src/handle/startup/run/login.js @@ -0,0 +1,19 @@ +const chalk = require('chalk'); + +const ora = require('../../../util/log/ora'); + +module.exports = async (client) => { + const t = Date.now(); + client.misc = {}; + client.misc.dscconnected = false; + + 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;}); + if (!client.misc.dscconnected) { + client.warn("Discord not connected, considering runtime to be unusable and exiting.", 0, true, true); + throw new Error(); + } + return client.success(`Connected to Discord in ${chalk.white(`${Date.now() - t}ms`)}.`); +};