blob: 3b977fbf821a13ed371daf80165f89f5e0ad091a [file] [log] [blame]
pineafan1c837242022-08-04 22:04:24 +01001import fs from "fs";
2// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Skyler Grey75ea9172022-08-06 10:22:23 +01003// @ts-expect-error
pineafan1c837242022-08-04 22:04:24 +01004import * as readLine from "node:readline/promises";
5
6const defaultDict = {
Skyler Grey75ea9172022-08-06 10:22:23 +01007 developmentToken:
8 "Your development bot token (Used for testing in one server, rather than production)",
9 developmentGuildID: "Your development guild ID",
10 enableDevelopment: true,
11 token: "Your bot token",
12 managementGuildID:
13 "Your management guild ID (Used for running management commands on the bot)",
14 owners: [],
15 verifySecret:
16 "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)",
17 mongoUrl: "Your Mongo connection string, e.g. mongodb://127.0.0.1:27017",
18 baseUrl:
19 "Your website where buttons such as Verify and Role menu will link to, e.g. https://example.com",
20 pastebinApiKey: "An API key for pastebin (optional)",
21 pastebinUsername: "Your pastebin username (optional)",
22 pastebinPassword: "Your pastebin password (optional)"
pineafan1c837242022-08-04 22:04:24 +010023};
24
25const readline = readLine.createInterface({
26 input: process.stdin,
27 output: process.stdout
28});
29
30async function getInput(prompt: string): Promise<string> {
31 process.stdout.write(prompt);
32
33 const answer = await readline.question(prompt);
34 return answer.toString();
35}
36
Skyler Grey75ea9172022-08-06 10:22:23 +010037export default async function (walkthrough = false) {
pineafan1c837242022-08-04 22:04:24 +010038 if (walkthrough) {
Skyler Grey75ea9172022-08-06 10:22:23 +010039 console.log(
40 "\x1b[33m🛈 Entering walkthrough mode for any missing values."
41 );
42 console.log(
43 " \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n"
44 );
pineafan1c837242022-08-04 22:04:24 +010045
46 // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > ");
47 // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i";
48 // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") {
49 // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`);
50 // await exec(toUse);
51 // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`);
52 // } else {
53 // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m");
54 // }
55 }
56
57 let json;
58 let out = true;
59 try {
60 json = JSON.parse(fs.readFileSync("./src/config/main.json", "utf8"));
61 } catch (e) {
62 console.log("\x1b[31mâš  No main.json found, creating one.");
Skyler Grey75ea9172022-08-06 10:22:23 +010063 console.log(
64 " \x1b[2mYou can edit src/config/main.json directly using template written to the file.\x1b[0m\n"
65 );
pineafan1c837242022-08-04 22:04:24 +010066 out = false;
67 json = {};
68 }
69 for (const key in defaultDict) {
70 if (!json[key]) {
71 if (walkthrough) {
72 switch (key) {
Skyler Grey75ea9172022-08-06 10:22:23 +010073 case "enableDevelopment": {
74 json[key] =
75 (
76 (await getInput(
77 "\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) > "
78 )) || "Y"
79 ).toLowerCase() === "y";
80 break;
pineafan1c837242022-08-04 22:04:24 +010081 }
Skyler Grey75ea9172022-08-06 10:22:23 +010082 case "owners": {
83 let chosen = "!";
84 const toWrite = [];
85 while (chosen !== "") {
86 chosen = await getInput(
87 "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "
88 );
89 if (chosen !== "") {
90 toWrite.push(chosen);
91 }
92 }
93 json[key] = toWrite;
94 break;
95 }
96 default: {
97 json[key] = await getInput(
98 `\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `
99 );
100 }
pineafan1c837242022-08-04 22:04:24 +0100101 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 } else {
103 json[key] = defaultDict[key];
pineafan1c837242022-08-04 22:04:24 +0100104 }
pineafan1c837242022-08-04 22:04:24 +0100105 }
106 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 if (walkthrough && !json.mongoUrl)
108 json.mongoUrl = "mongodb://127.0.0.1:27017";
pineafan1c837242022-08-04 22:04:24 +0100109 if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/";
110 if (!json.baseUrl.endsWith("/")) json.baseUrl += "/";
111 let hosts;
112 try {
113 hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n");
114 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 return console.log(
116 "\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)"
117 );
pineafan1c837242022-08-04 22:04:24 +0100118 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100119 let localhost = hosts.find((line) => line.split(" ")[1] === "localhost");
120 if (localhost) {
121 localhost = localhost.split(" ")[0];
122 } else {
123 localhost = "127.0.0.1";
124 }
pineafan1c837242022-08-04 22:04:24 +0100125 json.mongoUrl = json.mongoUrl.replace("localhost", localhost);
126 json.baseUrl = json.baseUrl.replace("localhost", localhost);
127
128 fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4));
129
130 if (walkthrough) {
131 console.log("\x1b[32m✓ All properties added.\x1b[0m");
132 }
133 return out;
134}