3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-28 06:49:25 +01:00

Merge pull request #34 from psquid/quotes-migration

interpolatedQuote fully transplanted into quotes.js, should reduce pesky run.js reloading to near-zero
This commit is contained in:
Luke Slater 2012-03-14 07:39:10 -07:00
commit c22f869d15
4 changed files with 37 additions and 27 deletions

View File

@ -66,8 +66,14 @@ var command = function(dbot) {
} else { } else {
q[1] = q[1].trim(); q[1] = q[1].trim();
key = dbot.cleanNick(q[1]) key = dbot.cleanNick(q[1])
if(dbot.db.quoteArrs.hasOwnProperty(key)) { if(dbot.db.quoteArrs.hasOwnProperty(key) && dbot.moduleNames.include('quotes')) {
dbot.say(data.channel, q[1] + ': ' + dbot.interpolatedQuote(key)); var params = ['~q'];
key.split(' ').each((function(word) {
this.push(word);
}).bind(params));
data.message = params.join(' ');
dbot.commands[params[0]](data, params);
dbot.save();
} else { } else {
// See if it's similar to anything // See if it's similar to anything
var winnerDistance = Infinity; var winnerDistance = Infinity;

View File

@ -3,10 +3,14 @@ var puns = function(dbot) {
return { return {
'listener': function(data) { 'listener': function(data) {
if(data.user == 'reality') { if(dbot.moduleNames.include('quotes')) {
dbot.instance.say(data.channel, dbot.interpolatedQuote('realityonce')); if(data.user == 'reality') {
} else if(dbot.db.quoteArrs.hasOwnProperty(data.user.toLowerCase())) { data.message = '~q realityonce';
dbot.say(data.channel, data.user + ': ' + dbot.interpolatedQuote(data.user.toLowerCase())); } else if(dbot.db.quoteArrs.hasOwnProperty(data.user.toLowerCase())) {
data.message = '~q ' + data.user.toLowerCase();
}
var params = data.message.split(' ');
dbot.commands[params[0]](data, params);
} }
}, },

View File

@ -3,6 +3,24 @@ var quotes = function(dbot) {
var addStack = []; var addStack = [];
var rmAllowed = true; var rmAllowed = true;
// Retrieve a random quote from a given category, interpolating any quote references (~~QUOTE CATEGORY~~) within it
var interpolatedQuote = function(key, quoteTree) {
if( quoteTree !== undefined && quoteTree.indexOf( key ) != -1 ) { console.log('nrll'); return ''; }
else if( quoteTree === undefined ) quoteTree = [];
var quoteString = quotes[key].random();
var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/g);
var thisRef;
while( quoteRefs && (thisRef = quoteRefs.shift()) !== undefined ) {
var cleanRef = dbot.cleanNick(thisRef.replace(/^~~/,'').replace(/~~$/,'').trim());
if (quotes.hasOwnProperty(cleanRef)) {
quoteTree.push( key );
quoteString = quoteString.replace("~~"+cleanRef+"~~", interpolatedQuote(cleanRef, quoteTree.slice()));
quoteTree.pop();
}
}
return quoteString;
};
var commands = { var commands = {
'~q': function(data, params) { '~q': function(data, params) {
var q = data.message.valMatch(/^~q ([\d\w\s-]*)/, 2); var q = data.message.valMatch(/^~q ([\d\w\s-]*)/, 2);
@ -10,7 +28,7 @@ var quotes = function(dbot) {
q[1] = q[1].trim(); q[1] = q[1].trim();
key = q[1].toLowerCase(); key = q[1].toLowerCase();
if(quotes.hasOwnProperty(key)) { if(quotes.hasOwnProperty(key)) {
dbot.say(data.channel, q[1] + ': ' + dbot.interpolatedQuote(key)); dbot.say(data.channel, q[1] + ': ' + interpolatedQuote(key));
} else { } else {
dbot.say(data.channel, 'Nobody loves ' + q[1]); dbot.say(data.channel, 'Nobody loves ' + q[1]);
} }
@ -192,11 +210,11 @@ var quotes = function(dbot) {
'~rq': function(data, params) { '~rq': function(data, params) {
var rQuote = Object.keys(quotes).random(); var rQuote = Object.keys(quotes).random();
dbot.say(data.channel, rQuote + ': ' + dbot.interpolatedQuote(rQuote)); dbot.say(data.channel, rQuote + ': ' + interpolatedQuote(rQuote));
}, },
'~d': function(data, params) { '~d': function(data, params) {
dbot.say(data.channel, data.user + ': ' + dbot.interpolatedQuote(dbot.name)); dbot.say(data.channel, data.user + ': ' + interpolatedQuote(dbot.name));
}, },
'~link': function(data, params) { '~link': function(data, params) {

18
run.js
View File

@ -66,24 +66,6 @@ var DBot = function(timers) {
this.instance.connect(); this.instance.connect();
}; };
// Retrieve a random quote from a given category, interpolating any quote references (~~QUOTE CATEGORY~~) within it
DBot.prototype.interpolatedQuote = function(key, quoteTree) {
if( quoteTree !== undefined && quoteTree.indexOf( key ) != -1 ) { console.log('nrll'); return ''; }
else if( quoteTree === undefined ) quoteTree = [];
var quoteString = this.db.quoteArrs[key].random();
var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/g);
var thisRef;
while( quoteRefs && (thisRef = quoteRefs.shift()) !== undefined ) {
var cleanRef = this.cleanNick(thisRef.replace(/^~~/,'').replace(/~~$/,'').trim());
if (this.db.quoteArrs.hasOwnProperty(cleanRef)) {
quoteTree.push( key );
quoteString = quoteString.replace("~~"+cleanRef+"~~", this.interpolatedQuote(cleanRef, quoteTree.slice()));
quoteTree.pop();
}
}
return quoteString;
};
// Say something in a channel // Say something in a channel
DBot.prototype.say = function(channel, data) { DBot.prototype.say = function(channel, data) {
this.instance.say(channel, data); this.instance.say(channel, data);