blob: 20357544b6db0f3a7d469de2676ae1d381ad0e4b [file] [log] [blame]
pineafan1c837242022-08-04 22:04:24 +01001import fs from "fs";
Skyler Grey75ea9172022-08-06 10:22:23 +01002// @ts-expect-error
pineafan1c837242022-08-04 22:04:24 +01003import * as readLine from "node:readline/promises";
4
5const defaultDict = {
Skyler Grey11236ba2022-08-08 21:13:33 +01006 developmentToken: "Your development bot token (Used for testing in one server, rather than production)",
Skyler Grey75ea9172022-08-06 10:22:23 +01007 developmentGuildID: "Your development guild ID",
8 enableDevelopment: true,
9 token: "Your bot token",
Skyler Grey11236ba2022-08-08 21:13:33 +010010 managementGuildID: "Your management guild ID (Used for running management commands on the bot)",
Skyler Grey75ea9172022-08-06 10:22:23 +010011 owners: [],
12 verifySecret:
13 "If using verify, enter a code here which matches the secret sent back by your website. You can use a random code if you do not have one already. (Optional)",
14 mongoUrl: "Your Mongo connection string, e.g. mongodb://127.0.0.1:27017",
Skyler Grey11236ba2022-08-08 21:13:33 +010015 baseUrl: "Your website where buttons such as Verify and Role menu will link to, e.g. https://example.com",
Skyler Grey75ea9172022-08-06 10:22:23 +010016 pastebinApiKey: "An API key for pastebin (optional)",
17 pastebinUsername: "Your pastebin username (optional)",
18 pastebinPassword: "Your pastebin password (optional)"
pineafan1c837242022-08-04 22:04:24 +010019};
20
21const readline = readLine.createInterface({
22 input: process.stdin,
23 output: process.stdout
24});
25
26async function getInput(prompt: string): Promise<string> {
27 process.stdout.write(prompt);
28
29 const answer = await readline.question(prompt);
30 return answer.toString();
31}
32
Skyler Grey75ea9172022-08-06 10:22:23 +010033export default async function (walkthrough = false) {
pineafan1c837242022-08-04 22:04:24 +010034 if (walkthrough) {
Skyler Grey11236ba2022-08-08 21:13:33 +010035 console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values.");
36 console.log(" \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n");
pineafan1c837242022-08-04 22:04:24 +010037
38 // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > ");
39 // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i";
40 // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") {
41 // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`);
42 // await exec(toUse);
43 // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`);
44 // } else {
45 // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m");
46 // }
47 }
48
49 let json;
50 let out = true;
51 try {
52 json = JSON.parse(fs.readFileSync("./src/config/main.json", "utf8"));
53 } catch (e) {
54 console.log("\x1b[31mâš  No main.json found, creating one.");
Skyler Grey11236ba2022-08-08 21:13:33 +010055 console.log(" \x1b[2mYou can edit src/config/main.json directly using template written to the file.\x1b[0m\n");
pineafan1c837242022-08-04 22:04:24 +010056 out = false;
57 json = {};
58 }
59 for (const key in defaultDict) {
60 if (!json[key]) {
61 if (walkthrough) {
62 switch (key) {
Skyler Grey75ea9172022-08-06 10:22:23 +010063 case "enableDevelopment": {
64 json[key] =
65 (
66 (await getInput(
67 "\x1b[36mEnable development mode? This registers commands in a single server making it easier to test\x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > "
68 )) || "Y"
69 ).toLowerCase() === "y";
70 break;
pineafan1c837242022-08-04 22:04:24 +010071 }
Skyler Grey75ea9172022-08-06 10:22:23 +010072 case "owners": {
73 let chosen = "!";
74 const toWrite = [];
75 while (chosen !== "") {
76 chosen = await getInput(
77 "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "
78 );
79 if (chosen !== "") {
80 toWrite.push(chosen);
81 }
82 }
83 json[key] = toWrite;
84 break;
85 }
86 default: {
Skyler Grey11236ba2022-08-08 21:13:33 +010087 json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `);
Skyler Grey75ea9172022-08-06 10:22:23 +010088 }
pineafan1c837242022-08-04 22:04:24 +010089 }
Skyler Grey75ea9172022-08-06 10:22:23 +010090 } else {
91 json[key] = defaultDict[key];
pineafan1c837242022-08-04 22:04:24 +010092 }
pineafan1c837242022-08-04 22:04:24 +010093 }
94 }
Skyler Grey11236ba2022-08-08 21:13:33 +010095 if (walkthrough && !json.mongoUrl) json.mongoUrl = "mongodb://127.0.0.1:27017";
pineafan1c837242022-08-04 22:04:24 +010096 if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/";
97 if (!json.baseUrl.endsWith("/")) json.baseUrl += "/";
98 let hosts;
99 try {
100 hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n");
101 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 return console.log(
103 "\x1b[31mâš  No /etc/hosts found. Please ensure the file exists and is readable. (Windows is not supported, Mac and Linux users should not experience this error)"
104 );
pineafan1c837242022-08-04 22:04:24 +0100105 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100106 let localhost = hosts.find((line) => line.split(" ")[1] === "localhost");
107 if (localhost) {
108 localhost = localhost.split(" ")[0];
109 } else {
110 localhost = "127.0.0.1";
111 }
pineafan1c837242022-08-04 22:04:24 +0100112 json.mongoUrl = json.mongoUrl.replace("localhost", localhost);
113 json.baseUrl = json.baseUrl.replace("localhost", localhost);
114
115 fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4));
116
117 if (walkthrough) {
118 console.log("\x1b[32m✓ All properties added.\x1b[0m");
119 }
120 return out;
121}