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 SAMPLE_CONFIG_PATH = path.resolve(CONFIG_DIR, 'sample-config.json'); |
31 |
|
32 |
let prefix = '-', homeGuild = ''; |
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); |
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 |
(async () => { |
78 |
console.log(`SudoBot version ${version}`); |
79 |
console.log(`Copyright (C) OSN Inc 2022`); |
80 |
console.log(`Thanks for using SudoBot! We'll much appreciate if you star the repository on GitHub.\n`); |
81 |
|
82 |
prefix = (await promptLoop(`What will be the bot prefix? [${prefix}]: `, input => { |
83 |
if (input.trim().includes(' ')) { |
84 |
console.log(`Prefixes must not contain spaces!`); |
85 |
return false; |
86 |
} |
87 |
|
88 |
return true; |
89 |
}, prefix)).trim(); |
90 |
homeGuild = await promptLoop(`What will be the Home/Support Guild ID?: `, snowflakeValidator); |
91 |
|
92 |
rl.close(); |
93 |
|
94 |
console.log("Setup complete!"); |
95 |
console.table([ |
96 |
{ |
97 |
prefix, |
98 |
homeGuild |
99 |
} |
100 |
]); |
101 |
})().catch(console.error); |