palico-bot/src/features/formatTrack.js
Shinuwa 793765fb79 Resume interrupted tracks where they stopped
A track cut off without anyone asking (voice connection lost, bot
leaving an empty channel, restart) used to start over from the
beginning; on an hour-long mix, a drop near the end replayed it all.

- The requeued track keeps resumeAt and ffmpeg seeks to it (-ss before
  -i, so it jumps there without decoding the start). It resumes 2 s
  before the cut; a track cut in its first 2 s starts over.
- The player keeps playing without a connection
  (NoSubscriberBehavior.Play), so the position of a voice drop is taken
  when the connection drops, not 5 s later when it is given up.
- The saved queue holds the current position, and it is saved every
  30 s while playing: after a crash the track resumes at most 30 s
  earlier.
- Loop mode replays a resumed track from the beginning; skip and stop
  are unchanged.
- The web page and /queue show "Reprise à 49:15" for such a track, and
  the progress bar counts from the resume point.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 23:11:05 +01:00

29 lines
1.1 KiB
JavaScript

/**
* @file Formate un morceau pour les messages Discord : titre, durée lisible et, pour un morceau
* interrompu, sa position de reprise.
*
* Utilisé par : les commandes /play, /queue et /skip.
* Attention : la page web a son propre formatDuration (public/scripts/common.js).
*/
// Durée lisible : 3:07, 1:05:09.
function formatDuration(seconds) {
if (!Number.isFinite(seconds) || seconds < 0) return null;
const total = Math.round(seconds);
const hours = Math.floor(total / 3600);
const minutes = Math.floor((total % 3600) / 60);
const secs = String(total % 60).padStart(2, "0");
return hours > 0 ? `${hours}:${String(minutes).padStart(2, "0")}:${secs}` : `${minutes}:${secs}`;
}
function trackLabel(track) {
const title = track.title || track.originalName || "Sans titre";
const details = [formatDuration(track.duration)];
if (track.resumeAt > 0) {
details.push(`reprise à ${formatDuration(track.resumeAt)}`);
}
const text = details.filter(Boolean).join(", ");
return text ? `${title} (${text})` : title;
}
export { formatDuration, trackLabel };