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