blob: bdd3fdb721e5663a81d77c77b63b42a53cd49757 [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": [],
PineappleFan7f8fe332022-07-30 22:06:51 +010012 "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)",
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": {
pineafan41d93562022-07-30 22:10:15 +010064 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;
pineafan6d537412022-07-28 12:35:33 +010065 } 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 += "/";
pineafan41d93562022-07-30 22:10:15 +010084 let hosts;
85 try {
86 hosts = fs.readFileSync('/etc/hosts', 'utf8').toString().split("\n");
87 } catch (e) {
88 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)")
89 }
pineafan6d537412022-07-28 12:35:33 +010090 let localhost = hosts.find(line => line.split(" ")[1] === "localhost");
91 if (localhost) { localhost = localhost.split(" ")[0]; }
92 else { localhost = "127.0.0.1" }
93 json.mongoUrl = json.mongoUrl.replace("localhost", localhost);
94 json.baseUrl = json.baseUrl.replace("localhost", localhost);
95
96 fs.writeFileSync("./src/config/main.json", JSON.stringify(json, null, 4))
97
98 if (walkthrough) {
99 console.log("\x1b[32m✓ All properties added.\x1b[0m")
100 }
101 return out;
102}