enwnbot

Converts MediaWiki [[links]] and {{templates}} to links on IRC
git clone http://git.hanabi.in/repos/enwnbot.git
Log | Files | Refs | README | LICENSE

commit 030a393535f229cad26834cfabcbd16852bd749f
parent b924dbb66a63784c5eadd26367e0c0eb5780d583
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Fri, 19 Jun 2020 22:37:07 +0530

Add SET option

Diffstat:
M.gitignore | 5+++--
Mconfig.js | 4+++-
Mirc.js | 2++
Mutils.js | 77++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 84 insertions(+), 4 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ node_modules rss.js -test.js -\ No newline at end of file +test.js +config.bak.js +\ No newline at end of file diff --git a/config.js b/config.js @@ -6,6 +6,8 @@ const config = { channels: ['#foo', '##bar'], maintainers: ['ssmith'], report: '!ADMIN', + setterAdmins: ['matt', 'tony'], + allowedSetters: ['nick', 'nat'], RQAPI: 'https://en.wikinews.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Review&format=json&cmsort=timestamp&cmprop=timestamp|ids|title', server: 'irc.freenode.net', @@ -13,7 +15,7 @@ const config = { 'https://meta.wikimedia.org/w/api.php?action=shortenurl&format=json&url=', URL: 'https://en.wikinews.org/w/index.php?title=', URAPI: - 'https://en.wikinews.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Under%20review&format=json&cmsort=timestamp&cmprop=title', + 'https://en.wikinews.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Under%20review&format=json&cmsort=timestamp&cmprop=title|timestamp', }; module.exports = config; diff --git a/irc.js b/irc.js @@ -19,6 +19,7 @@ const { getFullLink, getFullTemplate, sayTime, + setthis, } = require('./utils'); const { fallback, reset, short } = require('./promUrlShortener'); @@ -118,6 +119,7 @@ function groupChat(sender, channel, msg) { if (msg.includes(`${botName} !UR`)) announceUR(sender, channel); if (msg.includes(`${botName} !FB`)) fallback(); if (msg.includes(`${botName} !TRY`)) reset(); + if (msg.includes(`${botName} !SET`)) setthis(sender, channel, msg, client); if (msg.includes(`${botName} !time`)) sayTime(msg, client, channel); const regex1 = /\[{2}(.*?)\]{2}/g; const regex2 = /\{{2}(.*?)\}{2}/g; diff --git a/utils.js b/utils.js @@ -1,5 +1,5 @@ const fetch = require('node-fetch'); -const { shortURL, URL } = require('./config'); +const { allowedSetters, setterAdmins, URL } = require('./config'); const TUrl = require('tinyurl'); const moment = require('moment-timezone'); const timezones = require('./time'); @@ -54,10 +54,85 @@ function sayTime(msg, client, channel) { client.say(channel, time); } +const chatSetter = {}; + +function setthis(sender, channel, msg, client) { + if (!allowedSetters.includes(sender)) { + client.say(channel, `You are not permitted to access this, ${sender}.`); + return; + } + let arr = msg.split(' ').filter(Boolean); + while (arr[1] != '!SET') arr.shift(); + const action = arr[2]; + switch (action) { + case 'help': { + client.say( + channel, + `${sender}: These are the commands you can use: add, get, keys, rmv, clr, mt(?).` + ); + break; + } + case 'add': { + if (chatSetter[channel] == undefined) chatSetter[channel] = {}; + if (chatSetter[channel][sender] == undefined) + chatSetter[channel][sender] = new Map(); + const val = arr.slice(4).join(' '); + chatSetter[channel][sender].set(arr[3], val); + client.say(channel, `Added, ${sender}.`); + break; + } + case 'get': { + if (chatSetter[channel] == undefined) chatSetter[channel] = {}; + if (chatSetter[channel][sender] == undefined) + chatSetter[channel][sender] = new Map(); + const val = chatSetter[channel][sender].get(arr[3]); + client.say(channel, `${sender}: ${val}`); + break; + } + case 'keys': { + if (chatSetter[channel] == undefined) chatSetter[channel] = {}; + if (chatSetter[channel][sender] == undefined) + chatSetter[channel][sender] = new Map(); + const keys = []; + for (let key of chatSetter[channel][sender].keys()) keys.push(key); + client.say(channel, `${sender}: ${keys.join(', ')}`); + break; + } + case 'rmv': { + if (chatSetter[channel] == undefined) chatSetter[channel] = {}; + if (chatSetter[channel][sender] == undefined) + chatSetter[channel][sender] = new Map(); + chatSetter[channel][sender].delete(arr[3]); + client.say(channel, `${sender}: removed`); + break; + } + case 'clr': { + if (chatSetter[channel] == undefined) chatSetter[channel] = {}; + chatSetter[channel][sender].clear(); + client.say(channel, `${sender}: deleted all sets.`); + break; + } + case 'mt': { + if (!setterAdmins.includes(sender) || !chatSetter[channel]) { + break; + } + Object.keys(chatSetter[channel]).forEach((name) => + chatSetter[channel][name].clear() + ); + client.say(channel, `Cleared all sets for everyone, ${sender}.`); + break; + } + default: { + client.say(channel, `Action: ${action} not found, ${sender}.`); + } + } +} + module.exports = { fetchData, fullUrl, getFullLink, getFullTemplate, sayTime, + setthis, };