look at what others are listening to, fix output if user doesnt have lastfm proeprty set

This commit is contained in:
reality 2013-10-24 12:30:53 +00:00
parent 614520443f
commit 8e791ed581

View File

@ -9,10 +9,10 @@ var _ = require('underscore')._,
var lastfm = function(dbot) { var lastfm = function(dbot) {
this.ApiRoot = 'http://ws.audioscrobbler.com/2.0/'; this.ApiRoot = 'http://ws.audioscrobbler.com/2.0/';
this.commands = { this.api = {
'~listening': function(event) { 'getListening': function(user, callback) {
dbot.api.profile.getProfileByUUID(event.rUser.id, function(profile) { dbot.api.profile.getProfileByUUID(user.id, function(profile) {
if(profile && profile.profile,lastfm != null) { if(profile && profile.profile.lastfm != null) {
profile = profile.profile; profile = profile.profile;
request.get(this.ApiRoot, { request.get(this.ApiRoot, {
'qs': { 'qs': {
@ -26,20 +26,36 @@ var lastfm = function(dbot) {
'json': true 'json': true
}, function(err, res, body) { }, function(err, res, body) {
if(_.has(body, 'error') && body.error == 6) { if(_.has(body, 'error') && body.error == 6) {
event.reply('Unknown Last.FM user.'); callback('no_user', null);
} else if(_.has(body, 'recenttracks') && !_.isUndefined(body.recenttracks.track[0])) { } else if(_.has(body, 'recenttracks') && !_.isUndefined(body.recenttracks.track[0])) {
var track = body.recenttracks.track[0]; callback(null, body.recenttracks.track[0]);
term = track.name + ' ' + track.artist['#text'], } else {
callback('no_listen', null);
}
});
} else {
callback('no_profile', null);
}
}.bind(this));
}
};
this.commands = {
'~listening': function(event) {
var outputListening = function(err, track) {
if(!err) {
var term = track.name + ' ' + track.artist['#text'],
output = ''; output = '';
if(_.has(track, '@attr') && _.has(track['@attr'], 'nowplaying') && track['@attr'].nowplaying == 'true') { if(_.has(track, '@attr') && _.has(track['@attr'], 'nowplaying') && track['@attr'].nowplaying == 'true') {
output = dbot.t('now_listening', { output = dbot.t('now_listening', {
'user': event.user, 'user': user.currentNick,
'track': track.name, 'track': track.name,
'artist': track.artist['#text'] 'artist': track.artist['#text']
}); });
} else { } else {
output = dbot.t('last_listened', { output = dbot.t('last_listened', {
'user': event.user, 'user': user.currentNick,
'track': track.name, 'track': track.name,
'artist': track.artist['#text'] 'artist': track.artist['#text']
}); });
@ -55,13 +71,28 @@ var lastfm = function(dbot) {
event.reply(output); event.reply(output);
}); });
} else { } else {
event.reply(dbot.t('no_listen', { 'user': event.user })); if(err == 'no_user') {
} event.reply('Unknown Last.FM user.');
}); } else if(err == 'no_listen') {
} else { event.reply(dbot.t('no_listen', { 'user': user.currentNick }));
} else if(err == 'no_profile') {
event.reply('Set a lastfm username with "~set lastfm username"'); event.reply('Set a lastfm username with "~set lastfm username"');
} }
}
};
if(event.params[1]) {
dbot.api.users.resolveUser(event.server, event.params[1], function(user) {
if(user) {
this.api.getListening(user, outputListening);
} else {
event.reply('Unrecognised user.');
}
}.bind(this)); }.bind(this));
} else {
user = event.rUser;
this.api.getListening(event.rUser, outputListening);
}
} }
}; };
}; };