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`)}.`); +};