blob: 3890f66717805bc9b07650bfa9b96a476a0cb222 [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)",
pineafan3a02ea32022-08-11 21:35:04 +010018 pastebinPassword: "Your pastebin password (optional)",
19 rapidApiKey: "Your RapidAPI key (optional), used for Unscan"
pineafan1c837242022-08-04 22:04:24 +010020};
21
22const readline = readLine.createInterface({
23 input: process.stdin,
24 output: process.stdout
25});
26
27async function getInput(prompt: string): Promise<string> {
28 process.stdout.write(prompt);
29
30 const answer = await readline.question(prompt);
31 return answer.toString();
32}
33
Skyler Grey75ea9172022-08-06 10:22:23 +010034export default async function (walkthrough = false) {
pineafan1c837242022-08-04 22:04:24 +010035 if (walkthrough) {
Skyler Grey11236ba2022-08-08 21:13:33 +010036 console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values.");
37 console.log(" \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n");
pineafan1c837242022-08-04 22:04:24 +010038
39 // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > ");
40 // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i";
41 // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") {
42 // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`);
43 // await exec(toUse);
44 // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`);
45 // } else {
46 // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m");
47 // }
48 }
49
50 let json;
51 let out = true;
52 try {
53 json = JSON.parse(fs.readFileSync("./src/config/main.json", "utf8"));
54 } catch (e) {
55 console.log("\x1b[31mâš  No main.json found, creating one.");
Skyler Grey11236ba2022-08-08 21:13:33 +010056 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 +010057 out = false;
58 json = {};
59 }
60 for (const key in defaultDict) {
61 if (!json[key]) {
62 if (walkthrough) {
63 switch (key) {
Skyler Grey75ea9172022-08-06 10:22:23 +010064 case "enableDevelopment": {
65 json[key] =
66 (
67 (await getInput(
68 "\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) > "
69 )) || "Y"
70 ).toLowerCase() === "y";
71 break;
pineafan1c837242022-08-04 22:04:24 +010072 }
Skyler Grey75ea9172022-08-06 10:22:23 +010073 case "owners": {
74 let chosen = "!";
75 const toWrite = [];
76 while (chosen !== "") {
77 chosen = await getInput(
78 "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "
79 );
80 if (chosen !== "") {
81 toWrite.push(chosen);
82 }
83 }
84 json[key] = toWrite;
85 break;
86 }
87 default: {
Skyler Grey11236ba2022-08-08 21:13:33 +010088 json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `);
Skyler Grey75ea9172022-08-06 10:22:23 +010089 }
pineafan1c837242022-08-04 22:04:24 +010090 }
Skyler Grey75ea9172022-08-06 10:22:23 +010091 } else {
92 json[key] = defaultDict[key];
pineafan1c837242022-08-04 22:04:24 +010093 }
pineafan1c837242022-08-04 22:04:24 +010094 }
95 }
Skyler Grey11236ba2022-08-08 21:13:33 +010096 if (walkthrough && !json.mongoUrl) json.mongoUrl = "mongodb://127.0.0.1:27017";
pineafan1c837242022-08-04 22:04:24 +010097 if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/";
98 if (!json.baseUrl.endsWith("/")) json.baseUrl += "/";
99 let hosts;
100 try {
101 hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n");
102 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 return console.log(
104 "\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)"
105 );
pineafan1c837242022-08-04 22:04:24 +0100106 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 let localhost = hosts.find((line) => line.split(" ")[1] === "localhost");
108 if (localhost) {
109 localhost = localhost.split(" ")[0];
110 } else {
111 localhost = "127.0.0.1";
112 }
pineafan1c837242022-08-04 22:04:24 +0100113 json.mongoUrl = json.mongoUrl.replace("localhost", localhost);
114 json.baseUrl = json.baseUrl.replace("localhost", localhost);
115
116 fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4));
117
118 if (walkthrough) {
119 console.log("\x1b[32m✓ All properties added.\x1b[0m");
120 }
121 return out;
122}