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
+23 -9
View File
@@ -61,8 +61,14 @@ export async function checkCookies(): Promise<CookieStatus | null> {
}
}
function runYtDlp(args: string[], timeoutMs = 45_000): Promise<string> {
return new Promise((resolve, reject) => {
interface YtDlpRun {
stdout: string;
stderr: string;
code: number | null;
}
function runYtDlp(args: string[], timeoutMs = 45_000): Promise<YtDlpRun> {
return new Promise<YtDlpRun>((resolve, reject) => {
const child = spawn(config.YTDLP_PATH, args, { windowsHide: true });
let stdout = "";
let stderr = "";
@@ -88,7 +94,7 @@ function runYtDlp(args: string[], timeoutMs = 45_000): Promise<string> {
child.on("close", (code) => {
clearTimeout(timer);
if (code === 0 || stdout.trim().length > 0) {
resolve(stdout);
resolve({ stdout, stderr, code });
return;
}
log.warn({ code, stderr: stderr.slice(0, 800) }, "yt-dlp failed");
@@ -160,18 +166,26 @@ export async function search(
requestedBy: Requester,
): Promise<Track[]> {
const prefix = kind === "soundcloud" ? "scsearch" : "ytsearch";
const stdout = await runYtDlp([
const { stdout, stderr } = await runYtDlp([
...baseArgs(),
"--flat-playlist",
"--dump-json",
`${prefix}${limit}:${query}`,
]);
return parseNdjson(stdout).map((entry) => toTrack(entry, requestedBy, kind));
const entries = parseNdjson(stdout);
// yt-dlp can exit 0 with nothing to show (bot checks, region blocks). Without
// this the panel would just render an empty list and say nothing at all.
if (entries.length === 0 && stderr.trim()) {
log.warn({ query, kind, stderr: stderr.slice(0, 500) }, "search returned nothing");
throw new UserFacingError(firstUsefulError(stderr));
}
log.info({ query, kind, count: entries.length }, "search");
return entries.map((entry) => toTrack(entry, requestedBy, kind));
}
/** Resolves a URL that may point at a single track, a playlist, or an album. */
export async function resolveUrl(url: string, requestedBy: Requester, maxTracks: number): Promise<SearchResult> {
const stdout = await runYtDlp([
const { stdout } = await runYtDlp([
...baseArgs(),
"--flat-playlist",
"--dump-single-json",
@@ -200,7 +214,7 @@ export async function resolveUrl(url: string, requestedBy: Requester, maxTracks:
/** Resolves a direct, time-limited media URL for a page URL (used when seeking). */
export async function resolveStreamUrl(pageUrl: string): Promise<string> {
const stdout = await runYtDlp([...baseArgs(), "-f", "bestaudio/best", "--no-playlist", "-g", pageUrl]);
const { stdout } = await runYtDlp([...baseArgs(), "-f", "bestaudio/best", "--no-playlist", "-g", pageUrl]);
const url = stdout.split(/\r?\n/).map((l) => l.trim()).find(Boolean);
if (!url) throw new UserFacingError("Не удалось получить ссылку на аудиопоток");
return url;
@@ -252,8 +266,8 @@ export function openAudioStream(pageUrl: string): AudioProcess {
export async function checkAvailable(): Promise<string | null> {
try {
const out = await runYtDlp(["--version"], 15_000);
return out.trim() || null;
const { stdout } = await runYtDlp(["--version"], 15_000);
return stdout.trim() || null;
} catch {
return null;
}