From f55a69434a5559a4dba0aadc1c9b1ab94d735d5b Mon Sep 17 00:00:00 2001 From: Taken Date: Thu, 30 Nov 2023 18:07:33 +0100 Subject: [PATCH] Added init function to prevent missing env vars --- src/index.js | 2 ++ src/utils/init.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/utils/init.js diff --git a/src/index.js b/src/index.js index b29f3a9..b1d4e20 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ const { Client, GatewayIntentBits, Partials, Collection } = require("discord.js") const { loadSlashCommandsEvents, loadContextMenuEvents, loadModalEvents, loadButtonEvents, loadEvents, loadAutocompleteEvents } = require("./utils/eventHandler.js") const { autoDeployCommands } = require("./utils/autodeploy.js") +const { init } = require("./utils/init.js") require("dotenv").config() const mongoURI = process.env.MONGOURI const { connect } = require("mongoose") @@ -26,6 +27,7 @@ client.commands = new Collection() client.events = new Collection() client.modals = new Collection() +init() loadSlashCommandsEvents(client) loadAutocompleteEvents(client) loadContextMenuEvents(client) diff --git a/src/utils/init.js b/src/utils/init.js new file mode 100644 index 0000000..85a7ff3 --- /dev/null +++ b/src/utils/init.js @@ -0,0 +1,38 @@ +const envars = [ + "TOKEN", + "MONGOURI", + "DEV", + "HYPIXELAPIKEY" +] +const devenvars = [ + "DEVTOKEN", + "CLIENTID", + "DEVID", + "GUILDID" +] + +function init() { + if (process.env.NODe_ENV !== "dev") { + for (const envar of envars) { + if (!process.env[envar]) { + console.error(`Missing ${envar} environment variable!`) + process.exit(1) + } + } + } else { + for (const envar of envars) { + if (!process.env[envar]) { + console.error(`Missing ${envar} environment variable!`) + process.exit(1) + } + } + for (const envar of devenvars) { + if (!process.env[envar]) { + console.error(`Missing ${envar} environment variable!`) + process.exit(1) + } + } + } +} + +module.exports = { init }