Fix toptracks subcommand

and add a few options for time
This commit is contained in:
SileNce5k 2024-09-10 21:59:44 +02:00
parent 5f6949bc2e
commit bdd4f73b68
Signed by: SileNce
GPG key ID: B0A142BB4291B204
2 changed files with 49 additions and 7 deletions

View file

@ -26,12 +26,11 @@ module.exports = {
case "toptracks": case "toptracks":
case "tt": case "tt":
args.shift(); args.shift();
//sendText.text = await getTopTracks(message.author.id, args); sendText = await getTopTracks(message.author.id, args, message.guild);
sendText.text = "This command is currently being rewritten, come back later."
break; break;
case "cover": case "cover":
sendText = await getCurrentCover(message.author.id, message.guild); sendText = await getCurrentCover(message.author.id, message.guild);
sendText.embed.get break;
default: default:
sendText.text = `${args[0]} is not a valid subcommand.\nSee \`${prefix}help fm\` for more info.`; sendText.text = `${args[0]} is not a valid subcommand.\nSee \`${prefix}help fm\` for more info.`;
break; break;

View file

@ -1,16 +1,41 @@
// 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
module.exports = async function (lastfmUsername, option) { const getFmUsername = require("./getFmUsername");
const Discord = require('discord.js');
const getNickname = require('../getNickname')
const parseMention = require('../parseMention')
module.exports = async function (userID, option, guild) {
let lastfmUsername = await getFmUsername(userID)
let parse = parseMention(userID, guild)
let user = guild.members.cache.get(parse);
let nickname = getNickname(user, guild)
if (nickname == null) {
nickname = user.user.username;
}
let tracks = []; let tracks = [];
const options = { const options = {
"alltime": "overall", "d": "1day",
"m": "1month",
"w": "7day",
"q": "3month",
"h": "6month",
"y": "12month",
"all": "overall",
"daily": "1day",
"weekly": "7day", "weekly": "7day",
"monthly": "1month", "monthly": "1month",
"quarterly": "3month", "quarterly": "3month",
"halfyear": "6month", "halfyear": "6month",
"yearly": "12month", "yearly": "12month",
"biweekly": "14day",
"fortnight": "14day",
"twomonth": "2month",
"twoyear": "24month",
"weekend": "2day",
undefined: "7day" undefined: "7day"
} };
if(option.length === 0){ if(option.length === 0){
option[0] = "weekly" option[0] = "weekly"
} }
@ -40,6 +65,7 @@ module.exports = async function (lastfmUsername, option) {
duration = "yearly"; duration = "yearly";
break; break;
} }
let sendText = { text: "", embed: null }
const apiKey = process.env.LAST_FM_API_KEY; const apiKey = process.env.LAST_FM_API_KEY;
if(lastfmUsername != undefined){ if(lastfmUsername != undefined){
tracks = await new Promise ((resolve, reject) => { tracks = await new Promise ((resolve, reject) => {
@ -62,5 +88,22 @@ module.exports = async function (lastfmUsername, option) {
}); });
}); });
} }
return tracks; const embed = new Discord.MessageEmbed()
.setAuthor(`Top ${duration} for ${nickname}`, user.user.avatarURL({ dynamic: true, size: 4096 }))
.setThumbnail(tracks[0].cover)
.setColor(15780145)
let tracksInfo = "";
for(let i = 0; i < tracks.length; i++){
let track = `${i}. **${tracks[i].artist}** - ${tracks[i].song} - *${tracks[i].playcount} plays*`
if(i < tracks.length - 1){
tracksInfo += `${track}\n`;
}else{
tracksInfo += `${track}`;
}
}
embed.addFields({
name: ` `, value: `${tracksInfo}`
},)
sendText.embed = embed;
return sendText;
} }