Add seek validation, autoplay system, progress bar & codebase cleanup

- Add seek duration validation — seeks beyond track length are rejected
  with clear error showing max allowed position
- Add autoplay system with Last.fm similar tracks + YouTube search
  fallback — automatically queues similar tracks when queue ends
- Add live progress bar to now playing with elapsed/total time display
- Add robust ffmpeg cleanup to prevent race conditions during
  seek/skip/filter operations
- Add proper queue advancement after seek so bot stays in voice
- Add queueEnd event emission on skip/skipTo for autoplay trigger
- Add _lastPlayedTrack preservation for autoplay recommendations
This commit is contained in:
NoLogicAlan committed 2026-05-21 02:04:05 +03:00
1 parent bc1fe82919
commit 0f2ee99f1e
23 files changed
+665 -170

No files matched your search

+110 -11
View File
@@ -25,7 +25,6 @@ function normalizeTrackText(value) {
.toLowerCase();
}
/**
* Build the API signature required by Last.fm for authenticated calls.
* See: https:
@@ -71,7 +70,6 @@ async function apiCall(params, apiSecret, post = false) {
return data;
}
export class LastFmManager {
/**
* @param {object} config - The `lastfm` section from config.json
@@ -103,7 +101,6 @@ export class LastFmManager {
}
}
/**
* Set the bot ID for multi-bot database isolation.
* Ensures bot_id column exists in lastfm tables, then clears caches.
@@ -198,7 +195,6 @@ export class LastFmManager {
return { where: " AND bot_id = ?", params: [String(this.botId)] };
}
async _getPool() {
if (this._pool) return this._pool;
const mysql = await import("mysql2/promise");
@@ -243,7 +239,6 @@ export class LastFmManager {
`);
}
async getUser(userId) {
const cached = this._userCache.get(userId);
if (cached) return cached;
@@ -308,7 +303,6 @@ export class LastFmManager {
if (cached) cached.scrobbleEnabled = enabled;
}
/**
* Step 1: Get an auth token. The user must visit the Last.fm auth URL to approve it.
* @returns {Promise<string>} The auth token
@@ -342,7 +336,6 @@ export class LastFmManager {
return data.session;
}
/**
* Send a "now playing" notification to Last.fm (does NOT count as a scrobble).
* Called immediately when a track starts.
@@ -404,7 +397,6 @@ export class LastFmManager {
}
}
/**
* Get the user's loved tracks.
* @param {number} [limit=20] - Max tracks to return
@@ -516,6 +508,54 @@ export class LastFmManager {
}
}
/**
* Love a track on Last.fm for the given user.
* @param {string} userId - The Fluxer user ID
* @param {string} artist - Track artist
* @param {string} track - Track name
*/
async loveTrack(userId, artist, track) {
if (!this.enabled) return;
const user = await this.getUser(userId);
if (!user) throw new Error("NOT_LINKED");
await apiCall(
{
method: "track.love",
api_key: this.apiKey,
sk: user.sessionKey,
artist,
track,
},
this.apiSecret,
true
);
}
/**
* Unlove a track on Last.fm for the given user.
* @param {string} userId - The Fluxer user ID
* @param {string} artist - Track artist
* @param {string} track - Track name
*/
async unloveTrack(userId, artist, track) {
if (!this.enabled) return;
const user = await this.getUser(userId);
if (!user) throw new Error("NOT_LINKED");
await apiCall(
{
method: "track.unlove",
api_key: this.apiKey,
sk: user.sessionKey,
artist,
track,
},
this.apiSecret,
true
);
}
/**
* Search Last.fm for a freeform track query and return the best match.
* Useful for `%play lastfm: kendrick lamar luther` where artist/title
@@ -644,6 +684,39 @@ export class LastFmManager {
return scored[0]?.track ?? null;
}
/**
* Get similar tracks from Last.fm for a given track.
* @param {string} artist
* @param {string} track
* @param {number} [limit=5]
* @returns {Promise<Array<{ artist: string, name: string, url: string, match: number }>>}
*/
async getSimilarTracks(artist, track, limit = 5) {
if (!this.enabled) return [];
try {
const data = await apiCall(
{
method: "track.getsimilar",
api_key: this.apiKey,
artist,
track,
limit,
},
this.apiSecret
);
return (data.similartracks?.track ?? []).map(t => ({
artist: t.artist?.name ?? "Unknown",
name: t.name,
url: t.url ?? "",
match: parseFloat(t.match ?? 0),
})).filter(t => t.match > 0.1);
} catch {
return [];
}
}
/**
* Parse a Last.fm music URL and extract artist and track info.
* Supports:
@@ -717,7 +790,6 @@ export class LastFmManager {
return data.user;
}
/**
* Get a list of the user's Last.fm playlists by scraping their profile page.
* The Last.fm API removed user.getplaylists, so we fetch the HTML page.
@@ -893,6 +965,35 @@ export class LastFmManager {
}));
}
/**
* Get the user's top artists.
* @param {string} userId
* @param {string} [period="overall"] - overall | 7day | 1month | 3month | 6month | 12month
* @param {number} [limit=15]
* @returns {Promise<Array<{ name: string, url: string, playcount: number, image: string }>>}
*/
async getTopArtists(userId, period = "overall", limit = 15) {
const user = await this.getUser(userId);
if (!user) throw new Error("NOT_LINKED");
const data = await apiCall(
{
method: "user.gettopartists",
api_key: this.apiKey,
user: user.username,
period,
limit,
},
this.apiSecret
);
return (data.topartists?.artist ?? []).map(a => ({
name: a.name,
url: a.url ?? "",
playcount: a.playcount ?? 0,
image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "",
}));
}
/**
* Fetch tracks from Last.fm by category and return search queries
@@ -956,7 +1057,6 @@ export class LastFmManager {
};
}
/**
* Get the total lifetime scrobbles across ALL linked Last.fm users.
* Syncs each user's scrobble count from the Last.fm API, then sums them up.
@@ -1104,7 +1204,6 @@ export class LastFmManager {
}
}
_assertEnabled() {
if (!this.enabled) throw new Error("Last.fm integration is not configured (missing apiKey/apiSecret).");
}