blob: b63debdea8593d089e7db71cb53e82f73729fedc [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
TheCodedProf80ad8542023-03-10 12:52:33 -05004const defaultDict: Record<string, string | string[] | boolean | Record<string, string | number>> = {
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)",
TheCodedProf80ad8542023-03-10 12:52:33 -050032 rapidApiKey: "Your RapidAPI key (optional), used for Unscan",
33 clamav: {
34 socket: "Your ClamAV socket file (optional)",
35 host: "Your ClamAV host (optional)",
36 port: "Your ClamAV port (optional)"
37 }
pineafan1c837242022-08-04 22:04:24 +010038};
39
40const readline = readLine.createInterface({
41 input: process.stdin,
42 output: process.stdout
43});
44
45async function getInput(prompt: string): Promise<string> {
46 process.stdout.write(prompt);
47
48 const answer = await readline.question(prompt);
49 return answer.toString();
50}
51
Skyler Grey75ea9172022-08-06 10:22:23 +010052export default async function (walkthrough = false) {
pineafan1c837242022-08-04 22:04:24 +010053 if (walkthrough) {
Skyler Grey11236ba2022-08-08 21:13:33 +010054 console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values.");
55 console.log(" \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n");
pineafan1c837242022-08-04 22:04:24 +010056
57 // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > ");
58 // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i";
59 // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") {
60 // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`);
61 // await exec(toUse);
62 // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`);
63 // } else {
64 // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m");
65 // }
66 }
67
pineafana2e39c72023-02-21 18:37:32 +000068 let json: typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010069 let out = true;
70 try {
Skyler Greyda16adf2023-03-05 10:22:12 +000071 json = (await import("./main.js")) as unknown as typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010072 } catch (e) {
pineafana2e39c72023-02-21 18:37:32 +000073 console.log("\x1b[31mâš  No main.ts found, creating one.");
74 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 +010075 out = false;
pineafana2e39c72023-02-21 18:37:32 +000076 json = {} as typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010077 }
pineafana2e39c72023-02-21 18:37:32 +000078
79 if (Object.keys(json).length) {
80 if (json["token"] === defaultDict["token"] || json["developmentToken"] === defaultDict["developmentToken"]) {
81 console.log("\x1b[31mâš  No main.ts found, creating one.");
Skyler Greyda16adf2023-03-05 10:22:12 +000082 console.log(
83 " \x1b[2mYou can edit src/config/main.ts directly using template written to the file.\x1b[0m\n"
84 );
PineaFan5d98a4b2023-01-19 16:15:47 +000085 json = {};
86 }
87 }
pineafana2e39c72023-02-21 18:37:32 +000088
pineafan1c837242022-08-04 22:04:24 +010089 for (const key in defaultDict) {
pineafana2e39c72023-02-21 18:37:32 +000090 if (Object.keys(json).includes(key)) {
pineafan1c837242022-08-04 22:04:24 +010091 if (walkthrough) {
92 switch (key) {
Skyler Grey75ea9172022-08-06 10:22:23 +010093 case "enableDevelopment": {
94 json[key] =
95 (
96 (await getInput(
97 "\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) > "
98 )) || "Y"
99 ).toLowerCase() === "y";
100 break;
pineafan1c837242022-08-04 22:04:24 +0100101 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 case "owners": {
103 let chosen = "!";
104 const toWrite = [];
105 while (chosen !== "") {
106 chosen = await getInput(
107 "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "
108 );
109 if (chosen !== "") {
110 toWrite.push(chosen);
111 }
112 }
113 json[key] = toWrite;
114 break;
115 }
TheCodedProffaae5332023-03-01 18:16:05 -0500116 case "mongoOptions": {
117 break;
118 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100119 default: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100120 json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `);
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 }
pineafan1c837242022-08-04 22:04:24 +0100122 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100123 } else {
pineafana2e39c72023-02-21 18:37:32 +0000124 json[key] = defaultDict[key]!;
pineafan1c837242022-08-04 22:04:24 +0100125 }
pineafan1c837242022-08-04 22:04:24 +0100126 }
127 }
pineafana2e39c72023-02-21 18:37:32 +0000128 if (walkthrough && !(json["mongoUrl"] ?? false)) json["mongoUrl"] = "mongodb://127.0.0.1:27017";
TheCodedProffaae5332023-03-01 18:16:05 -0500129 if (!((json["baseUrl"] as string | undefined) ?? "").endsWith("/")) (json["baseUrl"] as string) += "/";
pineafan1c837242022-08-04 22:04:24 +0100130 let hosts;
131 try {
132 hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n");
133 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 return console.log(
135 "\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)"
136 );
pineafan1c837242022-08-04 22:04:24 +0100137 }
pineafana2e39c72023-02-21 18:37:32 +0000138 let localhost: string | undefined = hosts.find((line) => line.split(" ")[1] === "localhost");
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 if (localhost) {
140 localhost = localhost.split(" ")[0];
141 } else {
142 localhost = "127.0.0.1";
143 }
pineafana2e39c72023-02-21 18:37:32 +0000144 json["mongoUrl"] = (json["mongoUrl"]! as string).replace("localhost", localhost!);
145 json["baseUrl"] = (json["baseUrl"]! as string).replace("localhost", localhost!);
TheCodedProffaae5332023-03-01 18:16:05 -0500146 json["mongoOptions"] = {
147 username: json["username"] as string,
148 password: json["password"] as string,
149 database: json["database"] as string,
150 host: json["host"] as string,
Skyler Greyda16adf2023-03-05 10:22:12 +0000151 authSource: json["authSource"] as string
TheCodedProffaae5332023-03-01 18:16:05 -0500152 };
pineafan1c837242022-08-04 22:04:24 +0100153
pineafana2e39c72023-02-21 18:37:32 +0000154 fs.writeFileSync("./src/config/main.ts", "export default " + JSON.stringify(json, null, 4) + ";");
pineafan1c837242022-08-04 22:04:24 +0100155
156 if (walkthrough) {
157 console.log("\x1b[32m✓ All properties added.\x1b[0m");
158 }
159 return out;
160}