blob: 056079687c0cc291f69461c3ec184db5bbab84c1 [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
TheCodedProffaae5332023-03-01 18:16:05 -05004const defaultDict: Record<string, string | string[] | boolean | Record<string, string>> = {
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)",
TheCodedProffaae5332023-03-01 18:16:05 -050017 mongoUsername: "Your Mongo username (optional)",
18 mongoPassword: "Your Mongo password (optional)",
19 mongoDatabase: "Your Mongo database name (optional, e.g. Nucleus)",
20 mongoHost: "Your Mongo host (optional, e.g. localhost:27017)",
21 mongoOptions: {
22 username: "",
23 password: "",
24 database: "",
25 host: "",
Skyler Greyda16adf2023-03-05 10:22:12 +000026 authSource: ""
TheCodedProffaae5332023-03-01 18:16:05 -050027 },
PineaFan19dc9b82023-01-19 12:25:54 +000028 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 +010029 pastebinApiKey: "An API key for pastebin (optional)",
30 pastebinUsername: "Your pastebin username (optional)",
pineafan3a02ea32022-08-11 21:35:04 +010031 pastebinPassword: "Your pastebin password (optional)",
32 rapidApiKey: "Your RapidAPI key (optional), used for Unscan"
pineafan1c837242022-08-04 22:04:24 +010033};
34
35const readline = readLine.createInterface({
36 input: process.stdin,
37 output: process.stdout
38});
39
40async function getInput(prompt: string): Promise<string> {
41 process.stdout.write(prompt);
42
43 const answer = await readline.question(prompt);
44 return answer.toString();
45}
46
Skyler Grey75ea9172022-08-06 10:22:23 +010047export default async function (walkthrough = false) {
pineafan1c837242022-08-04 22:04:24 +010048 if (walkthrough) {
Skyler Grey11236ba2022-08-08 21:13:33 +010049 console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values.");
50 console.log(" \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n");
pineafan1c837242022-08-04 22:04:24 +010051
52 // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > ");
53 // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i";
54 // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") {
55 // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`);
56 // await exec(toUse);
57 // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`);
58 // } else {
59 // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m");
60 // }
61 }
62
pineafana2e39c72023-02-21 18:37:32 +000063 let json: typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010064 let out = true;
65 try {
Skyler Greyda16adf2023-03-05 10:22:12 +000066 json = (await import("./main.js")) as unknown as typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010067 } catch (e) {
pineafana2e39c72023-02-21 18:37:32 +000068 console.log("\x1b[31mâš  No main.ts found, creating one.");
69 console.log(" \x1b[2mYou can edit src/config/main.ts directly using template written to the file.\x1b[0m\n");
pineafan1c837242022-08-04 22:04:24 +010070 out = false;
pineafana2e39c72023-02-21 18:37:32 +000071 json = {} as typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010072 }
pineafana2e39c72023-02-21 18:37:32 +000073
74 if (Object.keys(json).length) {
75 if (json["token"] === defaultDict["token"] || json["developmentToken"] === defaultDict["developmentToken"]) {
76 console.log("\x1b[31mâš  No main.ts found, creating one.");
Skyler Greyda16adf2023-03-05 10:22:12 +000077 console.log(
78 " \x1b[2mYou can edit src/config/main.ts directly using template written to the file.\x1b[0m\n"
79 );
PineaFan5d98a4b2023-01-19 16:15:47 +000080 json = {};
81 }
82 }
pineafana2e39c72023-02-21 18:37:32 +000083
pineafan1c837242022-08-04 22:04:24 +010084 for (const key in defaultDict) {
pineafana2e39c72023-02-21 18:37:32 +000085 if (Object.keys(json).includes(key)) {
pineafan1c837242022-08-04 22:04:24 +010086 if (walkthrough) {
87 switch (key) {
Skyler Grey75ea9172022-08-06 10:22:23 +010088 case "enableDevelopment": {
89 json[key] =
90 (
91 (await getInput(
92 "\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) > "
93 )) || "Y"
94 ).toLowerCase() === "y";
95 break;
pineafan1c837242022-08-04 22:04:24 +010096 }
Skyler Grey75ea9172022-08-06 10:22:23 +010097 case "owners": {
98 let chosen = "!";
99 const toWrite = [];
100 while (chosen !== "") {
101 chosen = await getInput(
102 "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "
103 );
104 if (chosen !== "") {
105 toWrite.push(chosen);
106 }
107 }
108 json[key] = toWrite;
109 break;
110 }
TheCodedProffaae5332023-03-01 18:16:05 -0500111 case "mongoOptions": {
112 break;
113 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 default: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100115 json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `);
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 }
pineafan1c837242022-08-04 22:04:24 +0100117 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100118 } else {
pineafana2e39c72023-02-21 18:37:32 +0000119 json[key] = defaultDict[key]!;
pineafan1c837242022-08-04 22:04:24 +0100120 }
pineafan1c837242022-08-04 22:04:24 +0100121 }
122 }
pineafana2e39c72023-02-21 18:37:32 +0000123 if (walkthrough && !(json["mongoUrl"] ?? false)) json["mongoUrl"] = "mongodb://127.0.0.1:27017";
TheCodedProffaae5332023-03-01 18:16:05 -0500124 if (!((json["baseUrl"] as string | undefined) ?? "").endsWith("/")) (json["baseUrl"] as string) += "/";
pineafan1c837242022-08-04 22:04:24 +0100125 let hosts;
126 try {
127 hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n");
128 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 return console.log(
130 "\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)"
131 );
pineafan1c837242022-08-04 22:04:24 +0100132 }
pineafana2e39c72023-02-21 18:37:32 +0000133 let localhost: string | undefined = hosts.find((line) => line.split(" ")[1] === "localhost");
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 if (localhost) {
135 localhost = localhost.split(" ")[0];
136 } else {
137 localhost = "127.0.0.1";
138 }
pineafana2e39c72023-02-21 18:37:32 +0000139 json["mongoUrl"] = (json["mongoUrl"]! as string).replace("localhost", localhost!);
140 json["baseUrl"] = (json["baseUrl"]! as string).replace("localhost", localhost!);
TheCodedProffaae5332023-03-01 18:16:05 -0500141 json["mongoOptions"] = {
142 username: json["username"] as string,
143 password: json["password"] as string,
144 database: json["database"] as string,
145 host: json["host"] as string,
Skyler Greyda16adf2023-03-05 10:22:12 +0000146 authSource: json["authSource"] as string
TheCodedProffaae5332023-03-01 18:16:05 -0500147 };
pineafan1c837242022-08-04 22:04:24 +0100148
pineafana2e39c72023-02-21 18:37:32 +0000149 fs.writeFileSync("./src/config/main.ts", "export default " + JSON.stringify(json, null, 4) + ";");
pineafan1c837242022-08-04 22:04:24 +0100150
151 if (walkthrough) {
152 console.log("\x1b[32m✓ All properties added.\x1b[0m");
153 }
154 return out;
155}