Database implemented for the Last.fm command instead of specifying the user + help
This commit is contained in:
parent
412ae681e2
commit
da37e6b634
6 changed files with 85 additions and 15 deletions
|
@ -9,7 +9,8 @@ async function sendHelpMessage(ctx, isEditing) {
|
|||
reply_markup: {
|
||||
inline_keyboard: [
|
||||
[{ text: Strings.mainCommands, callback_data: '1' }, { text: Strings.usefulCommands, callback_data: '2' }],
|
||||
[{ text: Strings.interactiveEmojis, callback_data: '3' }, { text: Strings.funnyCommands, callback_data: '4' }]
|
||||
[{ text: Strings.interactiveEmojis, callback_data: '3' }, { text: Strings.funnyCommands, callback_data: '4' }],
|
||||
[{ text: Strings.lastFm, callback_data: '5' }]
|
||||
]
|
||||
}
|
||||
};
|
||||
|
@ -33,7 +34,7 @@ module.exports = (bot) => {
|
|||
parse_mode: 'Markdown',
|
||||
reply_markup: JSON.stringify({
|
||||
inline_keyboard: [
|
||||
[{ text: Strings.goBack, callback_data: '5' }],
|
||||
[{ text: Strings.goBack, callback_data: '6' }],
|
||||
]
|
||||
})
|
||||
};
|
||||
|
@ -56,6 +57,10 @@ module.exports = (bot) => {
|
|||
await ctx.editMessageText(Strings.funnyCommandsDesc, options);
|
||||
break;
|
||||
case '5':
|
||||
await ctx.answerCbQuery();
|
||||
await ctx.editMessageText(Strings.lastFmDesc, options);
|
||||
break;
|
||||
case '6':
|
||||
await ctx.answerCbQuery();
|
||||
await sendHelpMessage(ctx, true);
|
||||
break;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
const fs = require('fs');
|
||||
const axios = require('axios');
|
||||
const Config = require('../props/config.json');
|
||||
const { getStrings } = require('../plugins/checklang.js');
|
||||
|
@ -7,14 +8,66 @@ const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(is
|
|||
const scrobbler_url = 'http://ws.audioscrobbler.com/2.0/';
|
||||
const api_key = Config.lastKey;
|
||||
|
||||
const dbFile = 'props/lastfm.json';
|
||||
let users = {};
|
||||
|
||||
function loadUsers() {
|
||||
if (!fs.existsSync(dbFile)) {
|
||||
console.log(`WARN: Last.fm user database ${dbFile} not found. Creating a new one.`);
|
||||
saveUsers();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const data = fs.readFileSync(dbFile, 'utf-8');
|
||||
users = JSON.parse(data);
|
||||
} catch (err) {
|
||||
console.log("WARN: Error loading the Last.fm user database:", err);
|
||||
users = {};
|
||||
}
|
||||
}
|
||||
|
||||
function saveUsers() {
|
||||
try {
|
||||
fs.writeFileSync(dbFile, JSON.stringify(users, null, 2), 'utf-8');
|
||||
} catch (err) {
|
||||
console.error("WARN: Error saving Last.fm users:", err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (bot) => {
|
||||
bot.command(['lt', 'lmu', 'last', 'lfm'], spamwatchMiddleware, async (ctx) => {
|
||||
loadUsers();
|
||||
|
||||
bot.command('setuser', (ctx) => {
|
||||
const userId = ctx.from.id;
|
||||
const Strings = getStrings(ctx.from.language_code);
|
||||
const userInput = ctx.message.text.split(" ");
|
||||
const lastfmUser = userInput[1];
|
||||
const lastUser = ctx.message.text.split(' ')[1];
|
||||
|
||||
if (!lastUser) {
|
||||
return ctx.reply(Strings.lastFmNoUser, {
|
||||
parse_mode: "Markdown",
|
||||
reply_to_message_id: ctx.message.message_id
|
||||
});
|
||||
};
|
||||
|
||||
users[userId] = lastUser;
|
||||
saveUsers();
|
||||
|
||||
const message = Strings.lastFmUserSet.replace('{lastUser}', lastUser);
|
||||
|
||||
ctx.reply(message, {
|
||||
parse_mode: "Markdown",
|
||||
reply_to_message_id: ctx.message.message_id
|
||||
});
|
||||
});
|
||||
|
||||
bot.command(['lt', 'lmu', 'last', 'lfm'], spamwatchMiddleware, async (ctx) => {
|
||||
const userId = ctx.from.id;
|
||||
const Strings = getStrings(ctx.from.language_code);
|
||||
const lastfmUser = users[userId];
|
||||
|
||||
if (!lastfmUser) {
|
||||
return ctx.reply(Strings.lastFmNoUser, {
|
||||
return ctx.reply(Strings.lastFmNoSet, {
|
||||
parse_mode: "Markdown",
|
||||
reply_to_message_id: ctx.message.message_id
|
||||
});
|
||||
|
@ -51,7 +104,7 @@ module.exports = (bot) => {
|
|||
const imageExtralarge = track.image.find(img => img.size === 'extralarge');
|
||||
const imageMega = track.image.find(img => img.size === 'mega');
|
||||
const imageUrl = (imageExtralarge && imageExtralarge['#text']) || (imageMega && imageMega['#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)}`;
|
||||
|
@ -82,7 +135,7 @@ module.exports = (bot) => {
|
|||
reply_to_message_id: ctx.message.message_id
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
const message = Strings.lastFmStatusFor
|
||||
.replace("{lastfmUser}", `[${lastfmUser}](${userUrl})`)
|
||||
.replace("{nowPlaying}", nowPlaying)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue