Skip to content

Instantly share code, notes, and snippets.

@GuntharDeNiro
Created September 6, 2021 13:29
Show Gist options
  • Save GuntharDeNiro/6d85ab713fb0854025c63a0389cac617 to your computer and use it in GitHub Desktop.
Save GuntharDeNiro/6d85ab713fb0854025c63a0389cac617 to your computer and use it in GitHub Desktop.
function loadConfig() {
return new Promise((resolve, reject) => {
try {
if (config_file.substring(0, 5) == 'https' || config_file.substring(0, 4) == 'http') {
console.log(sprintf('Loading remote config... [%s]'.GOOD_NEWS_COLOR, config_file));
rp(config_file).then((response) => {
resolve(JSON.parse(response));
});
} else {
console.log('Loading config...'.GOOD_NEWS_COLOR);
resolve(JSON.parse(fs.readFileSync(path.join(process.cwd(), config_file))));
}
} catch (e) {
//sentry.captureException(e);
console.log(`Bot crashed at ${lib.timeStamp()}\nReason: Failed to load config!!!\nParameter: ${config.CONFIGFILE}\nOriginal error: ${util.inspect(e)}`.error);
if (!_.isNil(config.bot.ALERTS_TOKEN)) app.telegram.sendMessage(config.bot.chat_id, 'Gunbot stopped to run');
process.exit(1);
}
}).then((json) => {
var needWrite = false;
for (var key in json.exchanges)
if (!json.exchanges[key].isEncrypted) {
var encrypted_master = helper.encrypt(json.exchanges[key].mastersecret);
var encrypted_api = helper.encrypt(json.exchanges[key].secret);
json.exchanges[key].mastersecret = encrypted_master;
json.exchanges[key].secret = encrypted_api;
json.exchanges[key].isEncrypted = true;
needWrite = true;
}
if (needWrite) fs.writeFileSync(path.join(process.cwd(), './config.js'), JSON.stringify(json, null, 4));
var autoConfig = {
autoconfig: JSON.parse(fs.readFileSync(path.join(process.cwd(), 'autoconfig.json')))
};
_.merge(json, autoConfig);
if (needWrite) fs.writeFileSync(path.join(process.cwd(), './config.js'), JSON.stringify(json, null, 4));
_.merge(config, json);
memory.config = config;
global.config = config;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment