|
| 1 | +const setup = require('./setup.js'); |
| 2 | +const { start } = require('./bot.js'); |
| 3 | +const chalk = require('chalk'); |
| 4 | + |
| 5 | +const printValues = function(values, text) { |
| 6 | + console.log(text ? text : 'Current values:'); |
| 7 | + for (var key in values) { |
| 8 | + console.log(` ${key} = \x1b[32m'${values[key]}'\x1b[0m`); |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +const startBot = function(values) { |
| 13 | + console.log(`${chalk.bgBlue("[INFO]")} ${chalk.blue("Starting bot... this may take a few seconds to start...")}`); |
| 14 | + var bot = start(values); |
| 15 | + bot.on('restart',() => { |
| 16 | + console.log('\nRestarting bot'); |
| 17 | + bot.destroy(); |
| 18 | + bot = start(values); |
| 19 | + }) |
| 20 | + var shutdown = function() { |
| 21 | + console.log(`${chalk.bgRed(`[STATUS]`)} ${chalk.red('Shutting down')}`); |
| 22 | + let destructor = bot.destroy(); |
| 23 | + if (destructor) { |
| 24 | + destructor.then(() => { |
| 25 | + process.exit(0); |
| 26 | + }).catch(console.error); |
| 27 | + } else { |
| 28 | + process.exit(0); |
| 29 | + } |
| 30 | + } |
| 31 | + process.on('SIGINT', shutdown); |
| 32 | + process.on('SIGTERM', shutdown); |
| 33 | +} |
| 34 | + |
| 35 | +if (process.argv.includes('-c') || process.argv.includes('--config')) { |
| 36 | + setup.loadValues().then((values) => { |
| 37 | + printValues(values); |
| 38 | + process.exit(0); |
| 39 | + }).catch((error) => { |
| 40 | + console.log('Unable to load saved values, reconfiguring all saved values again'); |
| 41 | + setup.createValues().then((values) => { |
| 42 | + setup.saveValues(values).then(() => { |
| 43 | + printValues(values, 'New values:'); |
| 44 | + process.exit(0); |
| 45 | + }).catch(console.error); |
| 46 | + }).catch(console.error); |
| 47 | + }) |
| 48 | +} else { |
| 49 | + console.log(`${chalk.bold.bgYellow(`[LOAD]`)} ${chalk.bold.yellow('Attempting to load enviroment values...')}`); |
| 50 | + setup.loadValues().then((values) => { |
| 51 | + startBot(values); |
| 52 | + }).catch((error) => { |
| 53 | + console.error(error); |
| 54 | + setup.createValues().then((values) => { |
| 55 | + setup.saveValues(values).then(() => { |
| 56 | + startBot(values); |
| 57 | + }).catch(console.error); |
| 58 | + }).catch(console.error); |
| 59 | + }) |
| 60 | +} |
0 commit comments