3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-27 21:22:38 +01:00
dbot/modules/regex/regex.js

44 lines
1.4 KiB
JavaScript
Raw Normal View History

2013-01-21 20:42:51 +01:00
var _ = require('underscore')._;
var regex = function(dbot) {
this.last = {};
this.listener = function(event) {
var q = event.message.valMatch(/^([\d\w\s]*)?:? ?s\/(.+)\/(.+)?\/([ig]*)?$/, 5);
2013-01-21 20:42:51 +01:00
if(q) {
2013-01-21 21:32:34 +01:00
var flags = q[4],
toMatch = new RegExp(q[2], flags),
replaceWith = q[3],
last,
2013-08-05 18:03:30 +02:00
replacement,
user;
2013-01-22 12:22:55 +01:00
if(!replaceWith) replaceWith = "";
2013-08-05 18:03:30 +02:00
user = q[1] || event.user;
last = this.last[event.channel.name][user];
replacement = last.replace(toMatch, replaceWith);
replacement = replacement.replace(/^.ACTION/, user).replace("\x01", '');
if(replacement != last) {
if(q[1]) {
event.reply(event.user + " thinks " + user + " meant: " + replacement);
} else {
event.reply(event.user + " meant: " + replacement);
}
}
2013-01-21 20:42:51 +01:00
} else {
if(_.has(this.last, event.channel.name)) {
this.last[event.channel.name][event.user] = event.message;
} else {
this.last[event.channel.name] = { };
this.last[event.channel.name][event.user] = event.message;
}
}
}.bind(this);
this.on = [ 'PRIVMSG' ];
};
exports.fetch = function(dbot) {
return new regex(dbot);
};