Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 1 | |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 2 | import fs from "fs"; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 3 | import * as readLine from "node:readline/promises"; |
| 4 | |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 5 | const defaultDict: Record<string, string | string[] | boolean | Record<string, string>> = { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 6 | developmentToken: "Your development bot token (Used for testing in one server, rather than production)", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 7 | developmentGuildID: "Your development guild ID", |
| 8 | enableDevelopment: true, |
| 9 | token: "Your bot token", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 10 | 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] | 11 | owners: [], |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 12 | commandsFolder: "Your built commands folder (usually dist/commands)", |
| 13 | eventsFolder: "Your built events folder (usually dist/events)", |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 14 | messageContextFolder: "Your built message context folder (usually dist/context/messages)", |
| 15 | userContextFolder: "Your built user context folder (usually dist/context/users)", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 16 | 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)", |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 18 | mongoUsername: "Your Mongo username (optional)", |
| 19 | mongoPassword: "Your Mongo password (optional)", |
| 20 | mongoDatabase: "Your Mongo database name (optional, e.g. Nucleus)", |
| 21 | mongoHost: "Your Mongo host (optional, e.g. localhost:27017)", |
| 22 | mongoOptions: { |
| 23 | username: "", |
| 24 | password: "", |
| 25 | database: "", |
| 26 | host: "", |
| 27 | authSource: "", |
| 28 | }, |
PineaFan | 19dc9b8 | 2023-01-19 12:25:54 +0000 | [diff] [blame] | 29 | baseUrl: "Your website where buttons such as Verify and Role menu will link to, e.g. https://example.com/", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 30 | pastebinApiKey: "An API key for pastebin (optional)", |
| 31 | pastebinUsername: "Your pastebin username (optional)", |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 32 | pastebinPassword: "Your pastebin password (optional)", |
| 33 | rapidApiKey: "Your RapidAPI key (optional), used for Unscan" |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | const readline = readLine.createInterface({ |
| 37 | input: process.stdin, |
| 38 | output: process.stdout |
| 39 | }); |
| 40 | |
| 41 | async function getInput(prompt: string): Promise<string> { |
| 42 | process.stdout.write(prompt); |
| 43 | |
| 44 | const answer = await readline.question(prompt); |
| 45 | return answer.toString(); |
| 46 | } |
| 47 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 48 | export default async function (walkthrough = false) { |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 49 | if (walkthrough) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 50 | console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values."); |
| 51 | 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] | 52 | |
| 53 | // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > "); |
| 54 | // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i"; |
| 55 | // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") { |
| 56 | // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`); |
| 57 | // await exec(toUse); |
| 58 | // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`); |
| 59 | // } else { |
| 60 | // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m"); |
| 61 | // } |
| 62 | } |
| 63 | |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 64 | let json: typeof defaultDict; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 65 | let out = true; |
| 66 | try { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 67 | json = await import("./main.js") as unknown as typeof defaultDict; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 68 | } catch (e) { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 69 | console.log("\x1b[31mâš No main.ts found, creating one."); |
| 70 | 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] | 71 | out = false; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 72 | json = {} as typeof defaultDict; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 73 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 74 | |
| 75 | if (Object.keys(json).length) { |
| 76 | if (json["token"] === defaultDict["token"] || json["developmentToken"] === defaultDict["developmentToken"]) { |
| 77 | console.log("\x1b[31mâš No main.ts found, creating one."); |
| 78 | console.log(" \x1b[2mYou can edit src/config/main.ts directly using template written to the file.\x1b[0m\n"); |
| 79 | json = {}; |
| 80 | } |
| 81 | } |
| 82 | |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 83 | for (const key in defaultDict) { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 84 | if (Object.keys(json).includes(key)) { |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 85 | if (walkthrough) { |
| 86 | switch (key) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 87 | case "enableDevelopment": { |
| 88 | json[key] = |
| 89 | ( |
| 90 | (await getInput( |
| 91 | "\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) > " |
| 92 | )) || "Y" |
| 93 | ).toLowerCase() === "y"; |
| 94 | break; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 95 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 96 | case "owners": { |
| 97 | let chosen = "!"; |
| 98 | const toWrite = []; |
| 99 | while (chosen !== "") { |
| 100 | chosen = await getInput( |
| 101 | "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > " |
| 102 | ); |
| 103 | if (chosen !== "") { |
| 104 | toWrite.push(chosen); |
| 105 | } |
| 106 | } |
| 107 | json[key] = toWrite; |
| 108 | break; |
| 109 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 110 | case "mongoOptions": { |
| 111 | break; |
| 112 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 113 | default: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 114 | 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] | 115 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 116 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 117 | } else { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 118 | json[key] = defaultDict[key]!; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 119 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 122 | if (walkthrough && !(json["mongoUrl"] ?? false)) json["mongoUrl"] = "mongodb://127.0.0.1:27017"; |
| 123 | if (!((json["baseUrl"] as string | undefined) ?? "").endsWith("/")) (json["baseUrl"] as string) += "/"; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 124 | let hosts; |
| 125 | try { |
| 126 | hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n"); |
| 127 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 128 | return console.log( |
| 129 | "\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)" |
| 130 | ); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 131 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 132 | let localhost: string | undefined = hosts.find((line) => line.split(" ")[1] === "localhost"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 133 | if (localhost) { |
| 134 | localhost = localhost.split(" ")[0]; |
| 135 | } else { |
| 136 | localhost = "127.0.0.1"; |
| 137 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 138 | json["mongoUrl"] = (json["mongoUrl"]! as string).replace("localhost", localhost!); |
| 139 | json["baseUrl"] = (json["baseUrl"]! as string).replace("localhost", localhost!); |
| 140 | json["mongoOptions"] = { |
| 141 | username: json["username"] as string, |
| 142 | password: json["password"] as string, |
| 143 | database: json["database"] as string, |
| 144 | host: json["host"] as string, |
| 145 | authSource: json["authSource"] as string, |
| 146 | }; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 147 | |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 148 | fs.writeFileSync("./src/config/main.ts", "export default " + JSON.stringify(json, null, 4) + ";"); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 149 | |
| 150 | if (walkthrough) { |
| 151 | console.log("\x1b[32m✓ All properties added.\x1b[0m"); |
| 152 | } |
| 153 | return out; |
| 154 | } |