blob: 67493381155d0769a90e7b6bfdf67e4336965827 [file] [log] [blame]
pineafan6d537412022-07-28 12:35:33 +01001import fs from 'fs';
2import * as readLine from 'node:readline/promises';
3import { exec } from 'node:child_process';
4
5const defaultDict = {
PineappleFana14b5fe2022-07-30 22:02:24 +01006 "developmentToken": "Your development bot token (Used for testing in one server, rather than production)",
pineafan6d537412022-07-28 12:35:33 +01007 "developmentGuildID": "Your development guild ID",
8 "enableDevelopment": true,
PineappleFana14b5fe2022-07-30 22:02:24 +01009 "token": "Your bot token",
10 "managementGuildID": "Your management guild ID (Used for running management commands on the bot)",
pineafan6d537412022-07-28 12:35:33 +010011 "owners": [],
PineappleFan0aadc1f2022-07-30 22:06:34 +010012 "verifySecret": "If using verify, enter a code here which matches the secret send back by your website. You can use a random code if you do not have one already. (Optional)",
pineafan6d537412022-07-28 12:35:33 +010013 "mongoUrl": "Your Mongo connection string, e.g. mongodb://127.0.0.1:27017",
PineappleFana14b5fe2022-07-30 22:02:24 +010014 "baseUrl": "Your website where buttons such as Verify and Role menu will link to, e.g. https://example.com",
pineafan6d537412022-07-28 12:35:33 +010015 "pastebinApiKey": "An API key for pastebin (optional)",
16 "pastebinUsername": "Your pastebin username (optional)",
17 "pastebinPassword": "Your pastebin password (optional)",
18}
19
20const readline = readLine.createInterface({
21 input: process.stdin,
22 output: process.stdout,
23});
24
25async function getInput(prompt) {
26 process.stdout.write(prompt);
27
28 const answer = await readline.question(prompt);
29 return answer.toString();
30}
31
32
33export 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 }
PineappleFana14b5fe2022-07-30 22:02:24 +010081 if (walkthrough && !json.mongoUrl) json.mongoUrl = "mongodb://127.0.0.1:27017"
pineafan6d537412022-07-28 12:35:33 +010082 if (!json.mongoUrl.endsWith("/")) json.mongoUrl += "/";
83 if (!json.baseUrl.endsWith("/")) json.baseUrl += "/";
84 let hosts = fs.readFileSync('/etc/hosts', 'utf8').toString().split("\n");
85 let localhost = hosts.find(line => line.split(" ")[1] === "localhost");
86 if (localhost) { localhost = localhost.split(" ")[0]; }
87 else { localhost = "127.0.0.1" }
88 json.mongoUrl = json.mongoUrl.replace("localhost", localhost);
89 json.baseUrl = json.baseUrl.replace("localhost", localhost);
90
91 fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4))
92
93 if (walkthrough) {
94 console.log("\x1b[32m✓ All properties added.\x1b[0m")
95 }
96 return out;
97}