blob: 042d1732da73f5bfd84ebbbcb5fda7ca755e638a [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
TheCodedProf35e73712023-03-10 17:35:35 -05004const defaultDict: Record<string, string | string[] | boolean | Record<string, string | number | undefined>> = {
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/",
TheCodedProf35e73712023-03-10 17:35:35 -050029 clamAVSocket: "Your ClamAV socket file (optional)",
30 clamAVHost: "Your ClamAV host (optional)",
31 clamAVPort: "Your ClamAV port (optional)",
TheCodedProf80ad8542023-03-10 12:52:33 -050032 clamav: {
TheCodedProf35e73712023-03-10 17:35:35 -050033 socket: "",
34 host: "",
35 port: 0
36 },
37 githubPAT: "Your GitHub Personal Access Token (optional)",
38 suggestionChannel: "Your suggestion channel ID (optional)"
pineafan1c837242022-08-04 22:04:24 +010039};
40
41const readline = readLine.createInterface({
42 input: process.stdin,
43 output: process.stdout
44});
45
46async function getInput(prompt: string): Promise<string> {
47 process.stdout.write(prompt);
48
49 const answer = await readline.question(prompt);
50 return answer.toString();
51}
52
Skyler Grey75ea9172022-08-06 10:22:23 +010053export default async function (walkthrough = false) {
pineafan1c837242022-08-04 22:04:24 +010054 if (walkthrough) {
Skyler Grey11236ba2022-08-08 21:13:33 +010055 console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values.");
56 console.log(" \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n");
pineafan1c837242022-08-04 22:04:24 +010057
58 // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > ");
59 // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i";
60 // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") {
61 // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`);
62 // await exec(toUse);
63 // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`);
64 // } else {
65 // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m");
66 // }
67 }
68
pineafana2e39c72023-02-21 18:37:32 +000069 let json: typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010070 let out = true;
71 try {
Skyler Greyda16adf2023-03-05 10:22:12 +000072 json = (await import("./main.js")) as unknown as typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010073 } catch (e) {
pineafana2e39c72023-02-21 18:37:32 +000074 console.log("\x1b[31mâš  No main.ts found, creating one.");
75 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 +010076 out = false;
pineafana2e39c72023-02-21 18:37:32 +000077 json = {} as typeof defaultDict;
pineafan1c837242022-08-04 22:04:24 +010078 }
pineafana2e39c72023-02-21 18:37:32 +000079
80 if (Object.keys(json).length) {
81 if (json["token"] === defaultDict["token"] || json["developmentToken"] === defaultDict["developmentToken"]) {
82 console.log("\x1b[31mâš  No main.ts found, creating one.");
Skyler Greyda16adf2023-03-05 10:22:12 +000083 console.log(
84 " \x1b[2mYou can edit src/config/main.ts directly using template written to the file.\x1b[0m\n"
85 );
PineaFan5d98a4b2023-01-19 16:15:47 +000086 json = {};
87 }
88 }
pineafana2e39c72023-02-21 18:37:32 +000089
pineafan1c837242022-08-04 22:04:24 +010090 for (const key in defaultDict) {
pineafana2e39c72023-02-21 18:37:32 +000091 if (Object.keys(json).includes(key)) {
pineafan1c837242022-08-04 22:04:24 +010092 if (walkthrough) {
93 switch (key) {
Skyler Grey75ea9172022-08-06 10:22:23 +010094 case "enableDevelopment": {
95 json[key] =
96 (
97 (await getInput(
98 "\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) > "
99 )) || "Y"
100 ).toLowerCase() === "y";
101 break;
pineafan1c837242022-08-04 22:04:24 +0100102 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 case "owners": {
104 let chosen = "!";
105 const toWrite = [];
106 while (chosen !== "") {
107 chosen = await getInput(
108 "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "
109 );
110 if (chosen !== "") {
111 toWrite.push(chosen);
112 }
113 }
114 json[key] = toWrite;
115 break;
116 }
TheCodedProffaae5332023-03-01 18:16:05 -0500117 case "mongoOptions": {
118 break;
119 }
TheCodedProf35e73712023-03-10 17:35:35 -0500120 case "clamav": {
121 break;
122 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100123 default: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100124 json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `);
Skyler Grey75ea9172022-08-06 10:22:23 +0100125 }
pineafan1c837242022-08-04 22:04:24 +0100126 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100127 } else {
pineafana2e39c72023-02-21 18:37:32 +0000128 json[key] = defaultDict[key]!;
pineafan1c837242022-08-04 22:04:24 +0100129 }
pineafan1c837242022-08-04 22:04:24 +0100130 }
131 }
pineafana2e39c72023-02-21 18:37:32 +0000132 if (walkthrough && !(json["mongoUrl"] ?? false)) json["mongoUrl"] = "mongodb://127.0.0.1:27017";
TheCodedProffaae5332023-03-01 18:16:05 -0500133 if (!((json["baseUrl"] as string | undefined) ?? "").endsWith("/")) (json["baseUrl"] as string) += "/";
TheCodedProfca29ebb2023-03-10 17:40:09 -0500134 const localhost = "127.0.0.1";
TheCodedProf35e73712023-03-10 17:35:35 -0500135 json["mongoUrl"] = (json["mongoUrl"]! as string).replace("localhost", localhost);
136 json["baseUrl"] = (json["baseUrl"]! as string).replace("localhost", localhost);
TheCodedProffaae5332023-03-01 18:16:05 -0500137 json["mongoOptions"] = {
138 username: json["username"] as string,
139 password: json["password"] as string,
140 database: json["database"] as string,
141 host: json["host"] as string,
Skyler Greyda16adf2023-03-05 10:22:12 +0000142 authSource: json["authSource"] as string
TheCodedProffaae5332023-03-01 18:16:05 -0500143 };
TheCodedProf35e73712023-03-10 17:35:35 -0500144 json["clamav"] = {
145 socket: json["clamAVSocket"] as string | undefined,
146 host: json["clamAVHost"] as string | undefined,
147 port: json["clamAVPort"] as number | undefined
TheCodedProfca29ebb2023-03-10 17:40:09 -0500148 };
pineafan1c837242022-08-04 22:04:24 +0100149
pineafana2e39c72023-02-21 18:37:32 +0000150 fs.writeFileSync("./src/config/main.ts", "export default " + JSON.stringify(json, null, 4) + ";");
pineafan1c837242022-08-04 22:04:24 +0100151
152 if (walkthrough) {
153 console.log("\x1b[32m✓ All properties added.\x1b[0m");
154 }
155 return out;
156}