3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 14:29:29 +01:00

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) {
this.ApiRoot = 'http://ws.audioscrobbler.com/2.0/';
this.commands = {
'~listening': function(event) {
dbot.api.profile.getProfileByUUID(event.rUser.id, function(profile) {
if(profile && profile.profile,lastfm != null) {
this.api = {
'getListening': function(user, callback) {
dbot.api.profile.getProfileByUUID(user.id, function(profile) {
if(profile && profile.profile.lastfm != null) {
profile = profile.profile;
request.get(this.ApiRoot, {
'qs': {
@ -26,20 +26,36 @@ var lastfm = function(dbot) {
'json': true
}, function(err, res, body) {
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])) {
var track = body.recenttracks.track[0];
term = track.name + ' ' + track.artist['#text'],
callback(null, body.recenttracks.track[0]);
} 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 = '';
if(_.has(track, '@attr') && _.has(track['@attr'], 'nowplaying') && track['@attr'].nowplaying == 'true') {
output = dbot.t('now_listening', {
'user': event.user,
'user': user.currentNick,
'track': track.name,
'artist': track.artist['#text']
});
} else {
output = dbot.t('last_listened', {
'user': event.user,
'user': user.currentNick,
'track': track.name,
'artist': track.artist['#text']
});
@ -55,13 +71,28 @@ var lastfm = function(dbot) {
event.reply(output);
});
} else {
event.reply(dbot.t('no_listen', { 'user': event.user }));
}
});
} else {
if(err == 'no_user') {
event.reply('Unknown Last.FM user.');
} else if(err == 'no_listen') {
event.reply(dbot.t('no_listen', { 'user': user.currentNick }));
} else if(err == 'no_profile') {
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));
} else {
user = event.rUser;
this.api.getListening(event.rUser, outputListening);
}
}
};
};