feat(lastfm): restrict non-lastfm providers to top category only

This commit is contained in:
NoLogicAlan committed 2026-05-18 22:57:52 +03:00
1 parent 2292a74274
commit eaaa1f6131
2 files changed
+190 -10

No files matched your search

+86 -2
View File
@@ -7,6 +7,57 @@ import { playLastFmCategory } from "./lastfm.mjs";
// Last.fm provider keywords that trigger special play logic
const LASTFM_PLAY_CATEGORIES = ["loved", "top", "recent", "albums"];
/**
* Parse a lastfm query like "td:top", "sp:loved", "playlist 1", or just "top".
* Returns { category, resolveProvider } where resolveProvider is the
* search provider to use for resolving tracks (e.g. "td" for Tidal).
*/
function parseLastFmSubProvider(query) {
const value = query.trim();
// Non-lastfm providers only allow "top" (or bare provider defaults to "top")
// Last.fm as resolve provider allows all categories: loved, top, recent, albums, playlist
const subMatch = value.match(/^([a-z]+):\s*(.*)$/i);
if (subMatch) {
const maybeProvider = subMatch[1].toLowerCase();
const rest = subMatch[2].trim().toLowerCase();
if (PROVIDER_CHOICES.includes(maybeProvider)) {
const isLastFmProvider = maybeProvider === "lf" || maybeProvider === "lastfm";
if (!rest) {
if (isLastFmProvider) {
return { category: "", resolveProvider: maybeProvider };
}
return { category: "top", resolveProvider: maybeProvider };
}
if (isLastFmProvider) {
// Last.fm as resolve provider: all categories allowed
if (LASTFM_PLAY_CATEGORIES.includes(rest) || rest.startsWith("playlist")) {
return { category: rest, resolveProvider: maybeProvider };
}
} else {
// Non-lastfm providers: only "top" allowed
if (rest === "top") {
return { category: "top", resolveProvider: maybeProvider };
}
return { category: "", resolveProvider: maybeProvider, invalidCategory: rest };
}
}
}
// Bare provider without colon: "td", "sp", "yt" → defaults to "top"
const lower = value.toLowerCase();
if (PROVIDER_CHOICES.includes(lower)) {
const isLastFmProvider = lower === "lf" || lower === "lastfm";
if (isLastFmProvider) {
return { category: "", resolveProvider: lower };
}
return { category: "top", resolveProvider: lower };
}
return { category: lower, resolveProvider: null };
}
function parseInlineProvider(raw) {
const match = raw.match(/^([a-z]+):\s*(.+)$/i);
if (!match) return { provider: null, query: raw.trim() };
@@ -59,6 +110,9 @@ export const command = new CommandBuilder()
"$prefixplay -p yt take over league of legends",
"$prefixplay https://open.spotify.com/track/...",
"$prefixplay lastfm:loved",
"$prefixplay lastfm:td:top",
"$prefixplay lastfm:sp",
"$prefixplay lastfm:lf:loved",
"$prefixp take over league of legends"
)
.addTextOption(option =>
@@ -82,13 +136,42 @@ export async function run(message, data) {
const provider = inlineProvider ?? flagProvider ?? "ytm";
// ── Last.fm special provider: %play lastfm:loved / lastfm:top / lastfm:recent / lastfm:playlist 1 ──
// Also supports sub-provider syntax: %play lastfm:td:top (play Last.fm top tracks, search on Tidal)
// Bare provider defaults: %play lastfm:sp → top tracks on Spotify, %play lastfm:td → top tracks on Tidal
if (provider === "lastfm" || provider === "lf") {
const lowerQuery = query.toLowerCase().trim();
const parsed = parseLastFmSubProvider(query);
const lowerQuery = parsed.category;
const resolveProvider = parsed.resolveProvider;
// Invalid category used with a non-lastfm provider (e.g. "sp:loved", "td:recent")
if (parsed.invalidCategory) {
return message.reply({
embeds: [new EmbedBuilder().setColor("#ff0000").setDescription(
`❌ \`${resolveProvider}:${parsed.invalidCategory}\` is not valid. Non-Last.fm providers only support \`top\`.\nUse \`lastfm:${resolveProvider}\` or \`lastfm:${resolveProvider}:top\` instead.\nFor other categories, use Last.fm as the resolve provider: \`lastfm:lf:${parsed.invalidCategory}\``
)]
});
}
// If lf/lastfm used as resolve provider without a category, show help
if (!lowerQuery && resolveProvider && (resolveProvider === "lf" || resolveProvider === "lastfm")) {
return message.reply({
embeds: [new EmbedBuilder().setColor("#ff0000").setDescription([
`❌ Specify a Last.fm category after \`lf:\``,
``,
`**Available categories:**`,
`\`lf:loved\` — Play your loved tracks`,
`\`lf:top\` — Play your top tracks`,
`\`lf:recent\` — Play your recent tracks`,
`\`lf:albums\` — Play your top albums`,
`\`lf:playlist 1\` — Play a playlist`,
].join("\n"))]
});
}
// Simple categories: lastfm:loved, lastfm:top, lastfm:recent
if (LASTFM_PLAY_CATEGORIES.includes(lowerQuery)) {
const userId = message.message?.author?.id ?? message.author?.id;
return playLastFmCategory(this, message, userId, lowerQuery);
return playLastFmCategory(this, message, userId, lowerQuery, { resolveProvider });
}
// Playlist: lastfm:playlist 1 or lastfm:playlist 3
@@ -97,6 +180,7 @@ export async function run(message, data) {
const userId = message.message?.author?.id ?? message.author?.id;
return playLastFmCategory(this, message, userId, "playlist", {
playlistId: playlistMatch[1],
resolveProvider,
});
}
}