commit 2a2ec08894f042959b08ea37e702133e5ba0d6dc
parent c9d312d49d2b73986e39bb4f5b1ea56fe91401ed
Author: Agastya Chandrakant <acagastya@outlook.com>
Date: Fri, 23 Oct 2020 17:14:25 +0530
Add seen feature
Diffstat:
M | index.js | | | 71 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
1 file changed, 70 insertions(+), 1 deletion(-)
diff --git a/index.js b/index.js
@@ -25,6 +25,8 @@ const {
thanksRegex
} = require("./utils");
+const seenLookup = {};
+
const ircClient = new irc.Client(ircServer, ircBotName, { channels });
console.log("Connecting to the event stream...");
@@ -47,17 +49,66 @@ ircClient.addListener("error", function(message) {
});
ircClient.addListener("pm", function(sender, msg) {
- if(msg == "KILL" && ['pizero', 'pizero|afk', 'acagastya'].indexOf(sender) >= 0) process.abort();
+ if (
+ msg == "KILL" &&
+ ["pizero", "pizero|afk", "acagastya"].indexOf(sender) >= 0
+ )
+ process.abort();
ircClient.say(sender, "I am a bot.");
});
ircClient.addListener("message", groupChat);
+ircClient.addListener("names", getNames);
+
+ircClient.addListener("nick", handleNickChange);
+
+ircClient.addListener("join", handleJoin);
+
+ircClient.addListener("part", handlePart);
+
+ircClient.addListener("quit", handleQuit);
+
+function handleJoin(channel, nick) {
+ if (seenLookup[channel] === undefined) seenLookup[channel] = {};
+ seenLookup[channel][nick] = "";
+}
+
+function getNames(channel, nicks) {
+ seenLookup[channel] = { ...nicks };
+ Object.keys(seenLookup[channel]).forEach(
+ nick => (seenLookup[channel][nick] = "")
+ );
+}
+
+function handlePart(channel, nick) {
+ if (seenLookup[channel] === undefined) seenLookup[channel] = {};
+ seenLookup[channel][nick] = +new Date();
+}
+
+function handleQuit(nick, reason, channels) {
+ channels.forEach(channel => {
+ if (seenLookup[channel] !== undefined) {
+ seenLookup[channel][nick] = +new Date();
+ }
+ });
+}
+
+function handleNickChange(oldNick, newNick, channels) {
+ channels.forEach(channel => {
+ if (seenLookup[channel] !== undefined) {
+ seenLookup[channel][oldNick] = +new Date();
+ seenLookup[channel][newNick] = "";
+ }
+ });
+}
+
function groupChat(sender, channel, msg) {
if (thanksRegex.test(msg))
ircClient.say(channel, `You are welcome, ${sender}.`);
if (msg.includes(`${ircBotName} !RQ`)) announceRQ(sender, channel);
if (msg.includes(`${ircBotName} !UR`)) announceUR(sender, channel);
+ if (msg.startsWith("@seen ")) handleSeen(channel, msg);
const links = msg.match(linkRegex);
const templates = msg.match(templateRegex);
@@ -125,6 +176,24 @@ async function announceUR(sender, channel) {
}
}
+function handleSeen(channel, msg) {
+ const username = msg.split("@seen ")[1];
+ if (seenLookup[channel] === undefined) return;
+ if (seenLookup[channel][username] === undefined) {
+ ircClient.say(channel, `I have never seen ${username} on ${channel}.`);
+ return;
+ }
+ if (seenLookup[channel][username] === "") {
+ ircClient.say(channel, `${username} is online.`);
+ return;
+ }
+ const time = new Date(seenLookup[channel][username]).toUTCString();
+ ircClient.say(
+ channel,
+ `${username} was last onilne on ${channel} at ${time}.`
+ );
+}
+
function sayUrls(
review = false,
urlList,