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

Diff of /trunk/setup.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 442 by rakin, Mon Jul 29 17:30:15 2024 UTC revision 443 by rakin, Mon Jul 29 17:30:16 2024 UTC
# Line 27  const readline = require('readline'); Line 27  const readline = require('readline');
27    
28  const CONFIG_DIR = path.resolve(__dirname, 'config');  const CONFIG_DIR = path.resolve(__dirname, 'config');
29  const { version } = require('./package.json');  const { version } = require('./package.json');
30    const { existsSync } = require('fs');
31  const SAMPLE_CONFIG_PATH = path.resolve(CONFIG_DIR, 'sample-config.json');  const SAMPLE_CONFIG_PATH = path.resolve(CONFIG_DIR, 'sample-config.json');
32    const CONFIG_PATH = path.resolve(CONFIG_DIR, 'config.json');
 let prefix = '-', homeGuild = '';  
33    
34  const isSnowflake = text => /^\d+$/.test(text);  const isSnowflake = text => /^\d+$/.test(text);
35    
# Line 56  const promptLoop = async (text, validato Line 56  const promptLoop = async (text, validato
56          fn = prompt;          fn = prompt;
57      }      }
58    
59      const input = await fn(text);      const input = await fn(text, defaultValue);
60    
61      if (!(await validator(input ?? ''))) {      if (!(await validator(input ?? ''))) {
62          return promptLoop(text, validator, defaultValue);          return promptLoop(text, validator, defaultValue);
# Line 74  const snowflakeValidator = input => { Line 74  const snowflakeValidator = input => {
74      return true;      return true;
75  };  };
76    
77    let prefix = '-', homeGuild = '', owners = [];
78    
79  (async () => {  (async () => {
80      console.log(`SudoBot version ${version}`);      console.log(`SudoBot version ${version}`);
81      console.log(`Copyright (C) OSN Inc 2022`);      console.log(`Copyright (C) OSN Inc 2022`);
# Line 87  const snowflakeValidator = input => { Line 89  const snowflakeValidator = input => {
89    
90          return true;          return true;
91      }, prefix)).trim();      }, prefix)).trim();
92    
93      homeGuild = await promptLoop(`What will be the Home/Support Guild ID?: `, snowflakeValidator);      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();      rl.close();
128    

Legend:
Removed from v.442  
changed lines
  Added in v.443

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26