diff --git a/commands/lastfm.js b/commands/lastfm.js new file mode 100644 index 0000000..b51e166 --- /dev/null +++ b/commands/lastfm.js @@ -0,0 +1,80 @@ +const axios = require('axios'); +const Config = require('../props/config.json'); +const { getStrings } = require('../plugins/checklang.js'); +const { isOnSpamWatch } = require('../plugins/lib-spamwatch/spamwatch.js'); +const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(isOnSpamWatch); + +module.exports = (bot) => { + bot.command('lt', spamwatchMiddleware, async (ctx) => { + const Strings = getStrings(ctx.from.language_code); + const userInput = ctx.message.text.split(" "); + const lastfmUser = userInput[1]; + + if (!lastfmUser) { + return ctx.reply(Strings.lastFmNoUser, { + parse_mode: "Markdown", + reply_to_message_id: ctx.message.message_id + }); + }; + + try { + const response = await axios.get('http://ws.audioscrobbler.com/2.0/', { + params: { + method: 'user.getRecentTracks', + user: lastfmUser, + api_key: Config.lastKey, + format: 'json', + limit: 1 + } + }); + + const track = response.data.recenttracks.track[0]; + + if (!track) { + const noResponse = Strings.lastFmNoResponse.replace('{lastfmUser}', lastfmUser); + return ctx.reply(noResponse, { + parse_mode: "Markdown", + reply_to_message_id: ctx.message.message_id + }); + }; + + const trackName = track.name; + const artistName = track.artist['#text']; + const nowPlaying = track['@attr'] && track['@attr'].nowplaying ? Strings.lastFmListeningNow : Strings.lastFmLastPlayed; + + const imageUrl = track.image.find(img => img.size === 'extralarge')['#text'] || track.image.find(img => img.size === 'mega')['#text']; + const trackUrl = `https://www.last.fm/music/${encodeURIComponent(artistName)}/_/${encodeURIComponent(trackName)}`; + const artistUrl = `https://www.last.fm/music/${encodeURIComponent(artistName)}`; + const userUrl = `https://www.last.fm/user/${encodeURIComponent(lastfmUser)}`; + + const message = Strings.lastFmStatusFor + .replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`) + .replace("{nowPlaying}", nowPlaying) + .replace("{trackName}", `[${trackName}](${trackUrl})`) + .replace("{artistName}", `[${artistName}](${artistUrl})`); + + if (imageUrl) { + ctx.replyWithPhoto(imageUrl, { + caption: message, + parse_mode: "Markdown", + reply_to_message_id: ctx.message.message_id + }); + } else { + ctx.reply(message, { + parse_mode: "Markdown", + reply_to_message_id: ctx.message.message_id + }); + }; + + } catch (err) { + const userUrl = `https://www.last.fm/user/${encodeURIComponent(lastfmUser)}`; + const message = Strings.lastFmErr + .replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`) + .replace("{err}", err); + ctx.reply(message, { + parse_mode: "Markdown", + reply_to_message_id: ctx.message.message_id + }); + }; + }); +}; diff --git a/locales/english.json b/locales/english.json index 9cae45d..335329b 100644 --- a/locales/english.json +++ b/locales/english.json @@ -28,5 +28,11 @@ "userInfo": "*User info*\n\n*Name:* `{userName}`\n*Username:* `{userHandle}`\n*User ID:* `{userId}`\n*Language:* `{userLang}`\n*Premium user:* `{userPremium}`", "chatInfo": "*Chat info*\n\n*Name:* `{chatName}`\n*Chat ID:* `{chatId}`\n*Handle:* `{chatHandle}`\n*Type:* `{chatType}`\n*Members:* `{chatMembersCount}`\n*Is a forum:* `{isForum}`", "funEmojiResult": "*You rolled {emoji} and got *`{value}`*!*\nYou don't know what that means? Me too!", - "gifErr": "*Something went wrong while sending the GIF. Please try again later.*\n\n{err}" + "gifErr": "*Something went wrong while sending the GIF. Please try again later.*\n\n{err}", + "lastFmNoUser": "*Please provide a Last.fm username.*\nExample: `/lt username`", + "lastFmNoRecent": "*No recent tracks found for Last.fm user* `{lastfmUser}`*.*", + "lastFmListeningNow": "Listening now", + "lastFmLastPlayed": "Last played", + "lastFmStatusFor": "*Last.fm status for user* {lastfmUser}*:*\n\n*{nowPlaying}*: {trackName} by {artistName}", + "lastFmErr": "*Error retrieving data for Last.fm user* {lastfmUser}." } \ No newline at end of file diff --git a/locales/portuguese.json b/locales/portuguese.json index b776772..450b555 100644 --- a/locales/portuguese.json +++ b/locales/portuguese.json @@ -28,5 +28,11 @@ "userInfo": "*Informações do usuário*\n\n*Nome:* `{userName}`\n*Usuário:* `{userHandle}`\n*ID:* `{userId}`\n*Idioma:* `{userLang}`\n*Usuário Premium:* `{userPremium}`", "chatInfo": "*Informações do chat*\n\n*Nome:* `{chatName}`\n*ID do chat:* `{chatId}`\n*Identificador:* `{chatHandle}`\n*Tipo:* `{chatType}`\n*Membros:* `{chatMembersCount}`\n*É um fórum:* `{isForum}`", "funEmojiResult": "*Você lançou {emoji} e obteve *`{value}`*!*\nVocê não sabe o que isso significa? Nem eu!", - "gifErr": "*Algo deu errado ao enviar o GIF. Tente novamente mais tarde.*\n\n{err}" + "gifErr": "*Algo deu errado ao enviar o GIF. Tente novamente mais tarde.*\n\n{err}", + "lastFmNoUser": "*Por favor, forneça um nome de usuário do Last.fm.*\nExemplo: `/lt username`", + "lastFmNoRecent": "*Nenhas faixas recentes encontradas para o usuário do Last.fm* `{lastfmUser}`*.*", + "lastFmListeningNow": "Ouvindo agora", + "lastFmLastPlayed": "Última reprodução", + "lastFmStatusFor": "*Status do Last.fm para o usuário* {lastfmUser}*:*\n\n*{nowPlaying}*: {trackName} por {artistName}", + "lastFmErr": "*Erro ao recuperar dados para o usuário do Last.fm* {lastfmUser}." } \ No newline at end of file