mercury/bot.js

147 lines
5.0 KiB
JavaScript
Raw Normal View History

2023-10-02 02:11:25 +00:00
var config = require('./config/default.json');
2023-10-02 02:11:23 +00:00
var irc = require("irc");
var fs = require("fs");
var readline = require('readline');
const { Worker } = require('worker_threads');
//var randomWords = require('better-random-words');
2023-10-02 02:11:25 +00:00
warningMsg = ''+config.colours.brackets+'['+config.colours.warning+'WARNING'+config.colours.brackets+']'
errorMsg = ''+config.colours.brackets+'['+config.colours.error+'ERROR'+config.colours.brackets+']'
2023-10-02 02:11:25 +00:00
2023-10-02 02:11:25 +00:00
var bot = new irc.Client(config.irc.server, config.irc.nickname, {
channels: config.irc.channels,
secure: config.irc.ssl,
port: config.irc.port,
autoRejoin: config.irc.autorejoin,
userName: config.irc.username,
realName: config.irc.realname,
floodProtection: config.irc.floodprotection,
floodProtectionDelay: config.irc.floodprotectiondelay
2023-10-02 02:11:23 +00:00
});
const timer = ms => new Promise(res => setTimeout(res, ms))
2023-10-02 02:11:25 +00:00
const isValidUrl = urlString=> {
var urlPattern = new RegExp('^(https?:\\/\\/)?'+ // validate protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // validate domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // validate OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // validate port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // validate query string
'(\\#[-a-z\\d_]*)?$','i'); // validate fragment locator
return !!urlPattern.test(urlString);
}
2023-10-02 02:11:23 +00:00
async function help(chan, sub) {
if (sub === undefined) {
var sub = "default"
}
if (sub === "default") {
2023-10-02 02:11:24 +00:00
bot.say(chan, ' ____ ___ ___ ____________ _________ __')
bot.say(chan, ' / __ `__ \\/ _ \\/ ___/ ___/ / / / ___/ / / /')
bot.say(chan, ' / / / / / / __/ / / /__/ /_/ / / / /_/ / ')
bot.say(chan, '/_/ /_/ /_/\\___/_/ \\___/\\__,_/_/ \\__, / ')
bot.say(chan, ' /____/ ')
2023-10-02 02:11:25 +00:00
bot.say(chan, 'Mercury RSS Client - https://git.supernets.org/hogwart7/mercury')
bot.say(chan, 'm!feed [USER/FEED] [ENTRIES] - Return the last x amount of entries from any RSS feed or your own saved feeds (if you have saved feeds)')
2023-10-02 02:11:24 +00:00
bot.say(chan, "m!twitter [USER] [ENTRIES] - Return the last x amount of tweets from a particular user")
2023-10-02 02:11:25 +00:00
bot.say(chan, "m!set [CATEGORY] [OPTION] [VALUE] - Control bot settings, see wiki for info on usage.")
2023-10-02 02:11:23 +00:00
}
}
async function opt(chan, user, setting, setting2, value) {
//if (provfeed === undefined) {
// 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
}
});
worker.once('message', (string) => {
2023-10-02 02:11:25 +00:00
console.log('Received output from options worker, posting.');
bot.say(chan, string);
});
}
2023-10-02 02:11:25 +00:00
async function feed(chan, nick, provfeed, n) {
2023-10-02 02:11:24 +00:00
if (provfeed === undefined) {
2023-10-02 02:11:25 +00:00
bot.say(chan, errorMsg+" No feed has been provided.")
return;
2023-10-02 02:11:25 +00:00
} else if (provfeed === 'me' ) {
var provfeed = nick;
2023-10-02 02:11:23 +00:00
}
2023-10-02 02:11:24 +00:00
if (n === undefined) {
2023-10-02 02:11:25 +00:00
var n = config.feed.default_amount;
2023-10-02 02:11:23 +00:00
}
2023-10-02 02:11:25 +00:00
if (isValidUrl(provfeed) === false) {
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);
});
} else {
const worker = new Worker('./commands/feed-preset.js', {
workerData: {
provfeed,
n
}
});
worker.once('message', (string) => {
console.log('Received output from feed-preset worker, posting.');
bot.say(chan, string);
});
}
2023-10-02 02:11:23 +00:00
}
2023-10-02 02:11:24 +00:00
async function twitter(chan, provfeed, n) {
if (provfeed === undefined) {
2023-10-02 02:11:25 +00:00
bot.say(chan, errorMsg+" No account has been provided.")
return;
2023-10-02 02:11:24 +00:00
}
if (n === undefined) {
2023-10-02 02:11:25 +00:00
var n = config.twitter.default_amount;
2023-10-02 02:11:24 +00:00
}
const worker = new Worker('./commands/twitter.js', {
workerData: {
provfeed,
n
}
});
worker.once('message', (string) => {
console.log('Received output from twitter worker, posting.');
bot.say(chan, string);
});
}
2023-10-02 02:11:23 +00:00
bot.addListener('message', function(nick, to, text, from) {
var args = text.split(' ');
2023-10-02 02:11:25 +00:00
if (args[0] === config.irc.prefix+'help') {
2023-10-02 02:11:23 +00:00
help(to, args[1]);
2023-10-02 02:11:25 +00:00
} else if (args[0] === config.irc.prefix+'feed') {
2023-10-02 02:11:25 +00:00
feed(to, nick, args[1], args[2]);
2023-10-02 02:11:25 +00:00
} else if (args[0] === config.irc.prefix+'twitter') {
2023-10-02 02:11:24 +00:00
twitter(to, args[1], args[2])
2023-10-02 02:11:25 +00:00
} else if (args[0] === config.irc.prefix+'opt') {
opt(to, nick, args[1], args[2], args[3])
2023-10-02 02:11:23 +00:00
}
});
bot.addListener('error', function(message) {
console.log('error: ', message);
});
2023-10-02 02:11:24 +00:00
console.log('Starting Mercury');