mercury/bot.js

183 lines
6.7 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:25 +00:00
//var uconfig = require('./config/usersettings.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
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.floodprotect.flood_protection,
floodProtectionDelay: config.floodprotect.flood_protection_delay
2023-10-02 02:11:23 +00:00
});
2023-10-02 02:11:26 +00:00
const msgTimeout = new Set();
const msgTimeoutMsg = new Set();
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:26 +00:00
function consoleLog(log) {
if (config.misc.logging === "true") {
console.log(log)
} else {
return;
}
}
function openPostWorker(chan, command, d1, d2, d3, d4, d5) {
2023-10-02 02:11:26 +00:00
consoleLog(`[bot.openPostWorker] Opening ${command} worker`)
const worker = new Worker(`./commands/${command}.js`, {
workerData: {
d1, d2, d3, d4, d5
}
});
worker.once('message', (string) => {
2023-10-02 02:11:27 +00:00
consoleLog(`[bot.openPostWorker.finalising] Got output from ${command}, sending to `+chan);
bot.say(chan, string);
});
}
2023-10-02 02:11:23 +00:00
async function help(chan, sub) {
openPostWorker(chan, 'help', sub)
2023-10-02 02:11:23 +00:00
}
2023-10-02 02:11:25 +00:00
async function opt(chan, user, setting, setting2, value, value2) {
openPostWorker(chan, 'options', user, setting, setting2, value, value2)
}
2023-10-02 02:11:25 +00:00
async function feed(chan, nick, provfeed, n) {
2023-10-02 02:11:25 +00:00
var userconf = fs.readFileSync('./config/usersettings.json')
var uconfig = JSON.parse(userconf)
2023-10-02 02:11:27 +00:00
if (isValidUrl(provfeed) === false) {
consoleLog('[bot.feed] Provided feed is not a URL, transforming to lowercase')
var provfeed = provfeed.toLowerCase()
}
var predefinedFeeds = ['twitter', 'github']
var predefString = provfeed.split("/")
2023-10-02 02:11:24 +00:00
if (provfeed === undefined) {
2023-10-02 02:11:26 +00:00
consoleLog('[bot.feed] No feed provided')
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' ) {
2023-10-02 02:11:26 +00:00
consoleLog('[bot.feed] \"me\" was passed, correcting to '+nick)
2023-10-02 02:11:25 +00:00
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:27 +00:00
consoleLog('[bot.feed] No post was passed, reverting to '+config.feed.default_amount+', your set default.')
2023-10-02 02:11:25 +00:00
var n = config.feed.default_amount;
2023-10-02 02:11:23 +00:00
}
if (isValidUrl(provfeed) === true) { //URL Lookup
2023-10-02 02:11:26 +00:00
consoleLog('[bot.feed] Valid URL requested')
openPostWorker(chan, 'feed-preset', provfeed, n);
2023-10-02 02:11:27 +00:00
} else if (predefinedFeeds.includes(predefString[0])) { //Predefined Feed lookup
consoleLog('[bot.feed] Detected predefined feed: '+predefString[0])
openPostWorker(chan, "feed-predef", provfeed, n)
} else if (provfeed === nick) { //User Feed Lookup
2023-10-02 02:11:26 +00:00
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
2023-10-02 02:11:25 +00:00
bot.say(chan, "You have no saved feeds")
return;
}
} else if (uconfig[nick].alias !== undefined ) { //Alias Lookup
2023-10-02 02:11:26 +00:00
consoleLog('[bot.feed] Alias requested')
var provfeed = uconfig[nick].alias[provfeed.toUpperCase()]
openPostWorker(chan, "feed-preset", provfeed, n);
} else {
2023-10-02 02:11:26 +00:00
consoleLog('[bot.feed] No valid feed entered')
bot.say(chan, errorMsg+" Your chosen feed or alias is not valid")
2023-10-02 02:11:25 +00:00
}
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:26 +00:00
consoleLog('[bot.twitter] No twitter account provided')
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
}
openPostWorker(chan, "twitter", provfeed, n)
2023-10-02 02:11:24 +00:00
}
2023-10-02 02:11:23 +00:00
bot.addListener('message', function(nick, to, text, from) {
if (text.startsWith(config.irc.prefix)) {
if (msgTimeout.has(to)) {
if (msgTimeoutMsg.has("yes")) {
return;
} else {
bot.say(to, errorMsg+" You are sending commands too quickly")
msgTimeoutMsg.add("yes");
setTimeout(() => {
msgTimeoutMsg.delete("yes");
}, config.floodprotect.command_listen_timeout)
}
2023-10-02 02:11:26 +00:00
} else {
var args = text.split(' ');
if (args[0] === config.irc.prefix+'help') {
help(to, args[1]);
} else if (args[0] === config.irc.prefix+'feed') {
feed(to, nick, args[1], args[2]);
} else if (args[0] === config.irc.prefix+'twitter') {
twitter(to, args[1], args[2])
} else if (args[0] === config.irc.prefix+'opt') {
opt(to, nick, args[1], args[2], args[3], args[4])
}
msgTimeout.add(to);
2023-10-02 02:11:26 +00:00
setTimeout(() => {
msgTimeout.delete(to);
}, config.floodprotect.command_listen_timeout)
2023-10-02 02:11:26 +00:00
}
2023-10-02 02:11:23 +00:00
}
});
bot.addListener('error', function(message) {
2023-10-02 02:11:26 +00:00
consoleLog('[ERROR]' +message)
2023-10-02 02:11:23 +00:00
});
2023-10-02 02:11:27 +00:00
consoleLog('[bot.init] Welcome to Mercury');
fs.open('./config/usersettings.json','r',function(err, fd){
if (err) {
fs.writeFile('./config/usersettings.json', '', function(err) {
if(err) {
2023-10-02 02:11:27 +00:00
consoleLog(err);
}
2023-10-02 02:11:27 +00:00
consoleLog("[bot.init] usersettings.json did not exist, it has been created");
});
fs.writeFileSync('./config/usersettings.json', "\{\n\}")
} else {
2023-10-02 02:11:27 +00:00
consoleLog("[bot.init] usersettings.json exists, continuing");
}
});
2023-10-02 02:11:27 +00:00
consoleLog('[bot.init] Connecting to '+config.irc.server+'/'+config.irc.port+' as '+config.irc.nickname);
process.on('uncaughtException', function (err) {
console.error(err);
if (config.errorhandling.log_errors_to_irc == 'true') {
bot.say(config.errorhandling.error_channel, errorMsg+" "+err.stack.split('\n',1).join(" "))
}
});