pineafan | 6d53741 | 2022-07-28 12:35:33 +0100 | [diff] [blame] | 1 | import fs from 'fs'; |
| 2 | import * as readLine from 'node:readline/promises'; |
| 3 | import { exec } from 'node:child_process'; |
| 4 | |
| 5 | const defaultDict = { |
| 6 | "token": "Your bot token", |
| 7 | "developmentToken": "Your development bot token", |
| 8 | "managementGuildID": "Your management guild ID", |
| 9 | "developmentGuildID": "Your development guild ID", |
| 10 | "enableDevelopment": true, |
| 11 | "owners": [], |
| 12 | "verifySecret": "If using verify, enter a code here which matches your website if needed", |
| 13 | "mongoUrl": "Your Mongo connection string, e.g. mongodb://127.0.0.1:27017", |
| 14 | "baseUrl": "Your website where buttons such as Verify will link to, e.g. https://example.com", |
| 15 | "pastebinApiKey": "An API key for pastebin (optional)", |
| 16 | "pastebinUsername": "Your pastebin username (optional)", |
| 17 | "pastebinPassword": "Your pastebin password (optional)", |
| 18 | } |
| 19 | |
| 20 | const readline = readLine.createInterface({ |
| 21 | input: process.stdin, |
| 22 | output: process.stdout, |
| 23 | }); |
| 24 | |
| 25 | async function getInput(prompt) { |
| 26 | process.stdout.write(prompt); |
| 27 | |
| 28 | const answer = await readline.question(prompt); |
| 29 | return answer.toString(); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | export default async function(walkthrough = false) { |
| 34 | if (walkthrough) { |
| 35 | console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values."); |
| 36 | console.log(" \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n"); |
| 37 | |
| 38 | // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > "); |
| 39 | // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i"; |
| 40 | // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") { |
| 41 | // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`); |
| 42 | // await exec(toUse); |
| 43 | // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`); |
| 44 | // } else { |
| 45 | // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m"); |
| 46 | // } |
| 47 | } |
| 48 | |
| 49 | let json; |
| 50 | let out = true |
| 51 | try { |
| 52 | json = JSON.parse(fs.readFileSync('./src/config/main.json', 'utf8')); |
| 53 | } catch (e) { |
| 54 | console.log("\x1b[31mâš No main.json found, creating one.") |
| 55 | console.log(" \x1b[2mYou can edit src/config/main.json directly using template written to the file.\x1b[0m\n") |
| 56 | out = false |
| 57 | json = {} |
| 58 | } |
| 59 | for (const key in defaultDict) { |
| 60 | if (!json[key]) { |
| 61 | if (walkthrough) { |
| 62 | switch (key) { |
| 63 | case "enableDevelopment": { |
| 64 | json[key] = (await getInput("\x1b[36mEnable development mode? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > ") || "Y").toLowerCase() === "y"; break; |
| 65 | } case "owners": { |
| 66 | let chosen = "!"; |
| 67 | let toWrite = [] |
| 68 | while (chosen !== "") { |
| 69 | chosen = await getInput("\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "); |
| 70 | if (chosen !== "") { toWrite.push(chosen); } |
| 71 | } |
| 72 | json[key] = toWrite; break; |
| 73 | } default: { |
| 74 | json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | else { json[key] = defaultDict[key] } |
| 79 | } |
| 80 | } |
| 81 | if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/"; |
| 82 | if (!json.baseUrl.endsWith("/")) json.baseUrl += "/"; |
| 83 | let hosts = fs.readFileSync('/etc/hosts', 'utf8').toString().split("\n"); |
| 84 | let localhost = hosts.find(line => line.split(" ")[1] === "localhost"); |
| 85 | if (localhost) { localhost = localhost.split(" ")[0]; } |
| 86 | else { localhost = "127.0.0.1" } |
| 87 | json.mongoUrl = json.mongoUrl.replace("localhost", localhost); |
| 88 | json.baseUrl = json.baseUrl.replace("localhost", localhost); |
| 89 | |
| 90 | fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4)) |
| 91 | |
| 92 | if (walkthrough) { |
| 93 | console.log("\x1b[32m✓ All properties added.\x1b[0m") |
| 94 | } |
| 95 | return out; |
| 96 | } |