blob: c385c86fdac86438f6985d4c5aad1d908fdd1a60 [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 Grey75ea9172022-08-06 10:22:23 +01005 developmentGuildID: "Your development guild ID",
6 enableDevelopment: true,
7 token: "Your bot token",
Skyler Grey11236ba2022-08-08 21:13:33 +01008 managementGuildID: "Your management guild ID (Used for running management commands on the bot)",
Skyler Grey75ea9172022-08-06 10:22:23 +01009 owners: [],
PineaFan100df682023-01-02 13:26:08 +000010 commandsFolder: "Your built commands folder (usually dist/commands)",
11 eventsFolder: "Your built events folder (usually dist/events)",
PineaFana00db1b2023-01-02 15:32:54 +000012 messageContextFolder: "Your built message context folder (usually dist/context/messages)",
13 userContextFolder: "Your built user context folder (usually dist/context/users)",
Skyler Grey75ea9172022-08-06 10:22:23 +010014 verifySecret:
15 "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 -050016 mongoUsername: "Your Mongo username (optional)",
17 mongoPassword: "Your Mongo password (optional)",
18 mongoDatabase: "Your Mongo database name (optional, e.g. Nucleus)",
19 mongoHost: "Your Mongo host (optional, e.g. localhost:27017)",
20 mongoOptions: {
21 username: "",
22 password: "",
23 database: "",
24 host: "",
Skyler Greyda16adf2023-03-05 10:22:12 +000025 authSource: ""
TheCodedProffaae5332023-03-01 18:16:05 -050026 },
PineaFan19dc9b82023-01-19 12:25:54 +000027 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 -050028 clamAVSocket: "Your ClamAV socket file (optional)",
29 clamAVHost: "Your ClamAV host (optional)",
30 clamAVPort: "Your ClamAV port (optional)",
TheCodedProf80ad8542023-03-10 12:52:33 -050031 clamav: {
TheCodedProf35e73712023-03-10 17:35:35 -050032 socket: "",
33 host: "",
34 port: 0
35 },
36 githubPAT: "Your GitHub Personal Access Token (optional)",
37 suggestionChannel: "Your suggestion channel ID (optional)"
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) {
Skyler Greyb86eb7e2023-06-14 14:03:18 +020080 if (json["token"] === defaultDict["token"]) {
pineafana2e39c72023-02-21 18:37:32 +000081 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 }
TheCodedProf35e73712023-03-10 17:35:35 -0500119 case "clamav": {
120 break;
121 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100122 default: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100123 json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `);
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 }
pineafan1c837242022-08-04 22:04:24 +0100125 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100126 } else {
pineafana2e39c72023-02-21 18:37:32 +0000127 json[key] = defaultDict[key]!;
pineafan1c837242022-08-04 22:04:24 +0100128 }
pineafan1c837242022-08-04 22:04:24 +0100129 }
130 }
pineafana2e39c72023-02-21 18:37:32 +0000131 if (walkthrough && !(json["mongoUrl"] ?? false)) json["mongoUrl"] = "mongodb://127.0.0.1:27017";
TheCodedProffaae5332023-03-01 18:16:05 -0500132 if (!((json["baseUrl"] as string | undefined) ?? "").endsWith("/")) (json["baseUrl"] as string) += "/";
TheCodedProfca29ebb2023-03-10 17:40:09 -0500133 const localhost = "127.0.0.1";
TheCodedProf35e73712023-03-10 17:35:35 -0500134 json["mongoUrl"] = (json["mongoUrl"]! as string).replace("localhost", localhost);
135 json["baseUrl"] = (json["baseUrl"]! as string).replace("localhost", localhost);
TheCodedProffaae5332023-03-01 18:16:05 -0500136 json["mongoOptions"] = {
137 username: json["username"] as string,
138 password: json["password"] as string,
139 database: json["database"] as string,
140 host: json["host"] as string,
Skyler Greyda16adf2023-03-05 10:22:12 +0000141 authSource: json["authSource"] as string
TheCodedProffaae5332023-03-01 18:16:05 -0500142 };
TheCodedProf35e73712023-03-10 17:35:35 -0500143 json["clamav"] = {
144 socket: json["clamAVSocket"] as string | undefined,
145 host: json["clamAVHost"] as string | undefined,
146 port: json["clamAVPort"] as number | undefined
TheCodedProfca29ebb2023-03-10 17:40:09 -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}