dbot/modules/quotes/quotes.js

172 lines
5.8 KiB
JavaScript
Raw Normal View History

var _ = require('underscore')._,
uuid = require('node-uuid');
var quotes = function(dbot) {
2013-01-15 15:51:07 +01:00
dbot.sessionData.rmCache = [];
this.rmCache = dbot.sessionData.rmCache;
this.quotes = dbot.db.quoteArrs;
this.rmAllowed = true;
2013-01-15 15:47:46 +01:00
this.rmTimer;
2013-01-15 15:47:46 +01:00
this.internalAPI = {
// Parse quote interpolations
'interpolatedQuote': function(server, channel, key, quote, callback) {
var quoteRefs = quote.match(/~~([\d\w\s-]*)~~/g);
if(quoteRefs) {
var ref = this.internalAPI.cleanRef(quoteRefs[0].replace(/^~~/,'').replace(/~~$/,'').trim());
if(ref === '-nicks-') {
dbot.api.users.getRandomChannelUser(server, channel, function(user) {
quote = quote.replace('~~' + ref + '~~', randomNick);
this.internalAPI.interpolatedQuote(server, channel, key, quote, callback);
}.bind(this));
} else {
this.api.getQuote(ref, function(interQuote) {
if(!interQuote || ref == key) {
interQuote = '';
}
quote = quote.replace('~~' + ref + '~~', interQuote);
this.internalAPI.interpolatedQuote(server, channel, key, quote, callback);
}.bind(this));
2013-01-15 15:47:46 +01:00
}
} else {
callback(quote);
2013-01-15 15:47:46 +01:00
}
}.bind(this),
'cleanRef': function(key) {
key = key.toLowerCase();
while(key.endsWith("_")) {
key = key.substring(0, key.length-1);
}
return key;
},
2013-01-15 15:47:46 +01:00
'resetRemoveTimer': function(event, key, quote) {
this.rmAllowed = false;
2013-01-27 21:52:11 +01:00
setTimeout(function() {
2013-01-15 15:47:46 +01:00
this.rmAllowed = true;
2013-01-27 21:52:11 +01:00
}.bind(this), 5000);
2013-01-15 15:47:46 +01:00
this.rmCache.push({
'key': key,
'quote': quote
});
2013-01-15 15:47:46 +01:00
clearTimeout(this.rmTimer);
2013-01-15 15:47:46 +01:00
if(this.rmCache.length < dbot.config.quotes.rmLimit) {
2013-01-27 21:52:11 +01:00
this.rmTimer = setTimeout(function() {
2013-01-15 15:47:46 +01:00
this.rmCache.length = 0; // lol what
2013-01-27 21:52:11 +01:00
}.bind(this), 600000);
2013-01-15 15:47:46 +01:00
} else {
_.each(dbot.config.admins, function(admin) {
dbot.say(event.server, admin, dbot.t('rm_cache_limit'));
});
}
}.bind(this)
};
2013-01-15 15:47:46 +01:00
this.api = {
'addQuote': function(key, quote, user, callback) {
var key = key.toLowerCase().trim(),
newCount,
category = false;
this.db.search('quote_category', { 'name': key }, function(result) {
category = result;
}, function(err) {
if(!category) {
var id = uuid.v4();
category = {
'id': id,
'name': key,
'quotes': [],
'owner': user
};
}
if(_.include(category.quotes, quote)) {
callback(false);
} else {
newCount = category.quotes.push(quote);
this.db.save('quote_category', category.id, category, function(err) {
this.rmAllowed = true;
callback(newCount);
}.bind(this));
}
}.bind(this));
},
'getQuote': function(key, callback) {
2013-04-23 19:26:44 +02:00
this.api.getQuoteCategory(key, function(category) {
if(category) {
var quotes = category.quotes;
var index = _.random(0, quotes.length - 1);
callback(quotes[index]);
2013-01-03 22:27:06 +01:00
} else {
callback(false);
2013-01-03 22:27:06 +01:00
}
});
},
'getInterpolatedQuote': function(server, channel, key, callback) {
key = key.trim().toLowerCase(),
this.api.getQuote(key, function(quote) {
if(quote) {
this.internalAPI.interpolatedQuote(server, channel, key, quote, callback);
} else {
callback(quote);
}
}.bind(this));
2013-04-23 19:26:44 +02:00
},
'getQuoteCategory': function(key, callback) {
var category = false,
key = key.trim().toLowerCase();
this.db.search('quote_category', { 'name': key }, function(result) {
category = result;
}, function(err) {
callback(category);
});
},
'getCategoryKeys': function(callback) {
var keys = [];
this.db.scan('quote_category', function(result) {
if(result) keys.push(result.name);
}, function(err) {
callback(keys);
});
2013-01-03 22:27:06 +01:00
}
};
2013-01-15 15:47:46 +01:00
this.listener = function(event) {
if(event.action == 'PRIVMSG') {
if(event.user == 'reality') {
var once = event.message.valMatch(/^I ([\d\w\s,'-]* once)/, 2);
} else {
2013-01-15 15:47:46 +01:00
var once = event.message.valMatch(/^reality ([\d\w\s,'-]* once)/, 2);
}
2013-01-15 15:47:46 +01:00
if(once) {
this.api.addQuote('realityonce', 'reality' + once[1], event.user, function(newCount) {
event.reply('\'reality ' + once[1] + '\' saved (' + newCount + ').');
});
2013-01-15 15:47:46 +01:00
}
} else if(event.action == 'JOIN') {
2013-04-20 12:51:39 +02:00
var userQuote = this.api.getQuote(event.user, function(quote) {
2013-04-12 23:16:56 +02:00
if(quote) {
event.reply(event.user + ': ' + quote);
}
});
2013-01-15 15:47:46 +01:00
}
}.bind(this);
this.on = ['PRIVMSG', 'JOIN'];
};
exports.fetch = function(dbot) {
2013-01-15 15:47:46 +01:00
return new quotes(dbot);
};