Added init function to prevent missing env vars

This commit is contained in:
2023-11-30 18:07:33 +01:00
parent f21608cf86
commit f55a69434a
2 changed files with 40 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
const { Client, GatewayIntentBits, Partials, Collection } = require("discord.js") const { Client, GatewayIntentBits, Partials, Collection } = require("discord.js")
const { loadSlashCommandsEvents, loadContextMenuEvents, loadModalEvents, loadButtonEvents, loadEvents, loadAutocompleteEvents } = require("./utils/eventHandler.js") const { loadSlashCommandsEvents, loadContextMenuEvents, loadModalEvents, loadButtonEvents, loadEvents, loadAutocompleteEvents } = require("./utils/eventHandler.js")
const { autoDeployCommands } = require("./utils/autodeploy.js") const { autoDeployCommands } = require("./utils/autodeploy.js")
const { init } = require("./utils/init.js")
require("dotenv").config() require("dotenv").config()
const mongoURI = process.env.MONGOURI const mongoURI = process.env.MONGOURI
const { connect } = require("mongoose") const { connect } = require("mongoose")
@@ -26,6 +27,7 @@ client.commands = new Collection()
client.events = new Collection() client.events = new Collection()
client.modals = new Collection() client.modals = new Collection()
init()
loadSlashCommandsEvents(client) loadSlashCommandsEvents(client)
loadAutocompleteEvents(client) loadAutocompleteEvents(client)
loadContextMenuEvents(client) loadContextMenuEvents(client)

38
src/utils/init.js Normal file
View File

@@ -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 }