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) { } else if(!isIgnoring && _.has(dbot.commands, commandName) && !dbot.commands[commandName].disabled) {
if(this.api.applyRegex(commandName, event)) { if(this.api.applyRegex(commandName, event)) {
try { try {
var command = dbot.commands[commandName]; var command = dbot.commands[commandName],
var results = command.apply(dbot.modules[command.module], [event]); 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) { } catch(err) {
if(dbot.config.debugMode == true) { if(dbot.config.debugMode == true) {
var stack = err.stack.split('\n').slice(1, dbot.config.debugLevel + 1); var stack = err.stack.split('\n').slice(1, dbot.config.debugLevel + 1);

View File

@ -116,138 +116,147 @@ var lastfm = function(dbot) {
this.commands = { this.commands = {
'~suggestion': function(event) { '~suggestion': function(event) {
if(event.rProfile && _.has(event.rProfile, 'lastfm')) { this.api.getListening(event.rProfile.lastfm, function(err, track) {
this.api.getListening(event.rProfile.lastfm, function(err, track) { if(!err) {
if(!err) { this.api.getSimilarArtists(track.artist.mbid, function(err, similar) {
this.api.getSimilarArtists(track.artist.mbid, function(err, similar) { if(!err) {
if(!err) { var choice = _.random(0, similar.length - 1);
var choice = _.random(0, similar.length - 1); this.api.getRandomArtistTrack(similar[choice].mbid, function(err, track) {
this.api.getRandomArtistTrack(similar[choice].mbid, function(err, track) { if(!err) {
if(!err) { var output = dbot.t('lfm_suggestion', {
var output = dbot.t('lfm_suggestion', { 'user': event.user,
'user': event.user, 'name': track.name,
'name': track.name, 'artist': track.artist.name
'artist': track.artist.name });
}); var term = track.name + ' ' + track.artist.name;
var term = track.name + ' ' + track.artist.name; dbot.api.youtube.search(term, function(body) {
dbot.api.youtube.search(term, function(body) { if(_.isObject(body) && _.has(body, 'feed') && _.has(body.feed, 'entry')) {
if(_.isObject(body) && _.has(body, 'feed') && _.has(body.feed, 'entry')) { var v = body.feed.entry[0];
var v = body.feed.entry[0]; link = v.link[0].href.match(dbot.modules.youtube.LinkRegex);
link = v.link[0].href.match(dbot.modules.youtube.LinkRegex); if(link) {
if(link) { output += ' - http://youtu.be/' + link[2];
output += ' - http://youtu.be/' + link[2];
}
} }
event.reply(output); }
}); event.reply(output);
} else { });
event.reply('something broke'); } else {
} event.reply('something broke');
}); }
} else { });
event.reply('something broke'); } else {
} event.reply('something broke');
}.bind(this));
} else {
if(err == 'no_user') {
event.reply('Unknown Last.FM user.');
} else if(err == 'no_listen') {
event.reply(dbot.t('no_listen', { 'user': event.user }));
} }
}.bind(this));
} else {
if(err == 'no_user') {
event.reply('Unknown Last.FM user.');
} else if(err == 'no_listen') {
event.reply(dbot.t('no_listen', { 'user': event.user }));
} }
}.bind(this)); }
} else { }.bind(this));
event.reply(event.user + ': Set a lastfm username with "~set lastfm username"');
}
}, },
'~listening': function(event) { '~listening': function(event) {
var getListening = function(user, lfm) { var user = event.rUser,
if(user && lfm) { lfm = event.rProfile.lastfm;
this.api.getListening(lfm, function(err, track) { if(event.res[0]) {
if(!err) { user = event.res[0].user;
var term = track.name + ' ' + track.artist['#text'], lfm = event.res[0].lfm;
output = ''; }
if(_.has(track, '@attr') && _.has(track['@attr'], 'nowplaying') && track['@attr'].nowplaying == 'true') {
output = dbot.t('now_listening', { this.api.getListening(lfm, function(err, track) {
'user': user.currentNick, if(!err) {
'track': track.name, var term = track.name + ' ' + track.artist['#text'],
'artist': track.artist['#text'] output = '';
}); if(_.has(track, '@attr') && _.has(track['@attr'], 'nowplaying') && track['@attr'].nowplaying == 'true') {
} else { output = dbot.t('now_listening', {
output = dbot.t('last_listened', { 'user': user.currentNick,
'user': user.currentNick, 'track': track.name,
'track': track.name, 'artist': track.artist['#text']
'artist': track.artist['#text'] });
}); } else {
} output = dbot.t('last_listened', {
dbot.api.youtube.search(term, function(body) { 'user': user.currentNick,
if(_.isObject(body) && _.has(body, 'feed') && _.has(body.feed, 'entry')) { 'track': track.name,
var v = body.feed.entry[0]; 'artist': track.artist['#text']
link = v.link[0].href.match(dbot.modules.youtube.LinkRegex); });
if(link) { }
output += ' - http://youtu.be/' + link[2]; dbot.api.youtube.search(term, function(body) {
} if(_.isObject(body) && _.has(body, 'feed') && _.has(body.feed, 'entry')) {
} var v = body.feed.entry[0];
event.reply(output); link = v.link[0].href.match(dbot.modules.youtube.LinkRegex);
}); if(link) {
} else { output += ' - http://youtu.be/' + link[2];
if(err == 'no_user') {
event.reply('Unknown LastFM user.');
} else if(err == 'no_listen') {
event.reply(dbot.t('no_listen', { 'user': user.currentNick }));
} }
} }
event.reply(output);
}); });
} else { } else {
if(!user) { if(err == 'no_user') {
event.reply('Unknown user.'); event.reply('Unknown LastFM user.');
} else { } else if(err == 'no_listen') {
event.reply(user.currentNick + ': Set a lastfm username with "~set lastfm username"'); event.reply(dbot.t('no_listen', { 'user': user.currentNick }));
} }
} }
}.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) { '~taste': function(event) {
if(event.rProfile && _.has(event.rProfile, 'lastfm')) { var u1 = event.rUser,
this.internalAPI.getLastFM(event.server, event.params[1], function(u2, lfm2) { lfm1 = event.rProfile.lastfm,
if(u2 && lfm2) { u2 = event.res[0].user,
this.api.tasteCompare(event.rProfile.lastfm, lfm2, function(err, comp) { lfm2 = event.res[0].lfm;
if(!err) {
var score = Math.floor(comp.score * 100); this.api.tasteCompare(event.rProfile.lastfm, lfm2, function(err, comp) {
event.reply(dbot.t('taste_compat', { if(!err) {
'user1': event.user, var score = Math.floor(comp.score * 100);
'user2': u2.currentNick, event.reply(dbot.t('taste_compat', {
'score': score 'user1': event.user,
})); 'user2': u2.currentNick,
} else { 'score': score
if(err == 'no_user') { }));
event.reply('Unknown Last.FM user.'); } else {
} else { if(err == 'no_user') {
event.reply('Well something went wrong and I don\'t know what it means'); event.reply('Unknown Last.FM user.');
}
}
});
} else { } else {
if(!u2) { event.reply('Well something went wrong and I don\'t know what it means');
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) { exports.fetch = function(dbot) {