Skip to content

Commit

Permalink
archived old functions
Browse files Browse the repository at this point in the history
  • Loading branch information
epai committed May 3, 2016
1 parent f7f6c5b commit c5cc605
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 0 deletions.
33 changes: 33 additions & 0 deletions functions(archived)/emoji.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
File renamed without changes.
40 changes: 40 additions & 0 deletions functions(archived)/hmsspace.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
});
}
}
6 changes: 6 additions & 0 deletions functions(archived)/index.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
File renamed without changes.
108 changes: 108 additions & 0 deletions functions(archived)/mention.js
Original file line number Diff line number Diff line change
@@ -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);
}
124 changes: 124 additions & 0 deletions functions(archived)/reminders.js
Original file line number Diff line number Diff line change
@@ -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 <time> to <action>", message.threadID);
return;
}

var reminderTime = new Date();
reminderTime.setSeconds(reminderTime.getSeconds() + numSeconds);

var timeStr = toHHMMSS(reminderTime);
//reminderTime.getHours() + ":" + reminderTime.getMinutes();
api.sendMessage("I'll remind you at " + timeStr, message.threadID);

setTimeout(function() {
api.sendMessage("You asked me to remind you " + msg +
" at " + timeStr, message.senderID);
}, numSeconds * 1000);
}
File renamed without changes.
File renamed without changes.

0 comments on commit c5cc605

Please sign in to comment.