pineafan | 1c83724 | 2022-08-04 22:04:24 +0100 | [diff] [blame] | 1 | import fs from "fs"; |
| 2 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 3 | // @ts-ignore |
| 4 | import * as readLine from "node:readline/promises"; |
| 5 | |
| 6 | const defaultDict = { |
| 7 | "developmentToken": "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": "Your management guild ID (Used for running management commands on the bot)", |
| 12 | "owners": [], |
| 13 | "verifySecret": "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", |
| 15 | "baseUrl": "Your website where buttons such as Verify and Role menu will link to, e.g. https://example.com", |
| 16 | "pastebinApiKey": "An API key for pastebin (optional)", |
| 17 | "pastebinUsername": "Your pastebin username (optional)", |
| 18 | "pastebinPassword": "Your pastebin password (optional)" |
| 19 | }; |
| 20 | |
| 21 | const readline = readLine.createInterface({ |
| 22 | input: process.stdin, |
| 23 | output: process.stdout |
| 24 | }); |
| 25 | |
| 26 | async function getInput(prompt: string): Promise<string> { |
| 27 | process.stdout.write(prompt); |
| 28 | |
| 29 | const answer = await readline.question(prompt); |
| 30 | return answer.toString(); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | export default async function(walkthrough = false) { |
| 35 | if (walkthrough) { |
| 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"); |
| 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."); |
| 56 | console.log(" \x1b[2mYou can edit src/config/main.json directly using template written to the file.\x1b[0m\n"); |
| 57 | out = false; |
| 58 | json = {}; |
| 59 | } |
| 60 | for (const key in defaultDict) { |
| 61 | if (!json[key]) { |
| 62 | if (walkthrough) { |
| 63 | switch (key) { |
| 64 | case "enableDevelopment": { |
| 65 | json[key] = (await getInput("\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) > ") || "Y").toLowerCase() === "y"; break; |
| 66 | } case "owners": { |
| 67 | let chosen = "!"; |
| 68 | const toWrite = []; |
| 69 | while (chosen !== "") { |
| 70 | chosen = await getInput("\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "); |
| 71 | if (chosen !== "") { toWrite.push(chosen); } |
| 72 | } |
| 73 | json[key] = toWrite; break; |
| 74 | } default: { |
| 75 | json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | else { json[key] = defaultDict[key]; } |
| 80 | } |
| 81 | } |
| 82 | if (walkthrough && !json.mongoUrl) json.mongoUrl = "mongodb://127.0.0.1:27017"; |
| 83 | if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/"; |
| 84 | if (!json.baseUrl.endsWith("/")) json.baseUrl += "/"; |
| 85 | let hosts; |
| 86 | try { |
| 87 | hosts = fs.readFileSync("/etc/hosts", "utf8").toString().split("\n"); |
| 88 | } catch (e) { |
| 89 | return console.log("\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)"); |
| 90 | } |
| 91 | let localhost = hosts.find(line => line.split(" ")[1] === "localhost"); |
| 92 | if (localhost) { localhost = localhost.split(" ")[0]; } |
| 93 | else { localhost = "127.0.0.1"; } |
| 94 | json.mongoUrl = json.mongoUrl.replace("localhost", localhost); |
| 95 | json.baseUrl = json.baseUrl.replace("localhost", localhost); |
| 96 | |
| 97 | fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4)); |
| 98 | |
| 99 | if (walkthrough) { |
| 100 | console.log("\x1b[32m✓ All properties added.\x1b[0m"); |
| 101 | } |
| 102 | return out; |
| 103 | } |