JaBa/scripts/verify-config.js

141 lines
4.3 KiB
JavaScript
Raw Normal View History

2021-12-10 21:39:54 +05:00
/* eslint-disable no-async-promise-executor */
const config = require("../config");
2021-12-10 21:39:54 +05:00
const fetch = require("node-fetch");
const chalk = require("chalk");
const success = (message) => console.log(` ${chalk.green("✓")} ${message}`);
const error = (message, howToFix) => console.log(` ${chalk.red("✗")} ${message}${howToFix ? ` : ${howToFix}` : ""}`);
const ignore = (message) => console.log(` ${chalk.yellow("~")} ${message}`);
const checks = [
() => {
console.log("\n\nEnvironnement");
return new Promise((res) => {
if (parseInt(process.version.split(".")[0].split("v")[1]) >= 12) {
2021-12-16 16:05:15 +05:00
success("node.js version equal or higher than v12");
2021-12-10 21:39:54 +05:00
} else {
error("node.js version should be equal or higher than v12");
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
res();
});
},
() => {
console.log("\n\nDiscord Bot");
return new Promise((res) => {
const Discord = require("discord.js");
const client = new Discord.Client();
let readyResolve;
new Promise((resolve) => readyResolve = resolve);
client.login(config.token).then(async () => {
2021-12-22 19:13:26 +05:00
success("Valid bot token");
2021-12-10 21:39:54 +05:00
await readyResolve();
if (!client.guilds.cache.has("568120814776614924")) {
2021-12-22 19:13:26 +05:00
error("Should be added to the emojis server", "please add your bot on this server: https://discord.gg/5wrBEwE4bc to make the emojis working");
2021-12-10 21:39:54 +05:00
} else {
2021-12-22 19:13:26 +05:00
success("Added to the emojis server");
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
res();
}).catch(() => {
2021-12-22 19:13:26 +05:00
error("Should be a valid bot token");
2021-12-10 21:39:54 +05:00
res();
});
client.on("ready", readyResolve);
});
},
() => {
console.log("\n\nMongoDB");
return new Promise((res) => {
const MongoClient = require("mongodb").MongoClient;
const dbName = config.mongoDB.split("/").pop();
const baseURL = config.mongoDB.substr(0, config.mongoDB.length - dbName.length);
const client = new MongoClient(baseURL, {
useUnifiedTopology: true
});
client.connect().then(async () => {
2021-12-16 16:05:15 +05:00
success("Connection to Mongo database success");
2021-12-10 21:39:54 +05:00
res();
}).catch(() => {
2021-12-22 19:13:26 +05:00
error("Should be able to connect to Mongo database", "please verify if the MongoDB server is started");
2021-12-10 21:39:54 +05:00
res();
});
});
},
() => {
console.log("\n\nAPI keys");
return new Promise(async (resolve) => {
if (!config.apiKeys.amethyste) {
2021-12-22 19:13:26 +05:00
ignore("Amethyste API is not configured, key should not be checked.");
2021-12-10 21:39:54 +05:00
} else {
const res = await fetch("https://v1.api.amethyste.moe/generate/blurple", {
method: "POST",
headers: {
Authorization: `Bearer ${config.apiKeys.amethyste}`
}
});
const result = await res.json();
if (result.status === 401) {
2021-12-22 19:13:26 +05:00
error("Should be a valid Amethyste API key", "get your key here: https://api.amethyste.moe/");
2021-12-10 21:39:54 +05:00
} else {
2021-12-22 19:13:26 +05:00
success("Valid Amethyste API key");
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
}
if (!config.apiKeys.blagueXYZ) {
ignore("blague.xyz API is not configured, key should not be checked.");
} else {
const res = await fetch("https://blague.xyz/api/joke/random", {
headers: {
Authorization: config.apiKeys.blagueXYZ
}
});
const result = await res.json();
if (result.status === 401) {
2021-12-22 19:13:26 +05:00
error("Should be a valid blague.xyz key", "get your key here: https://blague.xyz/");
2021-12-10 21:39:54 +05:00
} else {
2021-12-22 19:13:26 +05:00
success("Valid blague.xyz key");
2022-01-13 00:26:23 +05:00
}
}
2021-12-10 21:39:54 +05:00
resolve();
});
},
() => {
console.log("\n\nDashboard");
return new Promise(async (resolve) => {
if (!config.dashboard.enabled) {
ignore("Dashboard is not enabled, config shouldn't be checked.");
} else {
const checkPortTaken = (port) => {
return new Promise((resolve) => {
const net = require("net");
const tester = net.createServer()
.once("error", () => {
resolve(true);
})
2021-12-26 19:29:37 +05:00
.once("listening", function () {
2021-12-10 21:39:54 +05:00
tester
2021-12-26 19:29:37 +05:00
.once("close", function () {
2021-12-10 21:39:54 +05:00
resolve(false);
})
.close();
})
.listen(port);
});
};
const isPortTaken = await checkPortTaken(config.dashboard.port);
if (isPortTaken) {
2021-12-22 19:13:26 +05:00
error("Dashboard port should be available", "you have probably another process using this port");
2021-12-10 21:39:54 +05:00
} else {
2021-12-22 19:13:26 +05:00
success("Dashboard port is available");
2022-01-13 00:26:23 +05:00
}
}
2021-12-10 21:39:54 +05:00
resolve();
});
}
];
(async () => {
console.log(chalk.yellow("This script will check if your config is errored, and some other important things such as whether your database is started, etc..."));
2021-12-11 21:08:37 +05:00
for (const check of checks) {
2021-12-10 21:39:54 +05:00
await check();
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
process.exit(0);
})();