blob: a80094ee38d7933b8c4973b250aeb48ddecacdbe [file] [log] [blame]
pineafana2e39c72023-02-21 18:37:32 +00001
pineafan1c837242022-08-04 22:04:24 +01002import fs from "fs";
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)",
PineaFana00db1b2023-01-02 15:32:54 +000014 messageContextFolder: "Your built message context folder (usually dist/context/messages)",
15 userContextFolder: "Your built user context folder (usually dist/context/users)",
Skyler Grey75ea9172022-08-06 10:22:23 +010016 verifySecret:
17 "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)",
18 mongoUrl: "Your Mongo connection string, e.g. mongodb://127.0.0.1:27017",
PineaFan19dc9b82023-01-19 12:25:54 +000019 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 +010020 pastebinApiKey: "An API key for pastebin (optional)",
21 pastebinUsername: "Your pastebin username (optional)",
pineafan3a02ea32022-08-11 21:35:04 +010022 pastebinPassword: "Your pastebin password (optional)",
23 rapidApiKey: "Your RapidAPI key (optional), used for Unscan"
pineafan1c837242022-08-04 22:04:24 +010024};
25
26const readline = readLine.createInterface({
27 input: process.stdin,
28 output: process.stdout
29});
30
31async function getInput(prompt: string): Promise<string> {
32 process.stdout.write(prompt);
33
34 const answer = await readline.question(prompt);
35 return answer.toString();
36}
37
Skyler Grey75ea9172022-08-06 10:22:23 +010038export default async function (walkthrough = false) {
pineafan1c837242022-08-04 22:04:24 +010039 if (walkthrough) {
Skyler Grey11236ba2022-08-08 21:13:33 +010040 console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values.");
41 console.log(" \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n");
pineafan1c837242022-08-04 22:04:24 +010042
43 // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > ");
44 // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i";
45 // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") {
46 // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`);
47 // await exec(toUse);
48 // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`);
49 // } else {
50 // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m");
51 // }
52 }
53
pineafana2e39c72023-02-21 18:37:32 +000054 let json: typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010055 let out = true;
56 try {
pineafana2e39c72023-02-21 18:37:32 +000057 json = await import("./main.js") as unknown as typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010058 } catch (e) {
pineafana2e39c72023-02-21 18:37:32 +000059 console.log("\x1b[31mâš  No main.ts found, creating one.");
60 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 +010061 out = false;
pineafana2e39c72023-02-21 18:37:32 +000062 json = {} as typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010063 }
pineafana2e39c72023-02-21 18:37:32 +000064
65 if (Object.keys(json).length) {
66 if (json["token"] === defaultDict["token"] || json["developmentToken"] === defaultDict["developmentToken"]) {
67 console.log("\x1b[31mâš  No main.ts found, creating one.");
68 console.log(" \x1b[2mYou can edit src/config/main.ts directly using template written to the file.\x1b[0m\n");
PineaFan5d98a4b2023-01-19 16:15:47 +000069 json = {};
70 }
71 }
pineafana2e39c72023-02-21 18:37:32 +000072
pineafan1c837242022-08-04 22:04:24 +010073 for (const key in defaultDict) {
pineafana2e39c72023-02-21 18:37:32 +000074 if (Object.keys(json).includes(key)) {
pineafan1c837242022-08-04 22:04:24 +010075 if (walkthrough) {
76 switch (key) {
Skyler Grey75ea9172022-08-06 10:22:23 +010077 case "enableDevelopment": {
78 json[key] =
79 (
80 (await getInput(
81 "\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) > "
82 )) || "Y"
83 ).toLowerCase() === "y";
84 break;
pineafan1c837242022-08-04 22:04:24 +010085 }
Skyler Grey75ea9172022-08-06 10:22:23 +010086 case "owners": {
87 let chosen = "!";
88 const toWrite = [];
89 while (chosen !== "") {
90 chosen = await getInput(
91 "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "
92 );
93 if (chosen !== "") {
94 toWrite.push(chosen);
95 }
96 }
97 json[key] = toWrite;
98 break;
99 }
100 default: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100101 json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `);
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 }
pineafan1c837242022-08-04 22:04:24 +0100103 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100104 } else {
pineafana2e39c72023-02-21 18:37:32 +0000105 json[key] = defaultDict[key]!;
pineafan1c837242022-08-04 22:04:24 +0100106 }
pineafan1c837242022-08-04 22:04:24 +0100107 }
108 }
pineafana2e39c72023-02-21 18:37:32 +0000109 if (walkthrough && !(json["mongoUrl"] ?? false)) json["mongoUrl"] = "mongodb://127.0.0.1:27017";
110 if (!((json["mongoUrl"] as string | undefined) ?? "").endsWith("/")) json["mongoUrl"] += "/";
111 if (!((json["baseUrl"] as string | undefined) ?? "").endsWith("/")) json["baseUrl"] += "/";
pineafan1c837242022-08-04 22:04:24 +0100112 let hosts;
113 try {
114 hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n");
115 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 return console.log(
117 "\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)"
118 );
pineafan1c837242022-08-04 22:04:24 +0100119 }
pineafana2e39c72023-02-21 18:37:32 +0000120 let localhost: string | undefined = hosts.find((line) => line.split(" ")[1] === "localhost");
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 if (localhost) {
122 localhost = localhost.split(" ")[0];
123 } else {
124 localhost = "127.0.0.1";
125 }
pineafana2e39c72023-02-21 18:37:32 +0000126 json["mongoUrl"] = (json["mongoUrl"]! as string).replace("localhost", localhost!);
127 json["baseUrl"] = (json["baseUrl"]! as string).replace("localhost", localhost!);
pineafan1c837242022-08-04 22:04:24 +0100128
pineafana2e39c72023-02-21 18:37:32 +0000129 fs.writeFileSync("./src/config/main.ts", "export default " + JSON.stringify(json, null, 4) + ";");
pineafan1c837242022-08-04 22:04:24 +0100130
131 if (walkthrough) {
132 console.log("\x1b[32m✓ All properties added.\x1b[0m");
133 }
134 return out;
135}