From d0e1422af701a56da294db0b8fb9fc556ecffc25 Mon Sep 17 00:00:00 2001 From: TIANYAO ZHANG <88520881+ZHANGTIANYAO1@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:21:08 +0800 Subject: [PATCH] fix(chat): report failed starts for queue and collection commands --- src/bot/instance.test.ts | 6 ++++++ src/bot/instance.ts | 32 +++++++++++++++++++------------- src/web/api/player.ts | 5 +---- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/bot/instance.test.ts b/src/bot/instance.test.ts index fce36bd..9da915b 100644 --- a/src/bot/instance.test.ts +++ b/src/bot/instance.test.ts @@ -1710,6 +1710,12 @@ describe("cmdPlaylist with a playlist link (#160)", () => { expect(ctx.providers.netease.search).not.toHaveBeenCalled(); }); + it("reports a refused first track instead of announcing playback", async () => { + const ctx = makeCtx(); + ctx.resolveAndPlay.mockResolvedValue(false); + expect(await cmdPlaylist.call(ctx, cmd("2829883282"))).toBe("Cannot play: Song"); + }); + it("checks the link's platform is enabled", async () => { const ctx = makeCtx(); ctx.assertProviderEnabled.mockImplementation(() => { throw new Error("音源未启用:qq"); }); diff --git a/src/bot/instance.ts b/src/bot/instance.ts index 3ff0254..c871cd4 100755 --- a/src/bot/instance.ts +++ b/src/bot/instance.ts @@ -59,6 +59,13 @@ import { /** Reply sent when a non-admin invokes an admin-only chat command. */ export const COMMAND_DENIED_MESSAGE = "⛔ 需要管理员权限(该命令仅限管理员服务器组)"; +async function startTrackForCommand(bot: BotInstance, song: QueuedSong | null): Promise { + if (!song) return "No track to play"; + let reason: string | undefined; + const started = await bot.resolveAndPlay(song, failure => { reason = failure.message; }); + if (!started) return reason ? `Cannot play: ${song.name} — ${reason}` : `Cannot play: ${song.name}`; +} + /** Maps the persisted / command-line play-mode string to the PlayMode enum. * Shared by the !mode command and the restart-restore path (#125). */ const PLAY_MODE_BY_VALUE: Record = { @@ -1600,9 +1607,9 @@ export class BotInstance extends EventEmitter { if (wasIdle) { this.queue.playAt(this.queue.size() - 1); this.player.resetFailures(); - await this.resolveAndPlay(this.queue.current()!); + const failure = await startTrackForCommand(this, this.queue.current()); this.emit("stateChange"); - return `Now playing: ${s.name} - ${s.artist}`; + return failure ?? `Now playing: ${s.name} - ${s.artist}`; } this.emit("stateChange"); @@ -1630,10 +1637,9 @@ export class BotInstance extends EventEmitter { if (wasIdle) { this.queue.playAt(insertedAt); this.player.resetFailures(); - const ok = await this.resolveAndPlay(this.queue.current()!); + const failure = await startTrackForCommand(this, this.queue.current()); this.emit("stateChange"); - if (!ok) return `Cannot play: ${s.name}`; - return `Now playing: ${s.name} - ${s.artist}`; + return failure ?? `Now playing: ${s.name} - ${s.artist}`; } this.emit("stateChange"); @@ -1892,10 +1898,10 @@ export class BotInstance extends EventEmitter { this.queue.add(this.withRequester({ ...song, platform: provider.platform }, requesterName)); } const first = this.queue.play(); - if (first) await this.resolveAndPlay(first); + const failure = await startTrackForCommand(this, first); this.sweepLocalAudio("queue_replaced"); this.emit("stateChange"); - return `Loaded ${songs.length} songs. Now playing: ${first?.name ?? "unknown"}`; + return failure ?? `Loaded ${songs.length} songs. Now playing: ${first?.name ?? "unknown"}`; } private async cmdAlbum(cmd: ParsedCommand, requesterName?: string): Promise { @@ -1929,10 +1935,10 @@ export class BotInstance extends EventEmitter { this.queue.add(this.withRequester({ ...song, platform: provider.platform }, requesterName)); } const first = this.queue.play(); - if (first) await this.resolveAndPlay(first); + const failure = await startTrackForCommand(this, first); this.sweepLocalAudio("queue_replaced"); this.emit("stateChange"); - return `Loaded ${songs.length} songs. Now playing: ${first?.name ?? "unknown"}`; + return failure ?? `Loaded ${songs.length} songs. Now playing: ${first?.name ?? "unknown"}`; } private async cmdFm(cmd: ParsedCommand, requesterName?: string): Promise { @@ -1970,11 +1976,11 @@ export class BotInstance extends EventEmitter { this.player.resetFailures(); const first = this.queue.play(); - if (first) await this.resolveAndPlay(first); + const failure = await startTrackForCommand(this, first); this.sweepLocalAudio("queue_replaced"); this.emit("stateChange"); const label = provider.platform === "qq" ? "QQ Radar FM" : "Personal FM"; - return `${label} started: ${first?.name ?? "unknown"} - ${first?.artist ?? ""}`; + return failure ?? `${label} started: ${first?.name ?? "unknown"} - ${first?.artist ?? ""}`; } private async cmdArtist(cmd: ParsedCommand, requesterName?: string): Promise { @@ -2004,10 +2010,10 @@ export class BotInstance extends EventEmitter { this.player.resetFailures(); const first = this.queue.play(); - if (first) await this.resolveAndPlay(first); + const failure = await startTrackForCommand(this, first); this.sweepLocalAudio("queue_replaced"); this.emit("stateChange"); - return `Artist mode: ${cmd.args} — ${filtered.length} songs loaded. Now playing: ${first?.name ?? "unknown"}`; + return failure ?? `Artist mode: ${cmd.args} — ${filtered.length} songs loaded. Now playing: ${first?.name ?? "unknown"}`; } private async refillFm(): Promise { diff --git a/src/web/api/player.ts b/src/web/api/player.ts index ae42ac7..fe0889b 100644 --- a/src/web/api/player.ts +++ b/src/web/api/player.ts @@ -210,10 +210,7 @@ export function createPlayerRouter( const resolveFmProvider = () => fmProvider(scope, bot, platform); const message = await bot.startFm(resolveFmProvider(), requesterName(req), resolveFmProvider); res.json({ - ok: - !message.startsWith("No FM songs") && - !message.includes("not available") && - !message.includes("not connected"), + ok: message.startsWith("Personal FM started") || message.startsWith("QQ Radar FM started"), message, }); } catch (err) {