From c5cc605bf1f6be076a4e1d14dfc7eecc63ac75c0 Mon Sep 17 00:00:00 2001 From: Eric Pai Date: Tue, 3 May 2016 02:59:31 -0700 Subject: [PATCH] archived old functions --- functions(archived)/emoji.js | 33 +++++ .../expand_hms.js | 0 functions(archived)/hmsspace.js | 40 ++++++ functions(archived)/index.js | 6 + .../markovFn.js | 0 functions(archived)/mention.js | 108 +++++++++++++++ functions(archived)/reminders.js | 124 ++++++++++++++++++ .../songlinkify.js | 0 {functions => functions(archived)}/test.js | 0 9 files changed, 311 insertions(+) create mode 100644 functions(archived)/emoji.js rename {functions => functions(archived)}/expand_hms.js (100%) create mode 100644 functions(archived)/hmsspace.js create mode 100644 functions(archived)/index.js rename {functions => functions(archived)}/markovFn.js (100%) create mode 100644 functions(archived)/mention.js create mode 100644 functions(archived)/reminders.js rename {functions => functions(archived)}/songlinkify.js (100%) rename {functions => functions(archived)}/test.js (100%) diff --git a/functions(archived)/emoji.js b/functions(archived)/emoji.js new file mode 100644 index 0000000..c582798 --- /dev/null +++ b/functions(archived)/emoji.js @@ -0,0 +1,33 @@ +/* + * Module for emoji actions in the bot. + * + * Currently very naive: the matchPattern just matches any occurrence of + * any of the emoji triggers (e.g. *shrug*), then the action checks for the + * presence of one of them. Note that it will only actually send a message for + * the first trigger it finds, not all of them. +*/ + +module.exports.matchPattern = /(\*shrug\*)|(\/stare)|(\/lenny)/g; + +module.exports.action = function(api, message, cb) { + if (message.body.indexOf("*shrug*") != -1) { + api.sendMessage("¯\\_(ツ)_/¯", message.threadID); + } else if (message.body.indexOf("/stare") == 0) { + var possibleName = message.body.split(" ")[1]; + var isName = false; + if (possibleName) { + for (var i in message.participantNames) { + if (message.participantNames[i].split(" ")[0].toLowerCase() == possibleName) { + isName = true; + } + } + } + + var msg = "ಠ_ಠ"; + if (isName) + msg = "@" + possibleName + ": " + msg; + api.sendMessage(msg, message.threadID); + } else if (message.body.indexOf("/lenny") == 0) { + api.sendMessage("( ͡° ͜ʖ ͡° )", message.threadID); + } +} diff --git a/functions/expand_hms.js b/functions(archived)/expand_hms.js similarity index 100% rename from functions/expand_hms.js rename to functions(archived)/expand_hms.js diff --git a/functions(archived)/hmsspace.js b/functions(archived)/hmsspace.js new file mode 100644 index 0000000..6b06f8c --- /dev/null +++ b/functions(archived)/hmsspace.js @@ -0,0 +1,40 @@ +var key = require("../login")['hmsspacekey']; +var email = require("../login")['creatoremail']; +var request = require("request"); + +module.exports.matchPattern = /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g; + +module.exports.action = function (api, message, cb) { + var target = "http://hms.space/api/add"; + var allLinks = message.body.match(module.exports.matchPattern); + for (var i in allLinks) { + var toShorten = allLinks[i]; + request.post({ + url: target, + form: { + "apiKey": key, + "creator": message.senderName, + "target": toShorten, + "chatID": message.threadID, + "chatName": message.threadName, + } + }, function(err, httpResponse, body){ + if (err) { + console.log(err); + return setImmediate(cb); + } else { + var res = JSON.parse(body); + if (res["Success"]) { + var url = res["ResultURL"]; + //api.sendMessage(url, message.threadID); + return setImmediate(cb); + } else { + if (body.indexOf("redirect loops") != -1) + return setImmediate(cb); + console.error(res); + api.sendMessage("hms.space fucked up. Blame @jordon wing.", message.threadID); + } + } + }); + } +} diff --git a/functions(archived)/index.js b/functions(archived)/index.js new file mode 100644 index 0000000..f8a0425 --- /dev/null +++ b/functions(archived)/index.js @@ -0,0 +1,6 @@ +require("fs").readdirSync(__dirname + "/").forEach(function (filename) { + if (filename.match(/\.js$/) && filename != "index.js") { + var fname = filename.replace(".js", ""); + module.exports[fname] = require("./" + filename); + } +}); diff --git a/functions/markovFn.js b/functions(archived)/markovFn.js similarity index 100% rename from functions/markovFn.js rename to functions(archived)/markovFn.js diff --git a/functions(archived)/mention.js b/functions(archived)/mention.js new file mode 100644 index 0000000..f4b7cc3 --- /dev/null +++ b/functions(archived)/mention.js @@ -0,0 +1,108 @@ +async = require("async"); +cache = require("../mentionsCache"); +var DISALLOWED_MENTION_IDS = require("../login.js").DISALLOWED_MENTION_IDS; + +module.exports.matchPattern = /@/; +var idCache = new cache.MentionsCache(); +module.exports.cache = idCache; + +function sendMentionMessage(api, mentionedName, mentionedID, message, callback) { + if (DISALLOWED_MENTION_IDS.indexOf(mentionedID) != -1) + return setImmediate(callback); + else if (mentionedID == message['senderID'] && mentionedName == "everyone") + return setImmediate(callback); + api.getUserInfo(message['senderID'], function (err, info) { + if (err) { + console.log(err); + return setImmediate(callback); + } + else { + var mentionedFullName = info[Object.getOwnPropertyNames(info)[0]].name; + var messageText = mentionedFullName; + console.log("Sending to: ", mentionedID); + + if (mentionedName == "everyone") + messageText += " group"; + + messageText += " mentioned you"; + if (message.threadName) { + if (message.threadName == mentionedFullName) { + messageText += " in your personal chat"; + } else { + messageText += " in " + message.threadName; + } + } + messageText += ": " + message.body; + api.sendMessage(messageText, mentionedID); + if (mentionedName != "everyone") + idCache.addToCache(message.threadID, mentionedName, mentionedID); + return setImmediate(callback); + } + }); +} + +function buildCache(api, threadID, participantIDs, cb) { + api.getUserInfo(participantIDs, function(err, results) { + if (err) { + console.error(err); + return; + } + + for (var userID in results) { + idCache.addToCache(threadID, results[userID].name, userID); + } + if (cb) { + cb(); + } + }); +} + +module.exports.onMessage = function(api, message, cb) { + // Make sure we have the ID of everyone in the chat + // Since get user info is asynchronous, this is technically a race condition on first load + // But that's almost never going to be an issue. + if (message.body.indexOf("@") != -1) { + // quit now. mention action will take care of the cache. + return; + } + else if (message.participantIDs.length > idCache.getSize(message.threadID)) + buildCache(api, message.threadID, message.participantIDs, cb); +} + +//@name lastname must be the format for mentions. They can be put in the middle of sentences. Multiple mentions also work. +module.exports.action = function (api, message, cb) { + if (message.participantIDs.length > idCache.getSize(message.threadID)) + return buildCache(api, message.threadID, message.participantIDs, function() { + module.exports.action(api, message, cb); + }); + async.forEach(message.body.split(module.exports.matchPattern).slice(1), function (frag, callback) { + + if (frag.slice(0, 8) == "everyone") { + var ids = idCache.getAllIDs(message.threadID); + for (var i in ids) { + sendMentionMessage(api, "everyone", ids[i], message, function(){}); + } + return setImmediate(callback); + } + mentioned = frag.split(" ").slice(0,2).join(" "); + var id = idCache.getID(message.threadID, mentioned); + if (id) + return sendMentionMessage(api, mentioned, id, message, callback); + + api.getUserID(mentioned, function (err, ids) { + console.log("Had to fetch manually. Current cache: ", Object.keys(idCache[message.threadID] || {})); + if (err) { + console.log(err); + var errstring = err['error']; + //api.sendMessage({body: errstring}, message.threadID); + return setImmediate(callback); + } else if (message.participantIDs.indexOf(ids[0]['userID']) == -1 && message.participantIDs.indexOf(parseInt(ids[0]['userID'])) == -1) { + console.log("You can only mention people that are in the current thread."); + api.sendMessage("You can only mention people that are in the current thread.", message.threadID); + } else { + var validID = ids[0]['userID']; + return sendMentionMessage(api, mentioned, validID, message, callback); + } + }); + }, cb); +} diff --git a/functions(archived)/reminders.js b/functions(archived)/reminders.js new file mode 100644 index 0000000..68c6f1a --- /dev/null +++ b/functions(archived)/reminders.js @@ -0,0 +1,124 @@ +/* + * Module for reminders. + * + * Not designed for robust string processing, and currently + * just uses a setTimer call to set the reminder. +*/ +var idCache = require("./mention.js")['cache']; +module.exports.matchPattern = /^(F|f)rankie[,:]? remind me/g + +var TIME_UNITS = { + 'day': 24*60*60, + 'days': 24*60*60, + 'hour': 60*60, + 'hours': 60*60, + 'minute': 60, + 'minutes': 60, + 'second': 1, + 'seconds': 1 +} + +toHHMMSS = function (date) { + var hours = (date.getHours() % 12); + var minutes = date.getMinutes(); + var seconds = date.getSeconds(); + + //if (hours < 10) {hours = "0"+hours;} + if (minutes < 10) {minutes = "0"+minutes;} + if (seconds < 10) {seconds = "0"+seconds;} + var time = hours+':'+minutes + return time; + +} + +function getTimeArray(s) { + var timeRegex = /(\d+):(\d+)[:(\d+)]?/g + var match = timeRegex.exec(s); + if (match === null) + return null; + console.log(match); + + var result = match.splice(1); + result[0] = parseInt(result[0]); + result[1] = parseInt(result[1]); + if (result.length == 2) + result.push(0); + + return result; +} + + +module.exports.action = function(api, message, cb) { + var tokens = message.body.split(" ").splice(1); + + var stage = 1; + var timeValue, units, numSeconds; + var timeFormat = "delta"; + var msg = ""; + for (var i in tokens) { + var token = tokens[i]; + + if (stage == 1) { + if (token == "in") + continue; + else if (token == "at") { + timeFormat = "abs"; + continue; + } + + if (timeFormat == "abs") { + var timeArray = getTimeArray(token); + if (timeArray == null) { + break; + } + + var timerDate = new Date(); + timerDate.setHours(timeArray[0]); + timerDate.setMinutes(timeArray[1]); + timerDate.setSeconds(timeArray[2]); + + //console.log(timeArray); + //console.log(timerDate); + + numSeconds = Math.round((timerDate - new Date()) / 1000); + //console.log(numSeconds); + if (numSeconds < 0) + break; + stage = 2; + } else if (timeFormat == "delta") { + var t = parseInt(token); + if (!timeValue && !isNaN(t) && t != 0) { + timeValue = t; + continue; + } else if (TIME_UNITS[token]) { + units = token; + numSeconds = timeValue * (TIME_UNITS[token]); + stage = 2; + } + } + } else { + if (msg != "") + msg += " " + msg += token; + } + } + + if (stage != 2 || msg == "") { + // Bad reminder. + api.sendMessage("Bad reminder. Format: " + + "Frankie remind me in