add more detailed logging

This commit is contained in:
hgw 2023-10-02 02:11:26 +00:00
parent 2fc39ca86f
commit f08878e990
6 changed files with 87 additions and 43 deletions

32
bot.js
View File

@ -33,14 +33,23 @@ const isValidUrl = urlString=> {
return !!urlPattern.test(urlString);
}
function consoleLog(log) {
if (config.misc.logging === "true") {
console.log(log)
} else {
return;
}
}
function openPostWorker(chan, command, d1, d2, d3, d4, d5) {
consoleLog(`[bot.openPostWorker] Opening ${command} worker`)
const worker = new Worker(`./commands/${command}.js`, {
workerData: {
d1, d2, d3, d4, d5
}
});
worker.once('message', (string) => {
console.log(`${command} worker has signalled it has completed, sending output to IRC`);
consoleLog(`[bot.openPostWorker] Got output from ${command}, sending to `+chan);
bot.say(chan, string);
});
}
@ -56,24 +65,23 @@ async function opt(chan, user, setting, setting2, value, value2) {
async function feed(chan, nick, provfeed, n) {
var userconf = fs.readFileSync('./config/usersettings.json')
var uconfig = JSON.parse(userconf)
if (provfeed === undefined) {
consoleLog('[bot.feed] No feed provided')
bot.say(chan, errorMsg+" No feed has been provided.")
return;
} else if (provfeed === 'me' ) {
consoleLog('[bot.feed] \"me\" was passed, correcting to '+nick)
var provfeed = nick;
}
if (n === undefined) {
consoleLog('[bot.feed] no amount was passed, reverting to default')
var n = config.feed.default_amount;
}
console.log(isValidUrl(provfeed))
console.log(provfeed === nick)
console.log(uconfig[nick].alias !== undefined)
if (isValidUrl(provfeed) === true) { //URL Lookup
consoleLog('[bot.feed] Valid URL requested')
openPostWorker(chan, 'feed-preset', provfeed, n);
} else if (provfeed === nick) { //User Feed Lookup
consoleLog('[bot.feed] User feed requested')
if ( uconfig[nick] !== undefined ) { //If users nickname exists in json file
openPostWorker(chan, 'feed-list', provfeed, n, nick);
} else { //If it does not
@ -81,15 +89,18 @@ async function feed(chan, nick, provfeed, n) {
return;
}
} else if (uconfig[nick].alias !== undefined ) { //Alias Lookup
consoleLog('[bot.feed] Alias requested')
var provfeed = uconfig[nick].alias[provfeed]
openPostWorker(chan, "feed-preset", provfeed, n);
} else {
bot.say(chan, 'Not sure how you managed to get this error, but good job')
consoleLog('[bot.feed] No valid feed entered')
bot.say(chan, errorMsg+" Your chosen feed or alias is not valid")
}
}
async function twitter(chan, provfeed, n) {
if (provfeed === undefined) {
consoleLog('[bot.twitter] No twitter account provided')
bot.say(chan, errorMsg+" No account has been provided.")
return;
}
@ -113,7 +124,8 @@ bot.addListener('message', function(nick, to, text, from) {
});
bot.addListener('error', function(message) {
console.log('error: ', message);
consoleLog('[ERROR]' +message)
});
console.log('Starting Mercury');
console.log('[main] Starting Mercury');
console.log('[main.irc] Connecting to '+config.irc.server+'/'+config.irc.port+' as '+config.irc.nickname)

View File

@ -17,14 +17,23 @@ const timer = ms => new Promise(res => setTimeout(res, ms))
warningMsg = ''+config.colours.brackets+'['+config.colours.warning+'WARNING'+config.colours.brackets+']'
errorMsg = ''+config.colours.brackets+'['+config.colours.error+'ERROR'+config.colours.brackets+']'
function consoleLog(log) {
if (config.misc.logging === "true") {
console.log(log)
} else {
return;
}
}
async function sendUpstream(content) {
var output = content.join("\n")
consoleLog('[feed-list] All done.')
parentPort.postMessage(output);
process.exit()
}
function errorMessage(error, code, extra) {
console.log(error.code)
consoleLog('[feed-list.errorMessage] '+error.code)
if (code == "404") {
var error = errorMsg+" 404: " + extra + " not found"
} else if (error.code == "ECONNREFUSED") {
@ -55,6 +64,7 @@ async function fetchFeed(feedURL, n, nick) {
var n = n/feedsArr.length
}
for (let i = 0; i < feedsArr.length; i++) {
consoleLog('[feed-list.fetchFeed] Fetching '+feedsArr[i])
try {
var newFeed = await parser.parseURL(feedsArr[i]);
} catch (e) {
@ -83,7 +93,6 @@ async function fetchFeed(feedURL, n, nick) {
if (data.isoDate !== undefined) {
var date = moment(data.isoDate)
var syncDate = date.tz(config.feed.timezone)
console.log(syncDate.format())
var date = syncDate.format(config.feed.time_format)
} else {
var date = data.pubDate
@ -97,31 +106,12 @@ async function fetchFeed(feedURL, n, nick) {
author = ''+config.colours.author+data.creator+' '
body = ''+config.colours.body+body+' '
link = ''+config.colours.link+data.link+' '
//console.log(data);
//var string = "15[11" + date + "15] 08" + title + " " + body + " " + data.link;
var string = date+title+body+link;
content.push(string)
console.log(content)
}
//sendUpstream(content);
}
sendUpstream(content);
}
//var file = editJsonFile('/home/node/app/config/usersettings.json');
//async function getAllFeeds(nick, n) {
// console.log(nick)
// var feedsArr = uconfig[nick].feeds
// console.log(feedsArr)
// var num = n/feedsArr.length
// for (let i = 0; i < feedsArr.length; i++) {
// fetchFeed(feedsArr[i], num);
// }
// await sendUpstream(content);
//}
//getAllFeeds(nick, n)
fetchFeed(provfeed, n, provfeed);

View File

@ -15,14 +15,23 @@ const timer = ms => new Promise(res => setTimeout(res, ms))
warningMsg = ''+config.colours.brackets+'['+config.colours.warning+'WARNING'+config.colours.brackets+']'
errorMsg = ''+config.colours.brackets+'['+config.colours.error+'ERROR'+config.colours.brackets+']'
function consoleLog(log) {
if (config.misc.logging === "true") {
console.log(log)
} else {
return;
}
}
async function sendUpstream(content) {
var output = content.join("\n")
consoleLog('[feed-preset] All done.')
parentPort.postMessage(output);
process.exit()
}
function errorMessage(error, code, extra) {
console.log(error.code)
consoleLog('[feed-preset.errorMessage] '+error.code)
if (code == "404") {
var error = errorMsg+" 404: " + extra + " not found"
} else if (error.code == "ECONNREFUSED") {
@ -39,6 +48,7 @@ function errorMessage(error, code, extra) {
async function fetchFeed(feedURL, n) {
var content = [];
consoleLog('[feed-preset.fetchFeed] Fetching '+feedURL)
try {
var newFeed = await parser.parseURL(feedURL);
} catch (e) {
@ -67,7 +77,6 @@ async function fetchFeed(feedURL, n) {
if (data.isoDate !== undefined) {
var date = moment(data.isoDate)
var syncDate = date.tz(config.feed.timezone)
console.log(syncDate.format())
var date = syncDate.format(config.feed.time_format)
} else {
var date = data.pubDate

View File

@ -8,15 +8,23 @@ const timer = ms => new Promise(res => setTimeout(res, ms))
warningMsg = ''+config.colours.brackets+'['+config.colours.warning+'WARNING'+config.colours.brackets+']'
errorMsg = ''+config.colours.brackets+'['+config.colours.error+'ERROR'+config.colours.brackets+']'
function consoleLog(log) {
if (config.misc.logging === "true") {
console.log(log)
} else {
return;
}
}
async function sendUpstream(content) {
var output = content.join("\n")
consoleLog('[help] All done.')
parentPort.postMessage(output);
process.exit()
}
function errorMessage(error, code, extra) {
console.log(error.code)
consoleLog('[help.errorMessage] '+error.code)
if (code == "404") {
var error = errorMsg+" 404: " + extra + " not found"
} else if (error.code == "ECONNREFUSED") {
@ -37,13 +45,15 @@ async function help(sub) {
var sub = "default"
}
if (sub === "default") {
console.log(config.misc.display_help_logo)
if (config.misc.display_help_logo === 'true' ) {
consoleLog('[help.default] Logo enabled, including in output')
content.push(''+config.colours.help_logo+' ____ ___ ___ ____________ _________ __')
content.push(''+config.colours.help_logo+' / __ `__ \\/ _ \\/ ___/ ___/ / / / ___/ / / /')
content.push(''+config.colours.help_logo+' / / / / / / __/ / / /__/ /_/ / / / /_/ / ')
content.push(''+config.colours.help_logo+'/_/ /_/ /_/\\___/_/ \\___/\\__,_/_/ \\__, / ')
content.push(''+config.colours.help_logo+' /____/ ')
} else if (config.misc.display_help_logo !== 'true') {
consoleLog('[help.default] Logo disabled, not including in output')
}
content.push('Mercury RSS Client - https://git.supernets.org/hogwart7/mercury')
content.push('m!feed [USER/FEED/ALIAS] [ENTRIES] - Return the last x amount of entries from any RSS feed or your own saved feeds (if you have saved feeds)')

View File

@ -18,13 +18,22 @@ const timer = ms => new Promise(res => setTimeout(res, ms))
warningMsg = ''+config.colours.brackets+'['+config.colours.warning+'WARNING'+config.colours.brackets+']'
errorMsg = ''+config.colours.brackets+'['+config.colours.error+'ERROR'+config.colours.brackets+']'
function consoleLog(log) {
if (config.misc.logging === "true") {
console.log(log)
} else {
return;
}
}
async function sendUpstream(content) {
parentPort.postMessage(content);
consoleLog('[options] All done.')
process.exit()
}
function errorMessage(error, code, extra) {
console.log(error.code)
consoleLog('[options.errorMessage] '+error.code)
if (code == "404") {
var error = errorMsg+" 404: " + extra + " not found"
} else if (error.code == "ECONNREFUSED") {
@ -46,18 +55,18 @@ function errorMessage(error, code, extra) {
async function testFeed(feedURL) {
try {
console.log('Testing feed')
consoleLog('[options.testFeed] Testing '+feed)
var feed = await parser.parseURL(feedURL);
} catch (e) {
console.log(e)
consoleLog('[options.testFeed] '+e)
errorMessage(e, "INVALID", feedURL);
}
console.log(feed)
console.log("Feed is good, saving")
consoleLog("[options.testFeed] Feed is valid, continuing")
}
async function feed(nick, setting, value) {
if (setting === 'add') {
consoleLog('[options.feed] '+nick+' is adding '+value)
await testFeed(value);
var file = editJsonFile('/home/node/app/config/usersettings.json');
var feedsArr = uconfig[nick].feeds
@ -73,7 +82,7 @@ async function feed(nick, setting, value) {
content = [];
try {
var feedsArr = uconfig[nick].feeds
console.log(feedsArr)
consoleLog('[options.feed] Listing existing feeds for '+nick+': '+feedsArr)
content.push("These are your added feeds:")
} catch (e) {
errorMessage(e, "NOFEEDS", nick);
@ -88,6 +97,7 @@ async function feed(nick, setting, value) {
async function alias(setting, value, url, nick) {
if (setting === 'add') {
consoleLog('[options.alias] Adding/editing an alias for'+nick+': '+value+' ==> '+url)
await testFeed(url);
var file = editJsonFile('/home/node/app/config/usersettings.json');
file.set(nick+'.alias.'+value, url);
@ -95,6 +105,7 @@ async function alias(setting, value, url, nick) {
sendUpstream('Alias added ('+value+' ==> '+url+')')
}
if (setting === 'del') {
consoleLog('[options.alias] Removing an alias for '+nick+': '+value+' ==> \"\"')
var file = editJsonFile('/home/node/app/config/usersettings.json');
file.set(nick+'.alias.'+value, "");
file.save();
@ -103,6 +114,7 @@ async function alias(setting, value, url, nick) {
if (setting === 'list') {
content = [];
var obj = uconfig[nick].alias
consoleLog('[options.alias] Listing aliases for '+nick+': '+obj)
console.log(obj)
for (const [key, val] of Object.entries(obj)) {
if (val !== "") {
@ -115,6 +127,7 @@ async function alias(setting, value, url, nick) {
}
async function get(setting) {
consoleLog('[options.get] Getting value of '+setting)
var file = editJsonFile('/home/node/app/config/default.json')
console.log(file.get(setting));
sendUpstream(file.get(setting))

View File

@ -15,15 +15,23 @@ const timer = ms => new Promise(res => setTimeout(res, ms))
warningMsg = ''+config.colours.brackets+'['+config.colours.warning+'WARNING'+config.colours.brackets+']'
errorMsg = ''+config.colours.brackets+'['+config.colours.error+'ERROR'+config.colours.brackets+']'
function consoleLog(log) {
if (config.misc.logging === "true") {
console.log(log)
} else {
return;
}
}
async function sendUpstream(content) {
var output = content.join("\n")
consoleLog('[twitter] All done.')
parentPort.postMessage(output);
process.exit()
}
function errorMessage(error, code, extra) {
console.log(error.code)
consoleLog('[twitter.errorMessage] '+error.code)
if (code == "404") {
var error = errorMsg+" 404: " + extra + " not found"
} else if (error.code == "ECONNREFUSED") {
@ -40,8 +48,10 @@ function errorMessage(error, code, extra) {
async function fetchFeed(feedURL, n) {
var content = [];
consoleLog('[twitter.fetchFeed] fetching @'+feedURL)
if (feedURL.startsWith('@') == true) {
consoleLog('[twitter.fetchFeed] User passed @ in input, removing')
var feedURL = feedURL.substring(1,feedURL.length);
}
var randomNitter = config.twitter.nitter_instances[Math.floor(Math.random() * config.twitter.nitter_instances.length)];