/[sudobot]/trunk/setup.js
ViewVC logotype

Contents of /trunk/setup.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 443 - (show annotations)
Mon Jul 29 17:30:16 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: text/javascript
File size: 4086 byte(s)
feat: improve the setup script
1 #!/bin/node
2
3 /**
4 * setup.js -- the installer script
5 *
6 * This file is part of SudoBot.
7 *
8 * Copyright (C) 2021-2022 OSN Inc.
9 *
10 * SudoBot is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * SudoBot is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24 const path = require('path');
25 const fs = require('fs/promises');
26 const readline = require('readline');
27
28 const CONFIG_DIR = path.resolve(__dirname, 'config');
29 const { version } = require('./package.json');
30 const { existsSync } = require('fs');
31 const SAMPLE_CONFIG_PATH = path.resolve(CONFIG_DIR, 'sample-config.json');
32 const CONFIG_PATH = path.resolve(CONFIG_DIR, 'config.json');
33
34 const isSnowflake = text => /^\d+$/.test(text);
35
36 const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
37
38 const prompt = text => new Promise(resolve => {
39 rl.question(text, resolve);
40 });
41
42 const promptDefault = async (text, defaultValue) => {
43 const input = await prompt(text);
44
45 if (input.toString().trim() === '') {
46 return defaultValue;
47 }
48
49 return input;
50 };
51
52 const promptLoop = async (text, validator, defaultValue) => {
53 let fn = promptDefault;
54
55 if (defaultValue === undefined) {
56 fn = prompt;
57 }
58
59 const input = await fn(text, defaultValue);
60
61 if (!(await validator(input ?? ''))) {
62 return promptLoop(text, validator, defaultValue);
63 }
64
65 return input ?? '';
66 };
67
68 const snowflakeValidator = input => {
69 if (!isSnowflake(input)) {
70 console.log(`That's not a valid snowflake! Please enter a valid ID.`);
71 return false;
72 }
73
74 return true;
75 };
76
77 let prefix = '-', homeGuild = '', owners = [];
78
79 (async () => {
80 console.log(`SudoBot version ${version}`);
81 console.log(`Copyright (C) OSN Inc 2022`);
82 console.log(`Thanks for using SudoBot! We'll much appreciate if you star the repository on GitHub.\n`);
83
84 prefix = (await promptLoop(`What will be the bot prefix? [${prefix}]: `, input => {
85 if (input.trim().includes(' ')) {
86 console.log(`Prefixes must not contain spaces!`);
87 return false;
88 }
89
90 return true;
91 }, prefix)).trim();
92
93 homeGuild = await promptLoop(`What will be the Home/Support Guild ID?: `, snowflakeValidator);
94 owners = (await promptLoop(`Who will be the owner? Specify the owner user IDs separated with comma (,): `, input => {
95 const splitted = input.split(',');
96
97 for (const snowflake of splitted) {
98 if (!snowflakeValidator(snowflake)) {
99 console.log(`Invalid snowflake given! Make sure that the IDs are correctly given!`);
100 return false;
101 }
102 }
103
104 return true;
105 })).split(',').map(s => s.trim());
106
107 let config = Object.entries(JSON.parse((await fs.readFile(SAMPLE_CONFIG_PATH)).toString()));
108 config[1][0] = homeGuild;
109 config[1][1].prefix = prefix;
110 config = Object.fromEntries(config);
111
112 config.global.id = homeGuild;
113 config.global.owners = owners;
114
115 console.log(config);
116
117 if (existsSync(CONFIG_PATH)) {
118 const input = await promptDefault("The config file (config/config.json) already exists. Do you want to overwrite the file? [y/N]: ", "n");
119
120 if (input.trim().toLowerCase() !== "y" && input.trim().toLowerCase() !== "yes") {
121 console.log("Aborting setup.");
122 rl.close();
123 process.exit(1);
124 }
125 }
126
127 rl.close();
128
129 console.log("Setup complete!");
130 console.table([
131 {
132 prefix,
133 homeGuild
134 }
135 ]);
136 })().catch(console.error);

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26