FM: Add a toptracks feature
This commit is contained in:
parent
5b202ae9d3
commit
51e810264c
2 changed files with 42 additions and 1 deletions
|
@ -1,6 +1,6 @@
|
|||
const fmlogin = require("../../util/lastfm/fmlogin");
|
||||
const getCurrentScrobble = require("../../util/lastfm/getCurrentScrobble");
|
||||
|
||||
const getTopTracks = require("../../util/lastfm/getTopTracks");
|
||||
module.exports = {
|
||||
name: 'fm',
|
||||
description: 'Last fm commands. See `<prefix>help fm` for more info.',
|
||||
|
@ -14,6 +14,9 @@ module.exports = {
|
|||
case "set":
|
||||
sendText = await fmlogin(message.author.id, args[1]);
|
||||
break;
|
||||
case "toptracks":
|
||||
case "tt":
|
||||
sendText = await getTopTracks(message.author.id, args);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
|
38
util/lastfm/getTopTracks.js
Normal file
38
util/lastfm/getTopTracks.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
// http://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=username&api_key=YOUR_API_KEY&format=json
|
||||
|
||||
const getFmUsername = require("./getFmUsername")
|
||||
|
||||
module.exports = async function (userID, options) {
|
||||
let lastfmUsername = await getFmUsername(userID);
|
||||
let sendText = "";
|
||||
let tracks = [];
|
||||
const apiKey = process.env.LAST_FM_API_KEY;
|
||||
if(lastfmUsername != undefined){
|
||||
tracks = await new Promise ((resolve, reject) => {
|
||||
fetch(`https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=${lastfmUsername}&api_key=${apiKey}&format=json`)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
for(let i = 0; i < 10; i++){
|
||||
let track = {}
|
||||
let currentTrack = data.toptracks.track[i];
|
||||
track.artist = currentTrack.artist.name;
|
||||
track.song = currentTrack.name;
|
||||
track.playcount = currentTrack.playcount;
|
||||
tracks.push(track);
|
||||
}
|
||||
resolve(tracks);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
sendText = `Your top 10 tracks are:\n`;
|
||||
for(let i = 0; i < tracks.length; i++){
|
||||
sendText += `${i}. ${tracks[i].artist} - ${tracks[i].song} (${tracks[i].playcount} plays)\n`;
|
||||
}
|
||||
} else {
|
||||
sendText = "You haven't set your last.fm username yet. Use `fm set <lastfm_username>` to set it.";
|
||||
}
|
||||
return sendText;
|
||||
}
|
Loading…
Add table
Reference in a new issue