diff --git a/util/lastfm/getCurrentScrobble.js b/util/lastfm/getCurrentScrobble.js index 349b7fa..c118cf5 100644 --- a/util/lastfm/getCurrentScrobble.js +++ b/util/lastfm/getCurrentScrobble.js @@ -4,74 +4,73 @@ const getFmUsername = require("./getFmUsername"); const Discord = require('discord.js'); require("dotenv").config(); -module.exports = async function(userID, guild) { +module.exports = async function (userID, guild) { let parse = parseMention(userID, guild) let user = guild.members.cache.get(parse); let nickname = getNickname(user, guild) - if(nickname == null){ + if (nickname == null) { nickname = user.user.username; } let isCurrentScrobble = "Current"; - let sendText = {text: "", embed: null} + let sendText = { text: "", embed: null } 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 tracks = []; - let track; - try { - for(let i = 0; i < 2; i++){ - track = data.recenttracks.track[i]; - scrobble.artist = track.artist["#text"]; - scrobble.song = track.name; - console.log(`Track ${i}: ${scrobble.song}`); - scrobble.album = track.album["#text"]; - scrobble.cover = track.image[3]["#text"]; - if(i === 0){ - if(track['@attr'] != undefined || track['@attr'].nowplaying !== "true"){ - isCurrentScrobble = "Last"; + 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 tracks = []; + let track; + try { + for (let i = 0; i < 2; i++) { + track = data.recenttracks.track[i]; + scrobble.artist = track.artist["#text"]; + scrobble.song = track.name; + scrobble.album = track.album["#text"]; + scrobble.cover = track.image[3]["#text"]; + if (i === 0) { + if (!track["@attr"]) { + isCurrentScrobble = "Last"; + } + } + tracks.push(scrobble); } + resolve(tracks); + } catch (error) { + scrobble.error = true; + if (data.error === 6) { + scrobble.errorMsg = "User not found. Use `fm set ` to set your last.fm username."; + resolve(scrobble); + } + scrobble.errorMsg = "Last.fm is probably having problems. Try again later."; + resolve(scrobble); } - tracks.push(scrobble); - } - resolve(tracks); - } catch (error) { - scrobble.error = true; - if(data.error === 6){ - scrobble.errorMsg = "User not found. Use `fm set ` to set your last.fm username."; - resolve(scrobble); - } - scrobble.errorMsg = "Last.fm is probably having problems. Try again later."; - resolve(scrobble); - } - }) - .catch(error => { - console.error(error); - reject(error); + }) + .catch(error => { + console.error(error); + reject(error); + }); }); - }); - if(scrobble.error != null){ - sendText.text = scrobble.errorMsg; - return sendText; - } - const embed = new Discord.MessageEmbed() - .setAuthor(`Now playing - ${nickname}`, user.user.avatarURL({ dynamic: true, size: 4096 })) - .setThumbnail(scrobble[0].cover) - .setColor(15780145) - .addFields({ - name: `${isCurrentScrobble}:`, value: `${scrobble[0].song}\n **${scrobble[0].artist} • ** ${scrobble[0].album}` - },) - if(isCurrentScrobble === "Current"){ - embed.addFields({ - name: "Previous:", value: `${scrobble[1].song}\n **${scrobble[1].artist} • ** ${scrobble[1].album}` - },) - } - sendText.embed = embed; + if (scrobble.error != null) { + sendText.text = scrobble.errorMsg; + return sendText; + } + const embed = new Discord.MessageEmbed() + .setAuthor(`Now playing - ${nickname}`, user.user.avatarURL({ dynamic: true, size: 4096 })) + .setThumbnail(scrobble[0].cover) + .setColor(15780145) + .addFields({ + name: `${isCurrentScrobble}:`, value: `${scrobble[0].song}\n **${scrobble[0].artist} • ** ${scrobble[0].album}` + },) + if (isCurrentScrobble === "Current") { + embed.addFields({ + name: "Previous:", value: `${scrobble[1].song}\n **${scrobble[1].artist} • ** ${scrobble[1].album}` + },) + } + sendText.embed = embed; } else { sendText.text = "You haven't set your last.fm username yet. Use `fm set ` to set it."; } diff --git a/util/lastfm/getTopTracks.js b/util/lastfm/getTopTracks.js index 1d1b1d9..38da5c1 100644 --- a/util/lastfm/getTopTracks.js +++ b/util/lastfm/getTopTracks.js @@ -1,10 +1,10 @@ // http://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=username&api_key=YOUR_API_KEY&format=json - +const Discord = require('discord.js'); const getFmUsername = require("./getFmUsername") module.exports = async function (userID, option) { let lastfmUsername = await getFmUsername(userID); - let sendText = ""; + let sendText = {text: "", embed: null}; let tracks = []; const options = { "alltime": "overall", @@ -65,12 +65,17 @@ module.exports = async function (userID, option) { reject(error); }); }); - sendText = `Top ${duration} tracks for ${lastfmUsername}:\n`; + let author = `Top ${duration} tracks for ${lastfmUsername}:\n`; + let toptracks = ""; for(let i = 0; i < tracks.length; i++){ sendText += `${i}. ${tracks[i].artist} - ${tracks[i].song} (${tracks[i].playcount} plays)\n`; } + const embed = new Discord.MessageEmbed() + .setAuthor(`Now playing - ${nickname}`, user.user.avatarURL({ dynamic: true, size: 4096 })) + .setColor(15780145) + } else { - sendText = "You haven't set your last.fm username yet. Use `fm set ` to set it."; + sendText.text = "You haven't set your last.fm username yet. Use `fm set ` to set it."; } return sendText; } \ No newline at end of file