From d2d896667c2d83540b49fbb3219a0f527282e2b1 Mon Sep 17 00:00:00 2001 From: hgw Date: Mon, 2 Oct 2023 02:11:25 +0000 Subject: [PATCH] colour customisation + big bug fix --- bot.js | 9 +++++++-- commands/feed.js | 30 ++++++++++++++++++++---------- commands/twitter.js | 28 +++++++++++++++++++--------- config/example.default.json | 10 ++++++++++ 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/bot.js b/bot.js index aa45b1d..a738fe2 100644 --- a/bot.js +++ b/bot.js @@ -5,6 +5,9 @@ var readline = require('readline'); const { Worker } = require('worker_threads'); //var randomWords = require('better-random-words'); +warningMsg = '['+config.colours.warning+'WARNING]' +errorMsg = '['+config.colours.error+'ERROR]' + var bot = new irc.Client(config.irc.server, config.irc.nickname, { channels: config.irc.channels, secure: config.irc.ssl, @@ -36,7 +39,8 @@ async function help(chan, sub) { async function feed(chan, provfeed, n) { if (provfeed === undefined) { - bot.say(chan, "No feed has been provided.") + bot.say(chan, errorMsg+" No feed has been provided.") + return; } if (n === undefined) { var n = 5; @@ -55,7 +59,8 @@ async function feed(chan, provfeed, n) { async function twitter(chan, provfeed, n) { if (provfeed === undefined) { - bot.say(chan, "No account has been provided.") + bot.say(chan, errorMsg+" No account has been provided.") + return; } if (n === undefined) { var n = 5; diff --git a/commands/feed.js b/commands/feed.js index 3890e14..0c634f1 100644 --- a/commands/feed.js +++ b/commands/feed.js @@ -10,6 +10,9 @@ const moment = require('moment'); const tz = require('moment-timezone'); const timer = ms => new Promise(res => setTimeout(res, ms)) +warningMsg = '['+config.colours.warning+'WARNING]' +errorMsg = '['+config.colours.error+'ERROR]' + async function sendUpstream(content) { var output = content.join("\n") parentPort.postMessage(output); @@ -19,13 +22,13 @@ async function sendUpstream(content) { function errorMessage(error, code, extra) { console.log(error.code) if (code == "404") { - var error = "[04ERROR] 404: " + extra + " not found" + var error = errorMsg+" 404: " + extra + " not found" } else if (error.code == "ECONNREFUSED") { - var error = "[04ERROR] Connection Refused" + var error = errorMsg+" Connection Refused" } else if (error.code == "ERR_UNESCAPED_CHARACTERS"){ - var error = "[04ERROR] Unescaped Characters" + var error = errorMsg+" Unescaped Characters" } else { - var error = "[04ERROR] Unknown error" + var error = errorMsg+" Unknown error" } parentPort.postMessage(error); @@ -45,10 +48,10 @@ async function fetchFeed(feedURL, n) { } if (n > newFeed.items.length) { var n = newFeed.items.length; - content.push("[08WARNING] Your requested post amount exceeded the total available. Reverting to " + newFeed.items.length); + content.push(warningMsg+" Your requested post amount exceeded the total available. Reverting to " + newFeed.items.length); } else if (n < 1) { var n = 5 - content.push("[08WARNING] You requested a number less than 1. Reverting to 5"); + content.push(warningMsg+" You requested a number less than 1. Reverting to 5"); } //for (let i = 0; i < newFeed.items.length; i++) { for (let i = 0; i < n; i++) { @@ -71,10 +74,17 @@ async function fetchFeed(feedURL, n) { var truncatedString = body.substring(0,config.feed.body_max_chars); var body = truncatedString + "..." } - console.log(data); - var string = "15[11" + date + "15] 08" + title + " " + body + " " + data.link; - var output = string; - content.push(output) + + date = ''+config.colours.date_brackets+'['+config.colours.date+date+''+config.colours.date_brackets+'] ' + title = ''+config.colours.title+title+' ' + 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) } sendUpstream(content); } diff --git a/commands/twitter.js b/commands/twitter.js index bec7834..0eac9c4 100644 --- a/commands/twitter.js +++ b/commands/twitter.js @@ -10,6 +10,9 @@ const moment = require('moment'); const tz = require('moment-timezone'); const timer = ms => new Promise(res => setTimeout(res, ms)) +warningMsg = '['+config.colours.warning+'WARNING]' +errorMsg = '['+config.colours.error+'ERROR]' + async function sendUpstream(content) { var output = content.join("\n") parentPort.postMessage(output); @@ -19,13 +22,13 @@ async function sendUpstream(content) { function errorMessage(error, code, extra) { console.log(error.code) if (code == "404") { - var error = "[04ERROR] 404: " + extra + " not found" + var error = errorMsg+" 404: " + extra + " not found" } else if (error.code == "ECONNREFUSED") { - var error = "[04ERROR] Connection Refused" + var error = errorMsg+" Connection Refused" } else if (error.code == "ERR_UNESCAPED_CHARACTERS"){ - var error = "[04ERROR] Unescaped Characters" + var error = errorMsg+" Unescaped Characters" } else { - var error = "[04ERROR] Unknown error" + var error = errorMsg+" Unknown error" } parentPort.postMessage(error); @@ -52,10 +55,10 @@ async function fetchFeed(feedURL, n) { if (n > newFeed.items.length) { var n = newFeed.items.length; - content.push("[08WARNING] Your requested post amount exceeded the total available. Reverting to " + newFeed.items.length); + content.push(warningMsg+" Your requested post amount exceeded the total available. Reverting to " + newFeed.items.length); } else if (n < 1) { var n = 5 - content.push("[08WARNING] You requested a number less than 1. Reverting to 5"); + content.push(warningMsg+" You requested a number less than 1. Reverting to 5"); } for (let i = 0; i < n; i++) { @@ -83,10 +86,17 @@ async function fetchFeed(feedURL, n) { var truncatedString = body.substring(0,config.feed.body_max_chars); var body = truncatedString + "..." } + + date = ''+config.colours.date_brackets+'['+config.colours.date+date+''+config.colours.date_brackets+'] ' + title = ''+config.colours.title+title+' ' + 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" + data.creator + " " + body + " " + data.link; - var output = string; - content.push(output) + //var string = "15[11" + date + "15] 08" + data.creator + " " + body + " " + data.link; + var string = date+author+body+link + content.push(string) } sendUpstream(content); } diff --git a/config/example.default.json b/config/example.default.json index 8a1eaff..16ad265 100644 --- a/config/example.default.json +++ b/config/example.default.json @@ -13,6 +13,16 @@ "floodprotection": "false", "floodprotectiondelay": "0" }, + "colours": { + "date": "11", + "date_brackets": "15", + "title": "08", + "author": "08", + "body": "00", + "link": "02", + "warning": "08", + "error": "05" + }, "feed": { "useragent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 13.4; rv:109.0) Gecko/20100101 Firefox/114.0", "body_max_chars": "200",