move worker opener to its own function, vastly simplifying bot.js

This commit is contained in:
hgw 2023-10-02 02:11:26 +00:00
parent 7bd5195dab
commit c18cb0b8f6
5 changed files with 42 additions and 75 deletions

97
bot.js
View File

@ -33,6 +33,18 @@ const isValidUrl = urlString=> {
return !!urlPattern.test(urlString); return !!urlPattern.test(urlString);
} }
function openPostWorker(chan, command, d1, d2, d3, d4, d5) {
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`);
bot.say(chan, string);
});
}
async function help(chan, sub) { async function help(chan, sub) {
if (sub === undefined) { if (sub === undefined) {
var sub = "default" var sub = "default"
@ -51,32 +63,13 @@ async function help(chan, sub) {
} }
async function opt(chan, user, setting, setting2, value, value2) { async function opt(chan, user, setting, setting2, value, value2) {
//if (provfeed === undefined) { openPostWorker(chan, 'options', user, setting, setting2, value, value2)
// bot.say(chan, errorMsg+" No feed has been provided.")
// return;
//}
//if (n === undefined) {
// var n = config.feed.default_amount;
//}
const worker = new Worker('./commands/options.js', {
workerData: {
user,
setting,
setting2,
value,
value2
}
});
worker.once('message', (string) => {
console.log('Received output from options worker, posting.');
bot.say(chan, string);
});
} }
async function feed(chan, nick, provfeed, n) { async function feed(chan, nick, provfeed, n) {
var userconf = fs.readFileSync('./config/usersettings.json') var userconf = fs.readFileSync('./config/usersettings.json')
var uconfig = JSON.parse(userconf) var uconfig = JSON.parse(userconf)
//var uconfig = require('./config/usersettings.json');
if (provfeed === undefined) { if (provfeed === undefined) {
bot.say(chan, errorMsg+" No feed has been provided.") bot.say(chan, errorMsg+" No feed has been provided.")
return; return;
@ -90,50 +83,21 @@ async function feed(chan, nick, provfeed, n) {
console.log(isValidUrl(provfeed)) console.log(isValidUrl(provfeed))
console.log(provfeed === nick) console.log(provfeed === nick)
console.log(uconfig[nick].alias !== undefined) console.log(uconfig[nick].alias !== undefined)
if (isValidUrl(provfeed) === true) {
const worker = new Worker('./commands/feed-preset.js', { if (isValidUrl(provfeed) === true) { //URL Lookup
workerData: { openPostWorker(chan, 'feed-preset', provfeed, n);
provfeed, } else if (provfeed === nick) { //User Feed Lookup
n if ( uconfig[nick] !== undefined ) { //If users nickname exists in json file
} openPostWorker(chan, 'feed-list', provfeed, n, nick);
}); } else { //If it does not
worker.once('message', (string) => {
console.log('Received output from feed-preset worker, posting.');
bot.say(chan, string);
return;
});
} else if (provfeed === nick) {
if ( uconfig[nick] !== undefined ) {
const worker = new Worker('./commands/feed-list.js', {
workerData: {
provfeed,
n,
nick
}
});
worker.once('message', (string) => {
console.log('Received output from feed-list worker, posting.');
bot.say(chan, string);
return;
});
} else {
bot.say(chan, "You have no saved feeds") bot.say(chan, "You have no saved feeds")
return; return;
} }
} else if (uconfig[nick].alias !== undefined ) { } else if (uconfig[nick].alias !== undefined ) { //Alias Lookup
var provfeed = uconfig[nick].alias[provfeed] var provfeed = uconfig[nick].alias[provfeed]
const worker = new Worker('./commands/feed-preset.js', { openPostWorker(chan, "feed-preset", provfeed, n);
workerData: { } else {
provfeed, bot.say(chan, 'Not sure how you managed to get this error, but good job')
n,
nick
}
});
worker.once('message', (string) => {
console.log('Received output from feed-list worker, posting.');
bot.say(chan, string);
return;
});
} }
} }
@ -145,16 +109,7 @@ async function twitter(chan, provfeed, n) {
if (n === undefined) { if (n === undefined) {
var n = config.twitter.default_amount; var n = config.twitter.default_amount;
} }
const worker = new Worker('./commands/twitter.js', { openPostWorker(chan, "twitter", provfeed, n)
workerData: {
provfeed,
n
}
});
worker.once('message', (string) => {
console.log('Received output from twitter worker, posting.');
bot.say(chan, string);
});
} }
bot.addListener('message', function(nick, to, text, from) { bot.addListener('message', function(nick, to, text, from) {

View File

@ -1,7 +1,10 @@
const config = require('../config/default.json') const config = require('../config/default.json')
const uconfig = require('../config/usersettings.json') const uconfig = require('../config/usersettings.json')
const { parentPort, workerData } = require('worker_threads'); const { parentPort, workerData } = require('worker_threads');
const { provfeed, n, nick } = workerData; const { d1, d2, d3 } = workerData;
var provfeed = d1;
var n = d2;
var nick = d3;
let Parser = require('rss-parser'); let Parser = require('rss-parser');
let parser = new Parser({ let parser = new Parser({
headers: {'User-Agent': config.feed.useragent}, headers: {'User-Agent': config.feed.useragent},

View File

@ -1,6 +1,8 @@
const config = require('../config/default.json') const config = require('../config/default.json')
const { parentPort, workerData } = require('worker_threads'); const { parentPort, workerData } = require('worker_threads');
const { provfeed, n } = workerData; const { d1, d2 } = workerData;
var provfeed = d1;
var n = d2;
let Parser = require('rss-parser'); let Parser = require('rss-parser');
let parser = new Parser({ let parser = new Parser({
headers: {'User-Agent': config.feed.useragent}, headers: {'User-Agent': config.feed.useragent},

View File

@ -1,7 +1,12 @@
const config = require('../config/default.json') const config = require('../config/default.json')
const uconfig = require('../config/usersettings.json') const uconfig = require('../config/usersettings.json')
const { parentPort, workerData } = require('worker_threads'); const { parentPort, workerData } = require('worker_threads');
const { user, setting, setting2, value, value2 } = workerData; const { d1, d2, d3, d4, d5 } = workerData;
var user = d1;
var setting = d2;
var setting2 = d3;
var value = d4;
var value2 = d5;
const fs = require('fs-extra') const fs = require('fs-extra')
let Parser = require('rss-parser'); let Parser = require('rss-parser');
let parser = new Parser({ let parser = new Parser({

View File

@ -1,6 +1,8 @@
const config = require('../config/default.json') const config = require('../config/default.json')
const { parentPort, workerData } = require('worker_threads'); const { parentPort, workerData } = require('worker_threads');
const { provfeed, n } = workerData; const { d1, d2 } = workerData;
var provfeed = d1;
var n = d2
let Parser = require('rss-parser'); let Parser = require('rss-parser');
let parser = new Parser({ let parser = new Parser({
headers: {'User-Agent': config.feed.useragent}, headers: {'User-Agent': config.feed.useragent},