fix(playback): name the real reason instead of 「区域/版权限制」

Every playback failure produced the same sentence:

  无法播放「XXX」(区域/版权限制)

It was wrong for at least four different causes, all of which take a different
fix:

  - the account has no membership        → open one (or use the shared login)
  - the track is delisted / region-locked → pick another track
  - this quality level is not ours        → drop the level (or wait for the fix)
  - upstream refused the IP (-460)        → wait, then retry

Measured on the reporter's own library: a free track (fee=8) returned `url:null`
at exhigh while playing fine at higher — "no copyright" was never true for it.
Three separate debugging sessions went after a copyright problem because the
message said so.

The information was there all along: `/song/url/v1` returns `code`, `fee`,
`payed` and `level` on every response. Nothing read them.

What this does:

- `PlayFailure` / `PlayFailureCode` in provider.ts: one shape for "why", with a
  ready-to-render `message` and the raw upstream fields kept separately for logs.
- `netease.getSongUrl` throws `SongUrlUnavailable` instead of returning a bare
  `null` for a 200-with-no-URL, classified by the fields above. Providers that
  have not adopted it keep returning null — `toPlayFailure` covers both.
- `resolveUrlWithRetry` gains `onExhausted`, called once when the budget is
  spent (never on cancellation — pressing stop is not a failure) and carrying
  the last attempt's error.
- `BotInstance` keeps the result as `lastPlayFailure` and exposes
  `getLastPlayFailure()`. Kept as a field rather than a second return value
  because `resolveAndPlay` has ~20 call sites and its boolean is load-bearing in
  tests; only the web routes need the explanation.
- `player.ts` renders that cause: 「无法播放「X」:需要会员(exhigh)」,
  「…:当前音质无播放权限(higher)」, 「…:无版权或已下架」. Batch paths
  (playlist / album / artist) append 「,最近一次:…」 instead of claiming every
  track had a copyright problem.
- The failure log line now carries `code`, `quality` and `upstream`
  (`{"fee":8,"payed":0,"level":"exhigh"}`) — the fields that were missing from
  the log during the previous two investigations.

The old wording survives only as a fallback for a bot that could not name a
cause, so behaviour is unchanged when nothing is known.

15 new tests: the five classifications (VIP / album-only / already-paid /
delisted / quality), upstream fields preserved, `onExhausted` fires exactly once
and never on cancel, still fires for a provider that returns null, and the
message is already displayable.

Note for reviewers: this changes `netease.getSongUrl` from returning null to
throwing. `BotInstance` already wraps it in try/catch on the stream-recovery
path, and `resolveUrlWithRetry` catches everything, so the two production
callers are covered. Anything that wraps `getSongUrl` itself (e.g. the
per-user fallback in #186) needs a `catch` — flagged there.
This commit is contained in:
jiayuLian committed 2026-11-08 18:28:36 +08:00
1 parent e9995c1e30
commit 4c25cfe5e5
7 files changed
+436 -23

No files matched your search

+32 -9
View File
@@ -61,6 +61,29 @@ export function createPlayerRouter(
next();
});
/**
* One line explaining why a track would not play.
*
* Every failure used to say 「(区域/版权限制)」 — including a lapsed
* membership, a delisted track and a quality level the account cannot
* stream. That single string is why three separate investigations went
* hunting for a copyright problem that was not there. The bot now records
* the real cause; this only falls back to the old wording when it has none.
*/
const whyUnplayable = (bot: any, song: { name?: string; id?: string }): string => {
const label = song.name || song.id || "这首歌";
const reason: string | undefined = bot?.getLastPlayFailure?.()?.message;
return reason
? `无法播放「${label}」:${reason}`
: `无法播放「${label}」(区域/版权限制)`;
};
/** Same, for the batch paths where a cause is context, not the headline. */
const lastReason = (bot: any): string => {
const failure = bot?.getLastPlayFailure?.();
return failure?.message ? `,最近一次:${failure.message}` : "";
};
/** Map API platform string to the corresponding command flag. */
const platformFlag = (platform: unknown): string => {
if (platform === "bilibili") return "-b";
@@ -370,7 +393,7 @@ export function createPlayerRouter(
// the sequential retry path, which still has a chance.
}
if (queueable.length === 0) {
res.json({ ok: false, message: `歌单 ${totalCount} 首歌曲均无版权可播放(区域/版权限制)` });
res.json({ ok: false, message: `歌单 ${totalCount} 首歌曲均无法播放${lastReason(bot)}` });
return;
}
@@ -406,7 +429,7 @@ export function createPlayerRouter(
const playing = queue.current();
const loadedMsg = queueable.length < totalCount
? `已加载 ${queueable.length}/${totalCount} 首(其余区域/版权限制)`
? `已加载 ${queueable.length}/${totalCount} 首(其余无法播放${lastReason(bot)})`
: `已加载 ${queueable.length} 首`;
if (started && playing) {
res.json({ ok: true, message: `${loadedMsg},正在播放:${playing.name}` });
@@ -456,7 +479,7 @@ export function createPlayerRouter(
}
}
if (queueable.length === 0) {
res.json({ ok: false, message: `专辑 ${totalCount} 首歌曲均无版权可播放(区域/版权限制)` });
res.json({ ok: false, message: `专辑 ${totalCount} 首歌曲均无法播放${lastReason(bot)}` });
return;
}
@@ -484,7 +507,7 @@ export function createPlayerRouter(
const playing = queue.current();
const loadedMsg = queueable.length < totalCount
? `已加载 ${queueable.length}/${totalCount} 首(其余区域/版权限制)`
? `已加载 ${queueable.length}/${totalCount} 首(其余无法播放${lastReason(bot)})`
: `已加载 ${queueable.length} 首`;
if (started && playing) {
res.json({ ok: true, message: `${loadedMsg},正在播放:${playing.name}` });
@@ -546,7 +569,7 @@ export function createPlayerRouter(
}
}
if (queueable.length === 0) {
res.json({ ok: false, message: `歌手 ${totalCount} 首歌曲均无版权可播放(区域/版权限制)` });
res.json({ ok: false, message: `歌手 ${totalCount} 首歌曲均无法播放${lastReason(bot)}` });
return;
}
@@ -577,7 +600,7 @@ export function createPlayerRouter(
const playing = queue.current();
const loadedMsg = queueable.length < totalCount
? `已加载 ${queueable.length}/${totalCount} 首(其余区域/版权限制)`
? `已加载 ${queueable.length}/${totalCount} 首(其余无法播放${lastReason(bot)})`
: `已加载 ${queueable.length} 首`;
return started && playing
? { ok: true, message: `${loadedMsg},正在播放:${playing.name}` }
@@ -614,7 +637,7 @@ export function createPlayerRouter(
const ok = await bot.playSingleSong({ ...song }, requesterName(req));
return ok
? { ok: true, message: `正在播放:${song.name || 'Unknown'} - ${song.artist || 'Unknown'}` }
: { ok: false, message: `无法播放「${song.name || song.id}」(区域/版权限制)` };
: { ok: false, message: whyUnplayable(bot, song) };
});
res.json(body);
} catch (err) {
@@ -656,7 +679,7 @@ export function createPlayerRouter(
bot.getPlayer().resetFailures();
const ok = await bot.resolveAndPlay(queue.current()!);
if (!ok) {
return { ok: false, message: `无法播放「${song.name || song.id}」(区域/版权限制)` };
return { ok: false, message: whyUnplayable(bot, song) };
}
return { ok: true, message: `正在播放:${song.name || 'Unknown'} - ${song.artist || 'Unknown'}` };
}
@@ -695,7 +718,7 @@ export function createPlayerRouter(
bot.getPlayer().resetFailures();
const ok = await bot.resolveAndPlay(queue.current()!);
if (!ok) {
return { ok: false, message: `无法播放「${song.name || song.id}」(区域/版权限制)` };
return { ok: false, message: whyUnplayable(bot, song) };
}
return { ok: true, message: `正在播放:${song.name || "Unknown"} - ${song.artist || "Unknown"}` };
});