hubot-factoids-loche
Version:
Factoids for Hubot
91 lines (87 loc) • 3.4 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var Factoids;
Factoids = require('./factoids-core');
module.exports = function(robot) {
var factoids;
factoids = new Factoids(robot);
robot.hear(/(.+)\?/i, function(msg) {
var factoid;
factoid = factoids.get(msg.match[1]);
if (factoid && !factoid.forgotten) {
factoid.popularity++;
return msg.reply(msg.match[1] + " is " + factoid.value);
}
});
robot.hear(/^~(.+)/i, function(msg) {
var factoid, match;
if (match = /^~tell (.+?) about (.+)/i.exec(msg.message.text)) {
factoid = factoids.get(match[2]);
if (factoid && !factoid.forgotten) {
factoid.popularity++;
return msg.send(match[1] + ": " + match[2] + " is " + factoid.value);
}
} else if (match = /^~(.+?) is alias of (.+)/i.exec(msg.message.text)) {
if (factoids.set(match[1], "@" + match[2], msg.message.user.name, false)) {
return msg.reply("OK, " + match[1] + " is now an alias of " + match[2]);
}
} else if (match = /^~(.+?) is also (.+)/i.exec(msg.message.text)) {
factoid = factoids.add(match[1], match[2], msg.message.user.name);
return msg.reply("OK, " + match[1] + " is also " + match[2]);
} else if (match = /^~(.+?) is (.+)/i.exec(msg.message.text)) {
factoid = factoids.set(match[1], match[2], msg.message.user.name);
return msg.reply("OK, " + match[1] + " is " + factoid.value);
}
});
robot.respond(/forget (.+)/i, (function(_this) {
return function(msg) {
if (factoids.forget(msg.match[1])) {
return msg.reply("OK, forgot " + msg.match[1]);
} else {
return msg.reply('Not a factoid');
}
};
})(this));
robot.respond(/remember (.+)/i, (function(_this) {
return function(msg) {
var factoid;
factoid = factoids.remember(msg.match[1]);
if ((factoid != null) && !factoid.forgotten) {
return msg.reply("OK, " + msg.match[1] + " is " + factoid.value);
} else {
return msg.reply('Not a factoid');
}
};
})(this));
robot.respond(/factoids/i, function(msg) {
return msg.send(factoids.list().join('\n'));
});
robot.respond(/search (.+)/i, (function(_this) {
return function(msg) {
factoids = factoids.search(msg.match[1]);
if (factoids.length > 0) {
return msg.reply("Matched the following factoids: *" + (factoids.join('*, *')) + "*");
} else {
return msg.reply('No factoids matched');
}
};
})(this));
return robot.respond(/drop (.+)/i, (function(_this) {
return function(msg) {
var factoid, isAdmin, ref, ref1, user;
user = msg.envelope.user;
isAdmin = ((ref = robot.auth) != null ? ref.hasRole(user, 'factoids-admin') : void 0) || ((ref1 = robot.auth) != null ? ref1.hasRole(user, 'admin') : void 0);
if (isAdmin || (robot.auth == null)) {
factoid = msg.match[1];
if (factoids.drop(factoid)) {
return msg.reply("OK, " + factoid + " has been dropped");
} else {
return msg.reply("Not a factoid");
}
} else {
return msg.reply("You don't have permission to do that.");
}
};
})(this));
};
}).call(this);