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