| import fs from 'fs'; |
| import * as readLine from 'node:readline/promises'; |
| import { exec } from 'node:child_process'; |
| |
| const defaultDict = { |
| "developmentToken": "Your development bot token (Used for testing in one server, rather than production)", |
| "developmentGuildID": "Your development guild ID", |
| "enableDevelopment": true, |
| "token": "Your bot token", |
| "managementGuildID": "Your management guild ID (Used for running management commands on the bot)", |
| "owners": [], |
| "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)", |
| "mongoUrl": "Your Mongo connection string, e.g. mongodb://127.0.0.1:27017", |
| "baseUrl": "Your website where buttons such as Verify and Role menu will link to, e.g. https://example.com", |
| "pastebinApiKey": "An API key for pastebin (optional)", |
| "pastebinUsername": "Your pastebin username (optional)", |
| "pastebinPassword": "Your pastebin password (optional)", |
| } |
| |
| const readline = readLine.createInterface({ |
| input: process.stdin, |
| output: process.stdout, |
| }); |
| |
| async function getInput(prompt) { |
| process.stdout.write(prompt); |
| |
| const answer = await readline.question(prompt); |
| return answer.toString(); |
| } |
| |
| |
| export default async function(walkthrough = false) { |
| if (walkthrough) { |
| console.log("\x1b[33m🛈 Entering walkthrough mode for any missing values."); |
| console.log(" \x1b[2mIf you don't want to enter a value, just hit enter.\x1b[0m\n"); |
| |
| // let toUse = await getInput("\x1b[36m[Installing packages] Use Yarn or NPM? \x1b[0m(\x1b[32my\x1b[0m/\x1b[31mN\x1b[0m) > "); |
| // toUse = toUse.toLowerCase() === "y" ? "yarn install" : "npm i"; |
| // if ((await getInput(`\x1b[36m[Installing packages] Run ${toUse}? \x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > `)).toLowerCase() !== "n") { |
| // console.log(`\x1b[32m[Installing packages] Running ${toUse}...\x1b[0m`); |
| // await exec(toUse); |
| // console.log(`\x1b[32m[Installing packages] Installed\x1b[0m`); |
| // } else { |
| // console.log("\x1b[32m[Installing packages] Skipping...\x1b[0m"); |
| // } |
| } |
| |
| let json; |
| let out = true |
| try { |
| json = JSON.parse(fs.readFileSync('./src/config/main.json', 'utf8')); |
| } catch (e) { |
| console.log("\x1b[31mâš No main.json found, creating one.") |
| console.log(" \x1b[2mYou can edit src/config/main.json directly using template written to the file.\x1b[0m\n") |
| out = false |
| json = {} |
| } |
| for (const key in defaultDict) { |
| if (!json[key]) { |
| if (walkthrough) { |
| switch (key) { |
| case "enableDevelopment": { |
| json[key] = (await getInput("\x1b[36mEnable development mode? This redisters commands in a single server making it easier to test\x1b[0m(\x1b[32mY\x1b[0m/\x1b[31mn\x1b[0m) > ") || "Y").toLowerCase() === "y"; break; |
| } case "owners": { |
| let chosen = "!"; |
| let toWrite = [] |
| while (chosen !== "") { |
| chosen = await getInput("\x1b[36mEnter an owner ID \x1b[0m(\x1b[35mleave blank to finish\x1b[0m) > "); |
| if (chosen !== "") { toWrite.push(chosen); } |
| } |
| json[key] = toWrite; break; |
| } default: { |
| json[key] = await getInput(`\x1b[36m${key} \x1b[0m(\x1b[35m${defaultDict[key]}\x1b[0m) > `); |
| } |
| } |
| } |
| else { json[key] = defaultDict[key] } |
| } |
| } |
| if (walkthrough && !json.mongoUrl) json.mongoUrl = "mongodb://127.0.0.1:27017" |
| if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/"; |
| if (!json.baseUrl.endsWith("/")) json.baseUrl += "/"; |
| let hosts; |
| try { |
| hosts = fs.readFileSync('/etc/hosts', 'utf8').toString().split("\n"); |
| } catch (e) { |
| 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)") |
| } |
| let localhost = hosts.find(line => line.split(" ")[1] === "localhost"); |
| if (localhost) { localhost = localhost.split(" ")[0]; } |
| else { localhost = "127.0.0.1" } |
| json.mongoUrl = json.mongoUrl.replace("localhost", localhost); |
| json.baseUrl = json.baseUrl.replace("localhost", localhost); |
| |
| fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4)) |
| |
| if (walkthrough) { |
| console.log("\x1b[32m✓ All properties added.\x1b[0m") |
| } |
| return out; |
| } |