discord_bot/util/lastfm/getCurrentScrobble.js
SileNce5k 53358531e5
Split getFmUsername into own function
This also fixes the bug that happens when a user tries the default fm
command and it crashes the bot.
2024-03-07 10:48:11 +01:00

31 lines
No EOL
1.2 KiB
JavaScript

const getFmUsername = require("./getFmUsername");
require("dotenv").config();
module.exports = async function(userID) {
let sendText = "";
let scrobble = {};
const apiKey = process.env.LAST_FM_API_KEY;
let lastfmUsername = await getFmUsername(userID);
if(lastfmUsername != undefined){
scrobble = await new Promise ((resolve, reject) => {
fetch(`https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${lastfmUsername}&api_key=${apiKey}&format=json`)
.then(response => response.json())
.then(data => {
let scrobble = {};
let track = data.recenttracks.track[0];
scrobble.artist = track.artist["#text"];
scrobble.song = track.name;
scrobble.album = track.album["#text"];
resolve(scrobble);
})
.catch(error => {
console.error(error);
reject(error);
});
});
sendText = `Currently scrobbling:\n${scrobble.artist} - ${scrobble.song}\nAlbum: ${scrobble.album}`;
} else {
sendText = "You haven't set your last.fm username yet. Use `fm set <lastfm_username>` to set it.";
}
return sendText;
}