-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated eventloop, cache, functions for new api
- Loading branch information
Showing
7 changed files
with
242 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
var Cache = function() { | ||
this.api = undefined; | ||
this.namesCache = {}; // id : name | ||
this.threadsCache = {}; // id : {name : threadName, ids : participantIDs} | ||
} | ||
|
||
Cache.prototype.addNamesInfo = function(participantIDs, cb) { | ||
var cache = this.namesCache; | ||
var newIDs = []; | ||
for (var i=0; i < participantIDs.length; i++) { | ||
var id = participantIDs[i]; | ||
if (!cache[id]) | ||
newIDs.push(id); | ||
} | ||
if (newIDs.length > 0) { | ||
this.api.getUserInfo(newIDs, function(err, ret) { | ||
if (err) return console.error(err); | ||
for (var id in ret) | ||
if (ret.hasOwnProperty(id)) | ||
cache[id] = ret[id].name; | ||
cb(); | ||
}); | ||
} else { | ||
cb(); | ||
} | ||
} | ||
|
||
Cache.prototype.addThreadInfo = function(threadID, cb) { | ||
var callback = function(err, ret) { | ||
if (err) return console.error(err); | ||
this.threadsCache[threadID] = { | ||
'name':ret.name, | ||
'ids':ret.participantIDs} | ||
this.addNamesInfo(ret.participantIDs, cb); | ||
} | ||
this.api.getThreadInfo(threadID, callback.bind(this)); | ||
}; | ||
|
||
Cache.prototype.load = function(api, threadID, cb) { | ||
// all code that relies on names being in the cache should be | ||
// called in the callback (cb) of this function to ensure no | ||
// cache misses. | ||
if (this.api === undefined) | ||
this.api = api; | ||
if (!this.threadsCache[threadID]) { | ||
this.addThreadInfo(threadID, cb); | ||
} else { | ||
cb(); | ||
} | ||
} | ||
|
||
Cache.prototype.getAllParticipantIDs = function(threadID) { | ||
return this.threadsCache[threadID].ids; | ||
} | ||
|
||
Cache.prototype.senderFromID = function(senderID) { | ||
return this.namesCache[senderID]; | ||
} | ||
|
||
Cache.prototype.threadFromID = function(threadID) { | ||
return this.threadsCache[threadID].name; | ||
} | ||
|
||
Cache.prototype.getIDFromName = function(name) { | ||
name = name.toLowerCase(); | ||
var match = undefined; | ||
var matchNum = 0; | ||
for (var id in this.namesCache) { | ||
if (this.namesCache.hasOwnProperty(id)) { | ||
var cacheName = this.namesCache[id].toLowerCase(); | ||
if (cacheName === name) | ||
return id; | ||
var firstName = cacheName.split(" ")[0]; | ||
if (firstName === name) { | ||
if (!match) | ||
match = id; | ||
else | ||
return null; // match on first name must be ambiguous | ||
} | ||
} | ||
} | ||
return match; | ||
} | ||
|
||
var cache = new Cache(); | ||
module.exports.cache = cache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,23 @@ | ||
/* | ||
* Module for emoji actions in the bot. | ||
* 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.matchPattern = /\/emoji/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); | ||
if (message.threadID !== message.senderID) { | ||
cb(); | ||
return; | ||
} | ||
var emojis = [ | ||
"¯\\_(ツ)_/¯", | ||
"ಠ__ಠ", | ||
"( ͡° ͜ʖ ͡° )" | ||
]; | ||
api.sendMessage(emojis.join("\r\n"), message.threadID); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var key = require("../login")['hmsspacekey']; | ||
var email = require("../login")['creatoremail']; | ||
var request = require("request"); | ||
var cache = require("../cache"); | ||
|
||
// var cache = new Cache(); | ||
|
||
module.exports.matchPattern = /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g; | ||
|
||
// First, checks if it isn't implemented yet. | ||
if (!String.format) { | ||
String.format = function(format) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
return format.replace(/{(\d+)}/g, function(match, number) { | ||
return typeof args[number] != 'undefined' | ||
? args[number] | ||
: match | ||
; | ||
}); | ||
}; | ||
} | ||
|
||
module.exports.action = function (api, message, cb) { | ||
var links = message.body.match(module.exports.matchPattern); | ||
for (var i=0; i < links.length; i++) { | ||
var link = links[i]; | ||
console.log("found link! " + link); | ||
} | ||
} |
Oops, something went wrong.