fm: Add the possibility to specify period

This commit is contained in:
SileNce5k 2024-03-07 12:35:58 +01:00
parent e5862a323b
commit 3c7d0a6c28
No known key found for this signature in database
GPG key ID: 961132EB78C8915F
2 changed files with 20 additions and 2 deletions

View file

@ -21,7 +21,9 @@ module.exports = {
break;
case "toptracks":
case "tt":
args.shift();
sendText = await getTopTracks(message.author.id, args);
break;
default:
break;
}

View file

@ -2,14 +2,30 @@
const getFmUsername = require("./getFmUsername")
module.exports = async function (userID, options) {
module.exports = async function (userID, option) {
let lastfmUsername = await getFmUsername(userID);
let sendText = "";
let tracks = [];
const options = {
"alltime": "overall",
"weekly": "7day",
"monthly": "1month",
"quarterly": "3month",
"halfyear": "6month",
"yearly": "12month",
undefined: "7day"
}
if(option.length === 0){
option[0] = "weekly"
}else {
option[0] = options[option[0]];
if(option[0] === undefined)
option[0] = options[option[0]];
}
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`)
fetch(`https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=${lastfmUsername}&period${option[0]}&api_key=${apiKey}&format=json`)
.then(response => response.json())
.then(data => {
for(let i = 0; i < 10; i++){