pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 1 | import fs from "fs"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 2 | // @ts-expect-error |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 3 | import * as readLine from "node:readline/promises"; |
| 4 | |
| 5 | const defaultDict = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 6 | developmentToken: |
| 7 | "Your development bot token (Used for testing in one server, rather than production)", |
| 8 | developmentGuildID: "Your development guild ID", |
| 9 | enableDevelopment: true, |
| 10 | token: "Your bot token", |
| 11 | managementGuildID: |
| 12 | "Your management guild ID (Used for running management commands on the bot)", |
| 13 | owners: [], |
| 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)", |
| 16 | mongoUrl: "Your Mongo connection string, e.g. mongodb://127.0.0.1:27017", |
| 17 | baseUrl: |
| 18 | "Your website where buttons such as Verify and Role menu will link to, e.g. https://example.com", |
| 19 | pastebinApiKey: "An API key for pastebin (optional)", |
| 20 | pastebinUsername: "Your pastebin username (optional)", |
| 21 | pastebinPassword: "Your pastebin password (optional)" |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | const readline = readLine.createInterface({ |
| 25 | input: process.stdin, |
| 26 | output: process.stdout |
| 27 | }); |
| 28 | |
| 29 | async function getInput(prompt: string): Promise<string> { |
| 30 | process.stdout.write(prompt); |
| 31 | |
| 32 | const answer = await readline.question(prompt); |
| 33 | return answer.toString(); |
| 34 | } |
| 35 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 36 | export default async function (walkthrough = false) { |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 37 | if (walkthrough) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 38 | console.log( |
| 39 | "\x1b[33m🛈 Entering walkthrough mode for any missing values." |
| 40 | ); |
| 41 | console.log( |
| 42 | " \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n" |
| 43 | ); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 44 | |
| 45 | // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > "); |
| 46 | // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i"; |
| 47 | // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") { |
| 48 | // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`); |
| 49 | // await exec(toUse); |
| 50 | // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`); |
| 51 | // } else { |
| 52 | // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m"); |
| 53 | // } |
| 54 | } |
| 55 | |
| 56 | let json; |
| 57 | let out = true; |
| 58 | try { |
| 59 | json = JSON.parse(fs.readFileSync("./src/config/main.json", "utf8")); |
| 60 | } catch (e) { |
| 61 | console.log("\x1b[31mâš No main.json found, creating one."); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 62 | console.log( |
| 63 | " \x1b[2mYou can edit src/config/main.json directly using template written to the file.\x1b[0m\n" |
| 64 | ); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 65 | out = false; |
| 66 | json = {}; |
| 67 | } |
| 68 | for (const key in defaultDict) { |
| 69 | if (!json[key]) { |
| 70 | if (walkthrough) { |
| 71 | switch (key) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 72 | case "enableDevelopment": { |
| 73 | json[key] = |
| 74 | ( |
| 75 | (await getInput( |
| 76 | "\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) > " |
| 77 | )) || "Y" |
| 78 | ).toLowerCase() === "y"; |
| 79 | break; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 80 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 81 | case "owners": { |
| 82 | let chosen = "!"; |
| 83 | const toWrite = []; |
| 84 | while (chosen !== "") { |
| 85 | chosen = await getInput( |
| 86 | "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > " |
| 87 | ); |
| 88 | if (chosen !== "") { |
| 89 | toWrite.push(chosen); |
| 90 | } |
| 91 | } |
| 92 | json[key] = toWrite; |
| 93 | break; |
| 94 | } |
| 95 | default: { |
| 96 | json[key] = await getInput( |
| 97 | `\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > ` |
| 98 | ); |
| 99 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 100 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 101 | } else { |
| 102 | json[key] = defaultDict[key]; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 103 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 106 | if (walkthrough && !json.mongoUrl) |
| 107 | json.mongoUrl = "mongodb://127.0.0.1:27017"; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 108 | if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/"; |
| 109 | if (!json.baseUrl.endsWith("/")) json.baseUrl += "/"; |
| 110 | let hosts; |
| 111 | try { |
| 112 | hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n"); |
| 113 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 114 | return console.log( |
| 115 | "\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)" |
| 116 | ); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 117 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 118 | let localhost = hosts.find((line) => line.split(" ")[1] === "localhost"); |
| 119 | if (localhost) { |
| 120 | localhost = localhost.split(" ")[0]; |
| 121 | } else { |
| 122 | localhost = "127.0.0.1"; |
| 123 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 124 | json.mongoUrl = json.mongoUrl.replace("localhost", localhost); |
| 125 | json.baseUrl = json.baseUrl.replace("localhost", localhost); |
| 126 | |
| 127 | fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4)); |
| 128 | |
| 129 | if (walkthrough) { |
| 130 | console.log("\x1b[32m✓ All properties added.\x1b[0m"); |
| 131 | } |
| 132 | return out; |
| 133 | } |