enwnbot

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

utils.js (4164B)


      1 const fetch = require("node-fetch");
      2 const { allowedSetters, setterAdmins, URL } = require("./config");
      3 const TUrl = require("tinyurl");
      4 const moment = require("moment-timezone");
      5 const timezones = require("./time");
      6 
      7 async function fetchData(URI) {
      8   const res = {};
      9   try {
     10     const data = await fetch(URI);
     11     const parsed = await data.json();
     12     res.list = parsed.query.categorymembers;
     13   } catch (error) {
     14     res.error = true;
     15     console.warn("Error in fetchData:", error);
     16   }
     17   return res;
     18 }
     19 
     20 function fullUrl(title) {
     21   fixedTitle = title.replace(/\?/g, "%3F");
     22   let [main, anchor] = fixedTitle.split("#");
     23   main = main.replace(/ /g, "%20");
     24   if (anchor) anchor = anchor.replace(/ /g, "_");
     25   let final;
     26   if (anchor) final = `${main}%23${anchor}`;
     27   else final = main;
     28   return `${URL}${final}`;
     29 }
     30 
     31 function getFullLink(link) {
     32   const len = link.length;
     33   const trimmed = link.substr(2, len - 4);
     34   const finalUrl = fullUrl(trimmed);
     35   return finalUrl;
     36 }
     37 
     38 function getFullTemplate(template) {
     39   const len = template.length;
     40   const word = template
     41     .substr(2, len - 4)
     42     .split("|")[0]
     43     .replace(/ /g, "%20")
     44     .replave(/\?/g, "%3F");
     45   return `${URL}Template:${word}`;
     46 }
     47 
     48 function sayTime(msg, client, channel) {
     49   let arr = msg.split(" ").filter(Boolean);
     50   while (arr[1] != "!time") arr.shift();
     51   const user = arr[2];
     52   const timezone = timezones.get(user) || user;
     53   const TZ = moment.tz.names().includes(timezone) ? timezone : "UTC";
     54   let time = moment()
     55     .tz(timezone)
     56     .format("HH:mm MMM DD");
     57   if (TZ == "UTC") time += " UTC";
     58   client.say(channel, time);
     59 }
     60 
     61 const chatSetter = {};
     62 
     63 function setthis(sender, channel, msg, client) {
     64   if (!allowedSetters.includes(sender)) {
     65     client.say(channel, `You are not permitted to access this, ${sender}.`);
     66     return;
     67   }
     68   let arr = msg.split(" ").filter(Boolean);
     69   while (arr[1] != "!SET") arr.shift();
     70   const action = arr[2];
     71   switch (action) {
     72     case "help": {
     73       client.say(
     74         channel,
     75         `${sender}: These are the commands you can use: add, get, keys, rmv, clr, mt(?).`
     76       );
     77       break;
     78     }
     79     case "add": {
     80       if (chatSetter[channel] == undefined) chatSetter[channel] = {};
     81       if (chatSetter[channel][sender] == undefined)
     82         chatSetter[channel][sender] = new Map();
     83       const val = arr.slice(4).join(" ");
     84       chatSetter[channel][sender].set(arr[3], val);
     85       client.say(channel, `Added, ${sender}.`);
     86       break;
     87     }
     88     case "get": {
     89       if (chatSetter[channel] == undefined) chatSetter[channel] = {};
     90       if (chatSetter[channel][sender] == undefined)
     91         chatSetter[channel][sender] = new Map();
     92       const val = chatSetter[channel][sender].get(arr[3]);
     93       client.say(channel, `${sender}: ${val}`);
     94       break;
     95     }
     96     case "keys": {
     97       if (chatSetter[channel] == undefined) chatSetter[channel] = {};
     98       if (chatSetter[channel][sender] == undefined)
     99         chatSetter[channel][sender] = new Map();
    100       const keys = [];
    101       for (let key of chatSetter[channel][sender].keys()) keys.push(key);
    102       client.say(channel, `${sender}: ${keys.join(", ")}`);
    103       break;
    104     }
    105     case "rmv": {
    106       if (chatSetter[channel] == undefined) chatSetter[channel] = {};
    107       if (chatSetter[channel][sender] == undefined)
    108         chatSetter[channel][sender] = new Map();
    109       chatSetter[channel][sender].delete(arr[3]);
    110       client.say(channel, `${sender}: removed`);
    111       break;
    112     }
    113     case "clr": {
    114       if (chatSetter[channel] == undefined) chatSetter[channel] = {};
    115       chatSetter[channel][sender].clear();
    116       client.say(channel, `${sender}: deleted all sets.`);
    117       break;
    118     }
    119     case "mt": {
    120       if (!setterAdmins.includes(sender) || !chatSetter[channel]) {
    121         break;
    122       }
    123       Object.keys(chatSetter[channel]).forEach(name =>
    124         chatSetter[channel][name].clear()
    125       );
    126       client.say(channel, `Cleared all sets for everyone, ${sender}.`);
    127       break;
    128     }
    129     default: {
    130       client.say(channel, `Action: ${action} not found, ${sender}.`);
    131     }
    132   }
    133 }
    134 
    135 module.exports = {
    136   fetchData,
    137   fullUrl,
    138   getFullLink,
    139   getFullTemplate,
    140   sayTime,
    141   setthis
    142 };