187 lines
5.0 KiB
JavaScript
187 lines
5.0 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
const fs = require("fs");
|
|
const irc = require("irc");
|
|
const Summon = require("./spells/summon");
|
|
|
|
const botName = "BabiliBot"
|
|
const client = new irc.Client("localhost", botName, {
|
|
channels: [
|
|
"#bots"
|
|
],
|
|
localAddress: "0.0.0.0",
|
|
port: 6667,
|
|
userName: "aewens",
|
|
floodProtection: true,
|
|
floodProtectionDelay: 1000,
|
|
autoConnect: false,
|
|
stripColors: true,
|
|
encoding: "utf-8",
|
|
debug: true
|
|
});
|
|
|
|
client.config = JSON.parse(fs.readFileSync("config.json", "utf8"));
|
|
|
|
client.spells = [
|
|
Summon
|
|
];
|
|
|
|
// NOTE - Once the #chaos throne is returned to it's king, this will remain
|
|
client.blacklist = JSON.parse(fs.readFileSync("blacklist.json", "utf8"));
|
|
|
|
// Format:
|
|
// {
|
|
// timestamp: Date.now(),
|
|
// spell: spell.name
|
|
// }
|
|
client.memory = [];
|
|
|
|
const contains = (text, test) => {
|
|
return text.indexOf(test) > -1;
|
|
}
|
|
|
|
const logBlacklisters = (from, message) => {
|
|
const blacklistLog = `${from} tried to issue ${message}`;
|
|
fs.appendFile("blacklist.log", blacklistLog, (err) => {
|
|
if (err) throw err;
|
|
console.log("Blacklist", blacklistLog);
|
|
});
|
|
}
|
|
|
|
const addBlacklister = (from, message) => {
|
|
// Log reason for adding to the blacklist
|
|
const blacklistReason = `${from} was added for spamming ${message}`;
|
|
fs.appendFile("blacklisters.log", blacklistReason, (err) => {
|
|
if (err) throw err;
|
|
console.log("Blacklist", blacklistReason);
|
|
});
|
|
|
|
// Apply to current session
|
|
client.blacklist.push(from);
|
|
|
|
// Apply to future sessions
|
|
const blacklist = JSON.stringify(client.blacklist);
|
|
fs.writeFile("blacklist.json", blacklist, (err) => {
|
|
if (err) throw err;
|
|
console.log("Added to blacklist:", from);
|
|
});
|
|
}
|
|
|
|
const castSpell = (client, from, to, incantation) => {
|
|
let casted = false;
|
|
client.spells.forEach((_spell) => {
|
|
const spell = new _spell(client, from, incantation);
|
|
const response = spell.test();
|
|
|
|
if (spell.casted) {
|
|
casted = true;
|
|
client.memory.push({
|
|
timestamp: Date.now(),
|
|
spell: spell.name
|
|
});
|
|
|
|
if (response.say) {
|
|
client.say(to, response.content);
|
|
}
|
|
}
|
|
});
|
|
|
|
if (!casted) {
|
|
client.say(to, `${from}, malsagxulo! That spell does not exist.`);
|
|
}
|
|
}
|
|
|
|
// For those who cannot handle Esperanto, plebeyoj!
|
|
const useCheatCode = (client, from, to, cheatCode) => {
|
|
let cheatCodes = {};
|
|
|
|
// Generate cheat code
|
|
client.spells.forEach((_spell) => {
|
|
const spell = new _spell(client, from, "");
|
|
cheatCodes[spell.cheatCode] = spell.spell;
|
|
});
|
|
|
|
if (contains(Object.keys(cheatCodes), cheatCode)) {
|
|
castSpell(client, from, to, cheatCodes[cheatCode]);
|
|
}
|
|
}
|
|
|
|
const checkBlacklist = (client, from, to, message) => {
|
|
const now = Date.now();
|
|
const lastUsed = client.memory.slice(-1).pop();
|
|
const lastTimestamp = lastUsed ? lastUsed.timestamp : 0;
|
|
|
|
if (contains(client.blacklist, from)) {
|
|
// Log the blacklisters for later scrutiny
|
|
logBlacklisters(from, message);
|
|
|
|
client.action(to, `is ignoring ${from}. Malsaĝa!`);
|
|
|
|
return false;
|
|
}
|
|
|
|
// Add to blacklist if abusing bot
|
|
if (now < lastTimestamp + client.config.timeout) {
|
|
// Add exception for author of bot
|
|
if (from !== client.config.author) {
|
|
addBlacklister(from, message);
|
|
|
|
const banishment = `Malpermesante ${from}`;
|
|
client.say(to, `${banishment}! I no longer head your words.`);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
client.addListener("message", (from, to, _message) => {
|
|
console.log(`${from} => ${to}: ${_message}`);
|
|
|
|
const message = _message.toLowerCase();
|
|
const triggerWord = client.config.triggerWord;
|
|
const address = botName.toLowerCase();
|
|
|
|
if (message.startsWith(triggerWord)) {
|
|
const incantation = message.split(triggerWord)[1].trim();
|
|
const check = checkBlacklist(client, from, to, incantation);
|
|
|
|
if (check) {
|
|
castSpell(client, from, to, incantation);
|
|
}
|
|
} else if (message.startsWith(address)) {
|
|
let cheatCode = message.split(address)[1];
|
|
|
|
// Why punish formality?
|
|
if (cheatCode.startsWith(": ")) {
|
|
cheatCode = cheatCode.split(": ")[1];
|
|
} else if (cheatCode.startsWith(", ")) {
|
|
cheatCode = cheatCode.split(", ")[1];
|
|
}
|
|
|
|
cheatCode = cheatCode.trim();
|
|
const check = checkBlacklist(client, from, to, cheatCode);
|
|
|
|
if (check) {
|
|
useCheatCode(client, from, to, cheatCode);
|
|
}
|
|
}
|
|
});
|
|
|
|
client.addListener("pm", (from, message) => {
|
|
console.log(`${from} => ME: ${message}`);
|
|
});
|
|
|
|
client.addListener("error", (message) => {
|
|
console.error(`error: ${message}`, message);
|
|
});
|
|
|
|
client.addListener("registered", () => {
|
|
// client.say("NickServ", `RECOVER ${botName} ${client.config.password}`);
|
|
client.say("NickServ", `IDENTIFY ${client.config.password}`);
|
|
});
|
|
|
|
client.connect();
|
|
|
|
client.join("#bots"); |