mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-02 19:02:50 +08:00
fix(playback): cancel stale retries and scope failure messages
This commit is contained in:
1 parent
ea05787cb7
commit
90a4d80958
7 files changed
+126
-174
No files matched your search
+35
-45
@@ -1,7 +1,9 @@
|
||||
import { Router } from "express";
|
||||
import type { BotInstance } from "../../bot/instance.js";
|
||||
import type { QueuedSong } from "../../audio/queue.js";
|
||||
import type { BotManager } from "../../bot/manager.js";
|
||||
import type { BotDatabase } from "../../data/database.js";
|
||||
import type { MusicProvider, Song, ArtistSongPage } from "../../music/provider.js";
|
||||
import type { MusicProvider, Song, ArtistSongPage, PlayFailure } from "../../music/provider.js";
|
||||
import type { Logger } from "../../logger.js";
|
||||
import { parseCommand } from "../../bot/commands.js";
|
||||
import { requireBotAccess } from "../middleware/requirePermission.js";
|
||||
@@ -61,28 +63,20 @@ 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 whyUnplayable = (song: { name?: string; id?: string }, reason?: string): string => {
|
||||
const label = song.name || song.id || "这首歌";
|
||||
const reason: string | undefined = bot?.getLastPlayFailure?.()?.message;
|
||||
return reason
|
||||
? `无法播放「${label}」:${reason}`
|
||||
: `无法播放「${label}」(区域/版权限制)`;
|
||||
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}` : "";
|
||||
};
|
||||
async function startCollection(bot: BotInstance, first: QueuedSong | null) {
|
||||
let failure: PlayFailure | undefined;
|
||||
const capture = (reason: PlayFailure) => { failure = reason; };
|
||||
let started = first ? await bot.resolveAndPlay(first, capture) : false;
|
||||
if (first && !started && failure?.code !== "risk_control" && bot.getQueueManager().current() === first) {
|
||||
started = await bot.playNext(20, capture);
|
||||
}
|
||||
return { started, reason: failure?.message };
|
||||
}
|
||||
|
||||
/** Map API platform string to the corresponding command flag. */
|
||||
const platformFlag = (platform: unknown): string => {
|
||||
@@ -393,7 +387,7 @@ export function createPlayerRouter(
|
||||
// the sequential retry path, which still has a chance.
|
||||
}
|
||||
if (queueable.length === 0) {
|
||||
res.json({ ok: false, message: `歌单 ${totalCount} 首歌曲均无法播放${lastReason(bot)}` });
|
||||
res.json({ ok: false, message: `歌单 ${totalCount} 首歌曲均无法播放` });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -422,19 +416,16 @@ export function createPlayerRouter(
|
||||
// retry-skip behavior. Use a higher retry budget than the default
|
||||
// trackEnd auto-advance because user-initiated playlist plays
|
||||
// commonly have long contiguous runs of unplayable songs.
|
||||
let started = first ? await bot.resolveAndPlay(first) : false;
|
||||
if (first && !started) {
|
||||
started = await bot.playNext(20);
|
||||
}
|
||||
const { started, reason } = await startCollection(bot, first);
|
||||
|
||||
const playing = queue.current();
|
||||
const loadedMsg = queueable.length < totalCount
|
||||
? `已加载 ${queueable.length}/${totalCount} 首(其余无法播放${lastReason(bot)})`
|
||||
? `已加载 ${queueable.length}/${totalCount} 首(其余无法播放)`
|
||||
: `已加载 ${queueable.length} 首`;
|
||||
if (started && playing) {
|
||||
res.json({ ok: true, message: `${loadedMsg},正在播放:${playing.name}` });
|
||||
} else {
|
||||
res.json({ ok: false, message: `${loadedMsg},但无法开始播放。` });
|
||||
res.json({ ok: false, message: `${loadedMsg},但无法开始播放${reason ? `:${reason}` : "。"}` });
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error({ err }, "Play playlist failed");
|
||||
@@ -479,7 +470,7 @@ export function createPlayerRouter(
|
||||
}
|
||||
}
|
||||
if (queueable.length === 0) {
|
||||
res.json({ ok: false, message: `专辑 ${totalCount} 首歌曲均无法播放${lastReason(bot)}` });
|
||||
res.json({ ok: false, message: `专辑 ${totalCount} 首歌曲均无法播放` });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -500,19 +491,16 @@ export function createPlayerRouter(
|
||||
first = queue.play();
|
||||
}
|
||||
|
||||
let started = first ? await bot.resolveAndPlay(first) : false;
|
||||
if (first && !started) {
|
||||
started = await bot.playNext(20);
|
||||
}
|
||||
const { started, reason } = await startCollection(bot, first);
|
||||
|
||||
const playing = queue.current();
|
||||
const loadedMsg = queueable.length < totalCount
|
||||
? `已加载 ${queueable.length}/${totalCount} 首(其余无法播放${lastReason(bot)})`
|
||||
? `已加载 ${queueable.length}/${totalCount} 首(其余无法播放)`
|
||||
: `已加载 ${queueable.length} 首`;
|
||||
if (started && playing) {
|
||||
res.json({ ok: true, message: `${loadedMsg},正在播放:${playing.name}` });
|
||||
} else {
|
||||
res.json({ ok: false, message: `${loadedMsg},但无法开始播放。` });
|
||||
res.json({ ok: false, message: `${loadedMsg},但无法开始播放${reason ? `:${reason}` : "。"}` });
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error({ err }, "play-album failed");
|
||||
@@ -569,7 +557,7 @@ export function createPlayerRouter(
|
||||
}
|
||||
}
|
||||
if (queueable.length === 0) {
|
||||
res.json({ ok: false, message: `歌手 ${totalCount} 首歌曲均无法播放${lastReason(bot)}` });
|
||||
res.json({ ok: false, message: `歌手 ${totalCount} 首歌曲均无法播放` });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -595,16 +583,15 @@ export function createPlayerRouter(
|
||||
first = queue.play();
|
||||
}
|
||||
|
||||
let started = first ? await bot.resolveAndPlay(first) : false;
|
||||
if (first && !started) started = await bot.playNext(20);
|
||||
const { started, reason } = await startCollection(bot, first);
|
||||
|
||||
const playing = queue.current();
|
||||
const loadedMsg = queueable.length < totalCount
|
||||
? `已加载 ${queueable.length}/${totalCount} 首(其余无法播放${lastReason(bot)})`
|
||||
? `已加载 ${queueable.length}/${totalCount} 首(其余无法播放)`
|
||||
: `已加载 ${queueable.length} 首`;
|
||||
return started && playing
|
||||
? { ok: true, message: `${loadedMsg},正在播放:${playing.name}` }
|
||||
: { ok: false, message: `${loadedMsg},但无法开始播放。` };
|
||||
: { ok: false, message: `${loadedMsg},但无法开始播放${reason ? `:${reason}` : "。"}` };
|
||||
});
|
||||
res.json(body);
|
||||
} catch (err) {
|
||||
@@ -634,10 +621,11 @@ export function createPlayerRouter(
|
||||
// clear branch — do NOT also call it here, or it would delete retained
|
||||
// local uploads in keep-queue mode.
|
||||
const body = await bot.runExclusive(async () => {
|
||||
const ok = await bot.playSingleSong({ ...song }, requesterName(req));
|
||||
let reason: string | undefined;
|
||||
const ok = await bot.playSingleSong({ ...song }, requesterName(req), (failure: PlayFailure) => { reason = failure.message; });
|
||||
return ok
|
||||
? { ok: true, message: `正在播放:${song.name || 'Unknown'} - ${song.artist || 'Unknown'}` }
|
||||
: { ok: false, message: whyUnplayable(bot, song) };
|
||||
: { ok: false, message: whyUnplayable(song, reason) };
|
||||
});
|
||||
res.json(body);
|
||||
} catch (err) {
|
||||
@@ -677,9 +665,10 @@ export function createPlayerRouter(
|
||||
// Promote the just-added song to current and start it.
|
||||
queue.playAt(insertedAt);
|
||||
bot.getPlayer().resetFailures();
|
||||
const ok = await bot.resolveAndPlay(queue.current()!);
|
||||
let reason: string | undefined;
|
||||
const ok = await bot.resolveAndPlay(queue.current()!, (failure: PlayFailure) => { reason = failure.message; });
|
||||
if (!ok) {
|
||||
return { ok: false, message: whyUnplayable(bot, song) };
|
||||
return { ok: false, message: whyUnplayable(song, reason) };
|
||||
}
|
||||
return { ok: true, message: `正在播放:${song.name || 'Unknown'} - ${song.artist || 'Unknown'}` };
|
||||
}
|
||||
@@ -716,9 +705,10 @@ export function createPlayerRouter(
|
||||
queue.addNext({ ...song, requestedBy: requesterName(req) });
|
||||
queue.playAt(insertedAt);
|
||||
bot.getPlayer().resetFailures();
|
||||
const ok = await bot.resolveAndPlay(queue.current()!);
|
||||
let reason: string | undefined;
|
||||
const ok = await bot.resolveAndPlay(queue.current()!, (failure: PlayFailure) => { reason = failure.message; });
|
||||
if (!ok) {
|
||||
return { ok: false, message: whyUnplayable(bot, song) };
|
||||
return { ok: false, message: whyUnplayable(song, reason) };
|
||||
}
|
||||
return { ok: true, message: `正在播放:${song.name || "Unknown"} - ${song.artist || "Unknown"}` };
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user