blob: b00b3e4c3ba7d1d0b0ba63106308e3561639759a [file] [log] [blame]
pineafan1c837242022-08-04 22:04:24 +01001import fs from "fs";
pineafan1c837242022-08-04 22:04:24 +01002import * as readLine from "node:readline/promises";
3
PineaFan100df682023-01-02 13:26:08 +00004const defaultDict: Record<string, string | string[] | boolean> = {
Skyler Grey11236ba2022-08-08 21:13:33 +01005 developmentToken: "Your development bot token (Used for testing in one server, rather than production)",
Skyler Grey75ea9172022-08-06 10:22:23 +01006 developmentGuildID: "Your development guild ID",
7 enableDevelopment: true,
8 token: "Your bot token",
Skyler Grey11236ba2022-08-08 21:13:33 +01009 managementGuildID: "Your management guild ID (Used for running management commands on the bot)",
Skyler Grey75ea9172022-08-06 10:22:23 +010010 owners: [],
PineaFan100df682023-01-02 13:26:08 +000011 commandsFolder: "Your built commands folder (usually dist/commands)",
12 eventsFolder: "Your built events folder (usually dist/events)",
PineaFana00db1b2023-01-02 15:32:54 +000013 messageContextFolder: "Your built message context folder (usually dist/context/messages)",
14 userContextFolder: "Your built user context folder (usually dist/context/users)",
Skyler Grey75ea9172022-08-06 10:22:23 +010015 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",
PineaFan19dc9b82023-01-19 12:25:54 +000018 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 +010019 pastebinApiKey: "An API key for pastebin (optional)",
20 pastebinUsername: "Your pastebin username (optional)",
pineafan3a02ea32022-08-11 21:35:04 +010021 pastebinPassword: "Your pastebin password (optional)",
22 rapidApiKey: "Your RapidAPI key (optional), used for Unscan"
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 Grey11236ba2022-08-08 21:13:33 +010039 console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values.");
40 console.log(" \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n");
pineafan1c837242022-08-04 22:04:24 +010041
42 // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > ");
43 // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i";
44 // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") {
45 // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`);
46 // await exec(toUse);
47 // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`);
48 // } else {
49 // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m");
50 // }
51 }
52
53 let json;
54 let out = true;
55 try {
56 json = JSON.parse(fs.readFileSync("./src/config/main.json", "utf8"));
57 } catch (e) {
58 console.log("\x1b[31mâš  No main.json found, creating one.");
Skyler Grey11236ba2022-08-08 21:13:33 +010059 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 +010060 out = false;
61 json = {};
62 }
PineaFan5d98a4b2023-01-19 16:15:47 +000063 if (json) {
64 if (json.token === defaultDict["token"] || json.developmentToken === defaultDict["developmentToken"]) {
65 console.log("\x1b[31mâš  No main.json found, creating one.");
66 console.log(" \x1b[2mYou can edit src/config/main.json directly using template written to the file.\x1b[0m\n");
67 json = {};
68 }
69 }
pineafan1c837242022-08-04 22:04:24 +010070 for (const key in defaultDict) {
71 if (!json[key]) {
72 if (walkthrough) {
73 switch (key) {
Skyler Grey75ea9172022-08-06 10:22:23 +010074 case "enableDevelopment": {
75 json[key] =
76 (
77 (await getInput(
78 "\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) > "
79 )) || "Y"
80 ).toLowerCase() === "y";
81 break;
pineafan1c837242022-08-04 22:04:24 +010082 }
Skyler Grey75ea9172022-08-06 10:22:23 +010083 case "owners": {
84 let chosen = "!";
85 const toWrite = [];
86 while (chosen !== "") {
87 chosen = await getInput(
88 "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "
89 );
90 if (chosen !== "") {
91 toWrite.push(chosen);
92 }
93 }
94 json[key] = toWrite;
95 break;
96 }
97 default: {
Skyler Grey11236ba2022-08-08 21:13:33 +010098 json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `);
Skyler Grey75ea9172022-08-06 10:22:23 +010099 }
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 Grey11236ba2022-08-08 21:13:33 +0100106 if (walkthrough && !json.mongoUrl) json.mongoUrl = "mongodb://127.0.0.1:27017";
pineafan1c837242022-08-04 22:04:24 +0100107 if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/";
108 if (!json.baseUrl.endsWith("/")) json.baseUrl += "/";
109 let hosts;
110 try {
111 hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n");
112 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100113 return console.log(
114 "\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)"
115 );
pineafan1c837242022-08-04 22:04:24 +0100116 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 let localhost = hosts.find((line) => line.split(" ")[1] === "localhost");
118 if (localhost) {
119 localhost = localhost.split(" ")[0];
120 } else {
121 localhost = "127.0.0.1";
122 }
pineafan1c837242022-08-04 22:04:24 +0100123 json.mongoUrl = json.mongoUrl.replace("localhost", localhost);
124 json.baseUrl = json.baseUrl.replace("localhost", localhost);
125
126 fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4));
127
128 if (walkthrough) {
129 console.log("\x1b[32m✓ All properties added.\x1b[0m");
130 }
131 return out;
132}