pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 1 | import fs from "fs"; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 2 | import * as readLine from "node:readline/promises"; |
| 3 | |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 4 | const defaultDict: Record<string, string | string[] | boolean | Record<string, string | number | undefined>> = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 5 | developmentGuildID: "Your development guild ID", |
| 6 | enableDevelopment: true, |
| 7 | token: "Your bot token", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 8 | managementGuildID: "Your management guild ID (Used for running management commands on the bot)", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 9 | owners: [], |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 10 | commandsFolder: "Your built commands folder (usually dist/commands)", |
| 11 | eventsFolder: "Your built events folder (usually dist/events)", |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 12 | messageContextFolder: "Your built message context folder (usually dist/context/messages)", |
| 13 | userContextFolder: "Your built user context folder (usually dist/context/users)", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 14 | 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)", |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 16 | 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 Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 25 | authSource: "" |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 26 | }, |
PineaFan | 19dc9b8 | 2023-01-19 12:25:54 +0000 | [diff] [blame] | 27 | baseUrl: "Your website where buttons such as Verify and Role menu will link to, e.g. https://example.com/", |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 28 | clamAVSocket: "Your ClamAV socket file (optional)", |
| 29 | clamAVHost: "Your ClamAV host (optional)", |
| 30 | clamAVPort: "Your ClamAV port (optional)", |
TheCodedProf | 80ad854 | 2023-03-10 12:52:33 -0500 | [diff] [blame] | 31 | clamav: { |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 32 | socket: "", |
| 33 | host: "", |
| 34 | port: 0 |
| 35 | }, |
| 36 | githubPAT: "Your GitHub Personal Access Token (optional)", |
| 37 | suggestionChannel: "Your suggestion channel ID (optional)" |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | const readline = readLine.createInterface({ |
| 41 | input: process.stdin, |
| 42 | output: process.stdout |
| 43 | }); |
| 44 | |
| 45 | async 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 52 | export default async function (walkthrough = false) { |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 53 | if (walkthrough) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 54 | 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"); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 56 | |
| 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 | |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 68 | let json: typeof defaultDict; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 69 | let out = true; |
| 70 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 71 | json = (await import("./main.js")) as unknown as typeof defaultDict; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 72 | } catch (e) { |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 73 | 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"); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 75 | out = false; |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 76 | json = {} as typeof defaultDict; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 77 | } |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 78 | |
| 79 | if (Object.keys(json).length) { |
Skyler Grey | b86eb7e | 2023-06-14 14:03:18 +0200 | [diff] [blame] | 80 | if (json["token"] === defaultDict["token"]) { |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 81 | console.log("\x1b[31mâš No main.ts found, creating one."); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 82 | console.log( |
| 83 | " \x1b[2mYou can edit src/config/main.ts directly using template written to the file.\x1b[0m\n" |
| 84 | ); |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 85 | json = {}; |
| 86 | } |
| 87 | } |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 88 | |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 89 | for (const key in defaultDict) { |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 90 | if (Object.keys(json).includes(key)) { |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 91 | if (walkthrough) { |
| 92 | switch (key) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 93 | 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; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 101 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 102 | 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 | } |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 116 | case "mongoOptions": { |
| 117 | break; |
| 118 | } |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 119 | case "clamav": { |
| 120 | break; |
| 121 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 122 | default: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 123 | json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 124 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 125 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 126 | } else { |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 127 | json[key] = defaultDict[key]!; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 128 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 131 | if (walkthrough && !(json["mongoUrl"] ?? false)) json["mongoUrl"] = "mongodb://127.0.0.1:27017"; |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 132 | if (!((json["baseUrl"] as string | undefined) ?? "").endsWith("/")) (json["baseUrl"] as string) += "/"; |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 133 | const localhost = "127.0.0.1"; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 134 | json["mongoUrl"] = (json["mongoUrl"]! as string).replace("localhost", localhost); |
| 135 | json["baseUrl"] = (json["baseUrl"]! as string).replace("localhost", localhost); |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 136 | 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 Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 141 | authSource: json["authSource"] as string |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 142 | }; |
TheCodedProf | 35e7371 | 2023-03-10 17:35:35 -0500 | [diff] [blame] | 143 | json["clamav"] = { |
| 144 | socket: json["clamAVSocket"] as string | undefined, |
| 145 | host: json["clamAVHost"] as string | undefined, |
| 146 | port: json["clamAVPort"] as number | undefined |
TheCodedProf | ca29ebb | 2023-03-10 17:40:09 -0500 | [diff] [blame] | 147 | }; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 148 | |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 149 | fs.writeFileSync("./src/config/main.ts", "export default " + JSON.stringify(json, null, 4) + ";"); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 150 | |
| 151 | if (walkthrough) { |
| 152 | console.log("\x1b[32m✓ All properties added.\x1b[0m"); |
| 153 | } |
| 154 | return out; |
| 155 | } |