Temp commit
This commit is contained in:
parent
22d688ee80
commit
357f93623a
2 changed files with 64 additions and 60 deletions
|
@ -4,74 +4,73 @@ const getFmUsername = require("./getFmUsername");
|
||||||
const Discord = require('discord.js');
|
const Discord = require('discord.js');
|
||||||
|
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
module.exports = async function(userID, guild) {
|
module.exports = async function (userID, guild) {
|
||||||
let parse = parseMention(userID, guild)
|
let parse = parseMention(userID, guild)
|
||||||
let user = guild.members.cache.get(parse);
|
let user = guild.members.cache.get(parse);
|
||||||
let nickname = getNickname(user, guild)
|
let nickname = getNickname(user, guild)
|
||||||
if(nickname == null){
|
if (nickname == null) {
|
||||||
nickname = user.user.username;
|
nickname = user.user.username;
|
||||||
}
|
}
|
||||||
let isCurrentScrobble = "Current";
|
let isCurrentScrobble = "Current";
|
||||||
let sendText = {text: "", embed: null}
|
let sendText = { text: "", embed: null }
|
||||||
let scrobble = {};
|
let scrobble = {};
|
||||||
const apiKey = process.env.LAST_FM_API_KEY;
|
const apiKey = process.env.LAST_FM_API_KEY;
|
||||||
let lastfmUsername = await getFmUsername(userID);
|
let lastfmUsername = await getFmUsername(userID);
|
||||||
if(lastfmUsername != undefined){
|
if (lastfmUsername != undefined) {
|
||||||
scrobble = await new Promise ((resolve, reject) => {
|
scrobble = await new Promise((resolve, reject) => {
|
||||||
fetch(`https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${lastfmUsername}&api_key=${apiKey}&format=json`)
|
fetch(`https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${lastfmUsername}&api_key=${apiKey}&format=json`)
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
let scrobble = {};
|
let scrobble = {};
|
||||||
let tracks = [];
|
let tracks = [];
|
||||||
let track;
|
let track;
|
||||||
try {
|
try {
|
||||||
for(let i = 0; i < 2; i++){
|
for (let i = 0; i < 2; i++) {
|
||||||
track = data.recenttracks.track[i];
|
track = data.recenttracks.track[i];
|
||||||
scrobble.artist = track.artist["#text"];
|
scrobble.artist = track.artist["#text"];
|
||||||
scrobble.song = track.name;
|
scrobble.song = track.name;
|
||||||
console.log(`Track ${i}: ${scrobble.song}`);
|
scrobble.album = track.album["#text"];
|
||||||
scrobble.album = track.album["#text"];
|
scrobble.cover = track.image[3]["#text"];
|
||||||
scrobble.cover = track.image[3]["#text"];
|
if (i === 0) {
|
||||||
if(i === 0){
|
if (!track["@attr"]) {
|
||||||
if(track['@attr'] != undefined || track['@attr'].nowplaying !== "true"){
|
isCurrentScrobble = "Last";
|
||||||
isCurrentScrobble = "Last";
|
}
|
||||||
|
}
|
||||||
|
tracks.push(scrobble);
|
||||||
}
|
}
|
||||||
|
resolve(tracks);
|
||||||
|
} catch (error) {
|
||||||
|
scrobble.error = true;
|
||||||
|
if (data.error === 6) {
|
||||||
|
scrobble.errorMsg = "User not found. Use `<prefix>fm set <lastfm_username>` to set your last.fm username.";
|
||||||
|
resolve(scrobble);
|
||||||
|
}
|
||||||
|
scrobble.errorMsg = "Last.fm is probably having problems. Try again later.";
|
||||||
|
resolve(scrobble);
|
||||||
}
|
}
|
||||||
tracks.push(scrobble);
|
})
|
||||||
}
|
.catch(error => {
|
||||||
resolve(tracks);
|
console.error(error);
|
||||||
} catch (error) {
|
reject(error);
|
||||||
scrobble.error = true;
|
});
|
||||||
if(data.error === 6){
|
|
||||||
scrobble.errorMsg = "User not found. Use `<prefix>fm set <lastfm_username>` 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);
|
|
||||||
});
|
});
|
||||||
});
|
if (scrobble.error != null) {
|
||||||
if(scrobble.error != null){
|
sendText.text = scrobble.errorMsg;
|
||||||
sendText.text = scrobble.errorMsg;
|
return sendText;
|
||||||
return sendText;
|
}
|
||||||
}
|
const embed = new Discord.MessageEmbed()
|
||||||
const embed = new Discord.MessageEmbed()
|
.setAuthor(`Now playing - ${nickname}`, user.user.avatarURL({ dynamic: true, size: 4096 }))
|
||||||
.setAuthor(`Now playing - ${nickname}`, user.user.avatarURL({ dynamic: true, size: 4096 }))
|
.setThumbnail(scrobble[0].cover)
|
||||||
.setThumbnail(scrobble[0].cover)
|
.setColor(15780145)
|
||||||
.setColor(15780145)
|
.addFields({
|
||||||
.addFields({
|
name: `${isCurrentScrobble}:`, value: `${scrobble[0].song}\n **${scrobble[0].artist} • ** ${scrobble[0].album}`
|
||||||
name: `${isCurrentScrobble}:`, value: `${scrobble[0].song}\n **${scrobble[0].artist} • ** ${scrobble[0].album}`
|
},)
|
||||||
},)
|
if (isCurrentScrobble === "Current") {
|
||||||
if(isCurrentScrobble === "Current"){
|
embed.addFields({
|
||||||
embed.addFields({
|
name: "Previous:", value: `${scrobble[1].song}\n **${scrobble[1].artist} • ** ${scrobble[1].album}`
|
||||||
name: "Previous:", value: `${scrobble[1].song}\n **${scrobble[1].artist} • ** ${scrobble[1].album}`
|
},)
|
||||||
},)
|
}
|
||||||
}
|
sendText.embed = embed;
|
||||||
sendText.embed = embed;
|
|
||||||
} else {
|
} else {
|
||||||
sendText.text = "You haven't set your last.fm username yet. Use `<prefix>fm set <lastfm_username>` to set it.";
|
sendText.text = "You haven't set your last.fm username yet. Use `<prefix>fm set <lastfm_username>` to set it.";
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
// http://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=username&api_key=YOUR_API_KEY&format=json
|
// 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")
|
const getFmUsername = require("./getFmUsername")
|
||||||
|
|
||||||
module.exports = async function (userID, option) {
|
module.exports = async function (userID, option) {
|
||||||
let lastfmUsername = await getFmUsername(userID);
|
let lastfmUsername = await getFmUsername(userID);
|
||||||
let sendText = "";
|
let sendText = {text: "", embed: null};
|
||||||
let tracks = [];
|
let tracks = [];
|
||||||
const options = {
|
const options = {
|
||||||
"alltime": "overall",
|
"alltime": "overall",
|
||||||
|
@ -65,12 +65,17 @@ module.exports = async function (userID, option) {
|
||||||
reject(error);
|
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++){
|
for(let i = 0; i < tracks.length; i++){
|
||||||
sendText += `${i}. ${tracks[i].artist} - ${tracks[i].song} (${tracks[i].playcount} plays)\n`;
|
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 {
|
} else {
|
||||||
sendText = "You haven't set your last.fm username yet. Use `fm set <lastfm_username>` to set it.";
|
sendText.text = "You haven't set your last.fm username yet. Use `fm set <lastfm_username>` to set it.";
|
||||||
}
|
}
|
||||||
return sendText;
|
return sendText;
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue