feat(player): persist and restore autoplay state across restarts

- Save autoplay flag, recent history, and last played track in session snapshots
- Guard against pruning active autoplay sessions with empty queues
- Dynamically re-attach queueEnd autoplay listener during session restoration
- Trigger recommendation engine automatically if recovered with empty queue
- Persist player snapshots immediately when toggling autoplay on or off
This commit is contained in:
NoLogicAlan committed 2026-11-13 02:08:28 +03:00
1 parent 84c597ec3c
commit f1b2e43f8a
3 files changed
+33 -2

No files matched your search

+27 -1
View File
@@ -446,6 +446,22 @@ const BotVoiceMixin = {
player.queue.loop = !!snap.loop;
player.queue.songLoop = !!snap.songLoop;
if (snap.autoplay) {
player._autoplay = true;
if (Array.isArray(snap.autoplayHistory)) {
player._autoplayHistory = [...snap.autoplayHistory];
}
if (snap.lastTrack && !player._lastPlayedTrack) {
player._lastPlayedTrack = snap.lastTrack;
}
try {
const { attachAutoplay } = await import("../../commands/autoplay.mjs");
attachAutoplay(player, this);
} catch (e) {
logger.warn(`[Restore] Autoplay re-attach failed for guild ${player._guildId}:`, e?.message);
}
}
const restoredQueue = (Array.isArray(snap.queue) ? snap.queue : [])
.filter((t) => t && (t.url || t.encoded));
@@ -457,12 +473,22 @@ const BotVoiceMixin = {
else current = null;
}
if (current && !player._lastPlayedTrack) {
player._lastPlayedTrack = current;
}
player.queue.data = [];
if (current) player.queue.data.push(current);
for (const t of restoredQueue) player.queue.data.push(t);
const count = player.queue.data.length;
if (!count) return { count: 0, pos };
if (!count) {
if (snap.autoplay && player._autoplayHandler && player._lastPlayedTrack) {
player._autoplayHandler().catch((e) => logger.warn(`[Restore] Autoplay pick failed for guild ${player._guildId}:`, e?.message));
return { count: 1, pos: 0 };
}
return { count: 0, pos };
}
if (announce) {
const restoreEmbed = new EmbedBuilder()
+4 -1
View File
@@ -153,7 +153,7 @@ export class PlayerStateStore {
const current = player.queue?.getCurrent?.() ?? null;
const queued = Array.isArray(player.queue?.data) ? player.queue.data : [];
if (!current && queued.length === 0) {
if (!current && queued.length === 0 && !player._autoplay) {
return this.clearSnapshot(guildId);
}
@@ -172,6 +172,9 @@ export class PlayerStateStore {
volume: Number.isFinite(player.preferredVolume) ? player.preferredVolume : null,
loop: !!player.queue?.loop,
songLoop: !!player.queue?.songLoop,
autoplay: !!player._autoplay,
autoplayHistory: Array.isArray(player._autoplayHistory) ? player._autoplayHistory.slice(-20) : [],
lastTrack: slimTrack(player._lastPlayedTrack),
current: slimTrack(current),
positionMs,
queue: queued.slice(0, MAX_PERSISTED_TRACKS).map(slimTrack).filter(Boolean),