Bit more efficient. dont bother saying the replacement if replacement not made [#154]

This commit is contained in:
reality 2013-01-21 20:14:51 +00:00
parent 400d215734
commit 24bb198c5d

View File

@ -3,19 +3,23 @@ var _ = require('underscore')._;
var regex = function(dbot) { var regex = function(dbot) {
this.last = {}; this.last = {};
this.listener = function(event) { this.listener = function(event) {
var q = event.message.valMatch(/^s\/(.+)\/(.+)\/$/, 3); var q = event.message.valMatch(/^([\d\w\s]*)?:? ?s\/(.+)\/(.+)\/$/, 4);
var otherQ = event.message.valMatch(/^([\d\w\s]*): s\/(.+)\/(.+)\/$/, 4);
if(q) { if(q) {
var toMatch = new RegExp(q[1]); var toMatch = new RegExp(q[2]),
var replaceWith = q[2]; replaceWith = q[3],
var last = this.last[event.channel.name][event.user]; last,
event.reply(event.user + " meant: " + last.replace(toMatch, replaceWith)); replacement;
} else if(otherQ) {
var user = otherQ[1]; if(q[1] != null) {
var toMatch = new RegExp(otherQ[2]); var user = q[1];
var replaceWith = otherQ[3]; last = this.last[event.channel.name][user];
var last = this.last[event.channel.name][user]; replacement = last.replace(toMatch, replaceWith);
event.reply(event.user + " thinks " + user + " meant: " + last.replace(toMatch, replaceWith)); if(replacement != last) event.reply(event.user + " thinks " + user + " meant: " + replacement);
} else {
last = this.last[event.channel.name][event.user];
replacement = last.replace(toMatch, replaceWith);
if(replacement != last) event.reply(event.user + " meant: " + replacement);
}
} else { } else {
if(_.has(this.last, event.channel.name)) { if(_.has(this.last, event.channel.name)) {
this.last[event.channel.name][event.user] = event.message; this.last[event.channel.name][event.user] = event.message;