Add config presence checks + other initialisation tweaks
This commit is contained in:
parent
4c27bd5a1e
commit
3300a0b34b
30
bot.js
30
bot.js
@ -5,14 +5,27 @@
|
|||||||
// /_/ /_/ /_/\___/_/ \___/\__,_/_/ \__, /
|
// /_/ /_/ /_/\___/_/ \___/\__,_/_/ \__, /
|
||||||
// COLD HARD FEEDS /____/
|
// COLD HARD FEEDS /____/
|
||||||
//
|
//
|
||||||
var config = require('./config/default.json');
|
//var config = require('./config/default.json');
|
||||||
//var uconfig = require('./config/usersettings.json');
|
//var uconfig = require('./config/usersettings.json');
|
||||||
var irc = require("irc");
|
var irc = require("irc");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var readline = require('readline');
|
var readline = require('readline');
|
||||||
const { Worker } = require('worker_threads');
|
const { Worker } = require('worker_threads');
|
||||||
|
const { setMaxIdleHTTPParsers } = require("http");
|
||||||
//var randomWords = require('better-random-words');
|
//var randomWords = require('better-random-words');
|
||||||
|
|
||||||
|
const timer = ms => new Promise(res => setTimeout(res, ms))
|
||||||
|
|
||||||
|
console.log('[bot] Checking if config file exists')
|
||||||
|
if (fs.existsSync('./config/default.json')) {
|
||||||
|
var config = require('./config/default.json');
|
||||||
|
console.log('[bot] Config file exists, can proceed with initialisation')
|
||||||
|
} else {
|
||||||
|
console.log('[bot] The config file, default.json, does not exist in the config folder. Mercury can not start.')
|
||||||
|
process.exit()
|
||||||
|
}
|
||||||
|
timer(100)
|
||||||
|
|
||||||
warningMsg = ''+config.colours.brackets+'['+config.colours.warning+'WARNING'+config.colours.brackets+']'
|
warningMsg = ''+config.colours.brackets+'['+config.colours.warning+'WARNING'+config.colours.brackets+']'
|
||||||
errorMsg = ''+config.colours.brackets+'['+config.colours.error+'ERROR'+config.colours.brackets+']'
|
errorMsg = ''+config.colours.brackets+'['+config.colours.error+'ERROR'+config.colours.brackets+']'
|
||||||
|
|
||||||
@ -30,7 +43,6 @@ var bot = new irc.Client(config.irc.server, config.irc.nickname, {
|
|||||||
const msgTimeout = new Set();
|
const msgTimeout = new Set();
|
||||||
const msgTimeoutMsg = new Set();
|
const msgTimeoutMsg = new Set();
|
||||||
|
|
||||||
const timer = ms => new Promise(res => setTimeout(res, ms))
|
|
||||||
|
|
||||||
const isValidUrl = urlString=> {
|
const isValidUrl = urlString=> {
|
||||||
var urlPattern = new RegExp('^(https?:\\/\\/)?'+ // validate protocol
|
var urlPattern = new RegExp('^(https?:\\/\\/)?'+ // validate protocol
|
||||||
@ -54,13 +66,13 @@ function consoleLog(log) {
|
|||||||
var hostmask = null
|
var hostmask = null
|
||||||
|
|
||||||
function checkConfigValidity() {
|
function checkConfigValidity() {
|
||||||
consoleLog(`[bot.checkConfigValidity] Opening cvc worker`)
|
consoleLog(`[bot.checkConfigValidity] Opening config validator`)
|
||||||
const worker = new Worker(`./commands/cvc.js`, {});
|
const worker = new Worker(`./commands/cvc.js`, {});
|
||||||
worker.once('message', (string) => {
|
worker.once('message', (string) => {
|
||||||
if (string == 'kill') {
|
if (string == 'kill') {
|
||||||
process.exit()
|
process.exit()
|
||||||
} else if (string == 'allg') {
|
} else if (string == 'allg') {
|
||||||
consoleLog('[bot.checkConfigValidity] Config validity checked, seems all good, continuing with initialisation')
|
consoleLog('[bot.checkConfigValidity] Config seems valid, continuing')
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -210,9 +222,6 @@ bot.addListener('error', function(message) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
consoleLog('[bot.init] Checking config validity')
|
|
||||||
checkConfigValidity()
|
|
||||||
await timer(1500)
|
|
||||||
consoleLog('[bot.init] Checking if user settings file exists')
|
consoleLog('[bot.init] Checking if user settings file exists')
|
||||||
fs.open('./config/usersettings.json','r',function(err, fd){
|
fs.open('./config/usersettings.json','r',function(err, fd){
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -222,7 +231,6 @@ async function init() {
|
|||||||
consoleLog('[bot.init] [FATAL] User settings file could not be created. Mercury can not start')
|
consoleLog('[bot.init] [FATAL] User settings file could not be created. Mercury can not start')
|
||||||
process.exit()
|
process.exit()
|
||||||
}
|
}
|
||||||
consoleLog("[bot.init] User settings does not exist, please hold...");
|
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
fs.writeFileSync('./config/usersettings.json', "\{\n\}")
|
fs.writeFileSync('./config/usersettings.json', "\{\n\}")
|
||||||
@ -231,12 +239,16 @@ async function init() {
|
|||||||
consoleLog('[bot.init] [FATAL] User settings file was created but is not writable, could be a permissions issue. Mercury can not start')
|
consoleLog('[bot.init] [FATAL] User settings file was created but is not writable, could be a permissions issue. Mercury can not start')
|
||||||
process.exit()
|
process.exit()
|
||||||
}
|
}
|
||||||
|
timer(100)
|
||||||
consoleLog('[bot.init] User settings file has been created')
|
consoleLog('[bot.init] User settings file has been created')
|
||||||
} else {
|
} else {
|
||||||
consoleLog("[bot.init] User settings file exists");
|
consoleLog("[bot.init] User settings file exists");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await timer(100)
|
await timer(500)
|
||||||
|
consoleLog('[bot.init] Checking config validity')
|
||||||
|
checkConfigValidity()
|
||||||
|
await timer(2000)
|
||||||
if (config.irc.ssl == "true") {
|
if (config.irc.ssl == "true") {
|
||||||
consoleLog('[bot.init] Initialisation completed, connecting to '+config.irc.server+'/'+config.irc.port+' (SSL) as '+config.irc.nickname);
|
consoleLog('[bot.init] Initialisation completed, connecting to '+config.irc.server+'/'+config.irc.port+' (SSL) as '+config.irc.nickname);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user