Tie playback to the listener and keep the panel's view live

Four things people hit while using the panel:

- The bot could be sent into a channel the requester was not in, and
  playback could be started from nowhere. Playback now follows the
  listener (REQUIRE_LISTENER, on by default), and the voice row shows
  where you and the bot are instead of offering a free channel picker.
- Voice presence only refreshed on reload, because the SDK updates
  channel participants without emitting an event. The socket now watches
  that view and pushes changes.
- The bot left the channel whenever the queue ran dry. It now leaves only
  after the last person does, EMPTY_TIMEOUT_SECONDS later (120 by
  default), and stays put while anyone is still listening.
- A search that yielded nothing said nothing: yt-dlp can exit 0 with an
  empty result, so that case now reports the reason (or "nothing found"),
  and searches are logged with their result count.

The queue moved under the player so search owns the left column, and
elapsed time no longer renders as "LIVE" — formatDuration treated 0 as a
live stream, which also affected the chat's progress bar.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Leonid Pershin
2026-09-08 23:59:17 +03:00
co-authored by Claude Opus 5
parent f22b08b350
commit 3e53f34374
16 changed files with 1819 additions and 1657 deletions
+60 -52
View File
@@ -1,52 +1,60 @@
import type { LoopMode, Track } from "../types.js";
export function formatDuration(seconds: number): string {
if (!Number.isFinite(seconds) || seconds <= 0) return "LIVE";
const total = Math.floor(seconds);
const hours = Math.floor(total / 3600);
const minutes = Math.floor((total % 3600) / 60);
const secs = total % 60;
const pad = (value: number) => value.toString().padStart(2, "0");
return hours > 0 ? `${hours}:${pad(minutes)}:${pad(secs)}` : `${minutes}:${pad(secs)}`;
}
export function parseTimecode(input: string): number | null {
const trimmed = input.trim();
if (/^\d+$/.test(trimmed)) return Number.parseInt(trimmed, 10);
const match = /^(?:(\d+):)?(\d{1,2}):(\d{1,2})$/.exec(trimmed);
if (!match) return null;
const [, hours, minutes, seconds] = match;
return (
Number.parseInt(hours ?? "0", 10) * 3600 +
Number.parseInt(minutes ?? "0", 10) * 60 +
Number.parseInt(seconds ?? "0", 10)
);
}
export function progressBar(position: number, duration: number, width = 22): string {
if (duration <= 0) return "🔴 прямой эфир";
const ratio = Math.min(1, Math.max(0, position / duration));
const filled = Math.round(ratio * (width - 1));
const bar = `${"─".repeat(filled)}${"".repeat(Math.max(0, width - 1 - filled))}`;
return `\`${formatDuration(position)}\` ${bar} \`${formatDuration(duration)}\``;
}
const SOURCE_LABEL: Record<Track["source"], string> = {
youtube: "YouTube",
soundcloud: "SoundCloud",
direct: "Ссылка",
local: "Медиатека",
};
export function trackLine(track: Track, index?: number): string {
const prefix = index === undefined ? "" : `**${index}.** `;
const author = track.author ? `${track.author}` : "";
const link = /^https?:/i.test(track.url) ? `[${track.title}](${track.url})` : track.title;
return `${prefix}${link}${author} \`[${formatDuration(track.duration)}]\` · ${SOURCE_LABEL[track.source]} · <@${track.requestedBy.id}>`;
}
export function loopLabel(mode: LoopMode): string {
if (mode === "track") return "трек";
if (mode === "queue") return "очередь";
return "выключен";
}
import type { LoopMode, Track } from "../types.js";
/** Clock formatting for any position or length; 0 is a legitimate "0:00". */
export function formatDuration(seconds: number): string {
if (!Number.isFinite(seconds) || seconds < 0) return "0:00";
const total = Math.floor(seconds);
const hours = Math.floor(total / 3600);
const minutes = Math.floor((total % 3600) / 60);
const secs = total % 60;
const pad = (value: number) => value.toString().padStart(2, "0");
return hours > 0 ? `${hours}:${pad(minutes)}:${pad(secs)}` : `${minutes}:${pad(secs)}`;
}
/** Track length as shown to people: live streams and unknown lengths are not clocks. */
export function formatLength(track: { duration: number; isLive: boolean }): string {
if (track.isLive) return "LIVE";
if (track.duration <= 0) return "—";
return formatDuration(track.duration);
}
export function parseTimecode(input: string): number | null {
const trimmed = input.trim();
if (/^\d+$/.test(trimmed)) return Number.parseInt(trimmed, 10);
const match = /^(?:(\d+):)?(\d{1,2}):(\d{1,2})$/.exec(trimmed);
if (!match) return null;
const [, hours, minutes, seconds] = match;
return (
Number.parseInt(hours ?? "0", 10) * 3600 +
Number.parseInt(minutes ?? "0", 10) * 60 +
Number.parseInt(seconds ?? "0", 10)
);
}
export function progressBar(position: number, duration: number, width = 22): string {
if (duration <= 0) return "🔴 прямой эфир";
const ratio = Math.min(1, Math.max(0, position / duration));
const filled = Math.round(ratio * (width - 1));
const bar = `${"─".repeat(filled)}${"─".repeat(Math.max(0, width - 1 - filled))}`;
return `\`${formatDuration(position)}\` ${bar} \`${formatDuration(duration)}\``;
}
const SOURCE_LABEL: Record<Track["source"], string> = {
youtube: "YouTube",
soundcloud: "SoundCloud",
direct: "Ссылка",
local: "Медиатека",
};
export function trackLine(track: Track, index?: number): string {
const prefix = index === undefined ? "" : `**${index}.** `;
const author = track.author ? `${track.author}` : "";
const link = /^https?:/i.test(track.url) ? `[${track.title}](${track.url})` : track.title;
return `${prefix}${link}${author} \`[${formatLength(track)}]\` · ${SOURCE_LABEL[track.source]} · <@${track.requestedBy.id}>`;
}
export function loopLabel(mode: LoopMode): string {
if (mode === "track") return "трек";
if (mode === "queue") return "очередь";
return "выключен";
}