twitch_bot/commands/fm.js

44 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-02-01 12:04:14 +01:00
const command = {
name: "fm",
desc: "get last played track from last.fm with your username",
2023-02-02 23:37:42 +01:00
restricted: false,
2023-02-02 23:58:58 +01:00
mod: false,
2023-02-01 12:04:14 +01:00
run: async (client, msg, splitted) => {
if (splitted[2] === undefined) {
2023-02-04 17:21:55 +01:00
await client.say(
2023-02-01 12:04:14 +01:00
msg.channelName,
`${msg.displayName}, supply your Last.FM username PoroSad`
);
return;
2023-02-01 12:04:14 +01:00
}
const data = await fetch(
2023-02-06 15:02:34 +01:00
`https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${encodeURIComponent(
splitted[2]
)}&api_key=${process.env.LASTFM_KEY}&format=json&limit=1`
2023-02-01 12:04:14 +01:00
)
.then((res) => res.json())
.then((data) => {
return data.recenttracks.track[0];
})
2023-02-11 13:48:05 +01:00
.catch((err) =>
console.error("Error when fetching for last.fm: " + err.message)
);
2023-02-01 12:04:14 +01:00
if (data === undefined) {
2023-02-11 13:48:05 +01:00
await client.say(
msg.channelName,
`${msg.displayName}, Invalid user PoroSad`
);
return;
2023-02-01 12:04:14 +01:00
}
2023-02-11 13:48:05 +01:00
await client.say(
msg.channelName,
`${msg.displayName}, (Now Playing) Album: ${data.album["#text"]} Track: ${data.name} Artist: ${data.artist["#text"]} | ${data.url}`
);
2023-02-01 12:04:14 +01:00
},
};
2023-02-01 22:11:48 +01:00
module.exports = { command };