ability to set a custom resolver function on commands to gain info, applied this pattern to lastfm

This commit is contained in:
reality 2013-10-25 15:26:53 +00:00
parent 956b947a4c
commit 0fac0ac948
2 changed files with 134 additions and 115 deletions

View File

@ -75,8 +75,18 @@ var command = function(dbot) {
} else if(!isIgnoring && _.has(dbot.commands, commandName) && !dbot.commands[commandName].disabled) {
if(this.api.applyRegex(commandName, event)) {
try {
var command = dbot.commands[commandName];
var results = command.apply(dbot.modules[command.module], [event]);
var command = dbot.commands[commandName],
results;
if(_.has(command, 'resolver')) {
event.res = [];
command.resolver(event, function(err) {
if(!err) {
results = command.apply(dbot.modules[command.module], [event]);
}
});
} else {
results = command.apply(dbot.modules[command.module], [event]);
}
} catch(err) {
if(dbot.config.debugMode == true) {
var stack = err.stack.split('\n').slice(1, dbot.config.debugLevel + 1);

View File

@ -116,7 +116,6 @@ var lastfm = function(dbot) {
this.commands = {
'~suggestion': function(event) {
if(event.rProfile && _.has(event.rProfile, 'lastfm')) {
this.api.getListening(event.rProfile.lastfm, function(err, track) {
if(!err) {
this.api.getSimilarArtists(track.artist.mbid, function(err, similar) {
@ -156,14 +155,16 @@ var lastfm = function(dbot) {
}
}
}.bind(this));
} else {
event.reply(event.user + ': Set a lastfm username with "~set lastfm username"');
}
},
'~listening': function(event) {
var getListening = function(user, lfm) {
if(user && lfm) {
var user = event.rUser,
lfm = event.rProfile.lastfm;
if(event.res[0]) {
user = event.res[0].user;
lfm = event.res[0].lfm;
}
this.api.getListening(lfm, function(err, track) {
if(!err) {
var term = track.name + ' ' + track.artist['#text'],
@ -199,26 +200,14 @@ var lastfm = function(dbot) {
}
}
});
} else {
if(!user) {
event.reply('Unknown user.');
} else {
event.reply(user.currentNick + ': Set a lastfm username with "~set lastfm username"');
}
}
}.bind(this);
if(event.params[1]) {
this.internalAPI.getLastFM(event.server, event.params[1], getListening);
} else {
getListening(event.rUser, event.rProfile.lastfm);
}
},
'~taste': function(event) {
if(event.rProfile && _.has(event.rProfile, 'lastfm')) {
this.internalAPI.getLastFM(event.server, event.params[1], function(u2, lfm2) {
if(u2 && lfm2) {
var u1 = event.rUser,
lfm1 = event.rProfile.lastfm,
u2 = event.res[0].user,
lfm2 = event.res[0].lfm;
this.api.tasteCompare(event.rProfile.lastfm, lfm2, function(err, comp) {
if(!err) {
var score = Math.floor(comp.score * 100);
@ -235,19 +224,39 @@ var lastfm = function(dbot) {
}
}
});
} else {
if(!u2) {
event.reply('No such user.');
} else {
event.reply(u2.currentNick + ': Set a lastfm username with "~set lastfm username"');
}
}
}.bind(this));
} else {
event.reply(event.user + ': Set a lastfm username with "~set lastfm username"');
}
}
};
this.commands['~taste'].regex = [/^~taste ([\d\w[\]{}^|\\`_-]+?)/, 2];
_.each(this.commands, function(command) {
command.resolver = function(event, callback) {
if(event.rProfile && _.has(event.rProfile, 'lastfm')) {
if(event.params[1]) {
this.internalAPI.getLastFM(event.server, event.params[1], function(user, lfm) {
if(user && lfm) {
event.res.push({
'user': user,
'lfm': lfm
});
callback(false);
} else {
if(!user) {
event.reply('Unknown user.');
} else {
event.reply(user.currentNick + ': Set a lastfm username with "~set lastfm username"');
}
callback(true);
}
});
} else {
callback(false);
}
} else {
event.reply(event.user + ': Set a lastfm username with "~set lastfm username"');
callback(true);
}
}.bind(this);
}, this);
};
exports.fetch = function(dbot) {