blob: ddd164e3c207f19d4ccf24ed623251280d843484 [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
PineaFan100df682023-01-02 13:26:08 +00005const defaultDict: Record<string, string | string[] | boolean> = {
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: [],
PineaFan100df682023-01-02 13:26:08 +000012 commandsFolder: "Your built commands folder (usually dist/commands)",
13 eventsFolder: "Your built events folder (usually dist/events)",
Skyler Grey75ea9172022-08-06 10:22:23 +010014 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",
Skyler Grey11236ba2022-08-08 21:13:33 +010017 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 +010018 pastebinApiKey: "An API key for pastebin (optional)",
19 pastebinUsername: "Your pastebin username (optional)",
pineafan3a02ea32022-08-11 21:35:04 +010020 pastebinPassword: "Your pastebin password (optional)",
21 rapidApiKey: "Your RapidAPI key (optional), used for Unscan"
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 Grey11236ba2022-08-08 21:13:33 +010038 console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values.");
39 console.log(" \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n");
pineafan1c837242022-08-04 22:04:24 +010040
41 // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > ");
42 // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i";
43 // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") {
44 // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`);
45 // await exec(toUse);
46 // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`);
47 // } else {
48 // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m");
49 // }
50 }
51
52 let json;
53 let out = true;
54 try {
55 json = JSON.parse(fs.readFileSync("./src/config/main.json", "utf8"));
56 } catch (e) {
57 console.log("\x1b[31mâš  No main.json found, creating one.");
Skyler Grey11236ba2022-08-08 21:13:33 +010058 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 +010059 out = false;
60 json = {};
61 }
62 for (const key in defaultDict) {
63 if (!json[key]) {
64 if (walkthrough) {
65 switch (key) {
Skyler Grey75ea9172022-08-06 10:22:23 +010066 case "enableDevelopment": {
67 json[key] =
68 (
69 (await getInput(
70 "\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) > "
71 )) || "Y"
72 ).toLowerCase() === "y";
73 break;
pineafan1c837242022-08-04 22:04:24 +010074 }
Skyler Grey75ea9172022-08-06 10:22:23 +010075 case "owners": {
76 let chosen = "!";
77 const toWrite = [];
78 while (chosen !== "") {
79 chosen = await getInput(
80 "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "
81 );
82 if (chosen !== "") {
83 toWrite.push(chosen);
84 }
85 }
86 json[key] = toWrite;
87 break;
88 }
89 default: {
Skyler Grey11236ba2022-08-08 21:13:33 +010090 json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `);
Skyler Grey75ea9172022-08-06 10:22:23 +010091 }
pineafan1c837242022-08-04 22:04:24 +010092 }
Skyler Grey75ea9172022-08-06 10:22:23 +010093 } else {
94 json[key] = defaultDict[key];
pineafan1c837242022-08-04 22:04:24 +010095 }
pineafan1c837242022-08-04 22:04:24 +010096 }
97 }
Skyler Grey11236ba2022-08-08 21:13:33 +010098 if (walkthrough && !json.mongoUrl) json.mongoUrl = "mongodb://127.0.0.1:27017";
pineafan1c837242022-08-04 22:04:24 +010099 if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/";
100 if (!json.baseUrl.endsWith("/")) json.baseUrl += "/";
101 let hosts;
102 try {
103 hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n");
104 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 return console.log(
106 "\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)"
107 );
pineafan1c837242022-08-04 22:04:24 +0100108 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100109 let localhost = hosts.find((line) => line.split(" ")[1] === "localhost");
110 if (localhost) {
111 localhost = localhost.split(" ")[0];
112 } else {
113 localhost = "127.0.0.1";
114 }
pineafan1c837242022-08-04 22:04:24 +0100115 json.mongoUrl = json.mongoUrl.replace("localhost", localhost);
116 json.baseUrl = json.baseUrl.replace("localhost", localhost);
117
118 fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4));
119
120 if (walkthrough) {
121 console.log("\x1b[32m✓ All properties added.\x1b[0m");
122 }
123 return out;
124}