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 | 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: [], |
| 12 | verifySecret: |
| 13 | "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)", |
| 14 | mongoUrl: "Your Mongo connection string, e.g. mongodb://127.0.0.1:27017", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 15 | 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] | 16 | pastebinApiKey: "An API key for pastebin (optional)", |
| 17 | pastebinUsername: "Your pastebin username (optional)", |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 18 | pastebinPassword: "Your pastebin password (optional)", |
| 19 | rapidApiKey: "Your RapidAPI key (optional), used for Unscan" |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | const readline = readLine.createInterface({ |
| 23 | input: process.stdin, |
| 24 | output: process.stdout |
| 25 | }); |
| 26 | |
| 27 | async function getInput(prompt: string): Promise<string> { |
| 28 | process.stdout.write(prompt); |
| 29 | |
| 30 | const answer = await readline.question(prompt); |
| 31 | return answer.toString(); |
| 32 | } |
| 33 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 34 | export default async function (walkthrough = false) { |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 35 | if (walkthrough) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 36 | console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values."); |
| 37 | 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] | 38 | |
| 39 | // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > "); |
| 40 | // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i"; |
| 41 | // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") { |
| 42 | // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`); |
| 43 | // await exec(toUse); |
| 44 | // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`); |
| 45 | // } else { |
| 46 | // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m"); |
| 47 | // } |
| 48 | } |
| 49 | |
| 50 | let json; |
| 51 | let out = true; |
| 52 | try { |
| 53 | json = JSON.parse(fs.readFileSync("./src/config/main.json", "utf8")); |
| 54 | } catch (e) { |
| 55 | console.log("\x1b[31mâš No main.json found, creating one."); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 56 | console.log(" \x1b[2mYou can edit src/config/main.json directly using template written to the file.\x1b[0m\n"); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 57 | out = false; |
| 58 | json = {}; |
| 59 | } |
| 60 | for (const key in defaultDict) { |
| 61 | if (!json[key]) { |
| 62 | if (walkthrough) { |
| 63 | switch (key) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 64 | case "enableDevelopment": { |
| 65 | json[key] = |
| 66 | ( |
| 67 | (await getInput( |
| 68 | "\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) > " |
| 69 | )) || "Y" |
| 70 | ).toLowerCase() === "y"; |
| 71 | break; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 72 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | case "owners": { |
| 74 | let chosen = "!"; |
| 75 | const toWrite = []; |
| 76 | while (chosen !== "") { |
| 77 | chosen = await getInput( |
| 78 | "\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > " |
| 79 | ); |
| 80 | if (chosen !== "") { |
| 81 | toWrite.push(chosen); |
| 82 | } |
| 83 | } |
| 84 | json[key] = toWrite; |
| 85 | break; |
| 86 | } |
| 87 | default: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 88 | 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] | 89 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 90 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 91 | } else { |
| 92 | json[key] = defaultDict[key]; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 93 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 94 | } |
| 95 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 96 | if (walkthrough && !json.mongoUrl) json.mongoUrl = "mongodb://127.0.0.1:27017"; |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 97 | if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/"; |
| 98 | if (!json.baseUrl.endsWith("/")) json.baseUrl += "/"; |
| 99 | let hosts; |
| 100 | try { |
| 101 | hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n"); |
| 102 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 103 | return console.log( |
| 104 | "\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)" |
| 105 | ); |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 106 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 107 | let localhost = hosts.find((line) => line.split(" ")[1] === "localhost"); |
| 108 | if (localhost) { |
| 109 | localhost = localhost.split(" ")[0]; |
| 110 | } else { |
| 111 | localhost = "127.0.0.1"; |
| 112 | } |
pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 113 | json.mongoUrl = json.mongoUrl.replace("localhost", localhost); |
| 114 | json.baseUrl = json.baseUrl.replace("localhost", localhost); |
| 115 | |
| 116 | fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4)); |
| 117 | |
| 118 | if (walkthrough) { |
| 119 | console.log("\x1b[32m✓ All properties added.\x1b[0m"); |
| 120 | } |
| 121 | return out; |
| 122 | } |