From d130652279b13ee7edb815c6a9cd96c2bf875674 Mon Sep 17 00:00:00 2001 From: TIANYAO ZHANG <88520881+ZHANGTIANYAO1@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:25:07 +0800 Subject: [PATCH] fix(teamspeak): resume only on arrivals in the bot channel --- src/bot/auto-pause.ts | 18 ++---------------- src/bot/instance-recovery.test.ts | 27 +++++++++++++++++++++++++-- src/bot/instance.test.ts | 1 + src/bot/instance.ts | 29 +++++++++-------------------- 4 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/bot/auto-pause.ts b/src/bot/auto-pause.ts index 3fd01d6..8033c23 100644 --- a/src/bot/auto-pause.ts +++ b/src/bot/auto-pause.ts @@ -42,22 +42,8 @@ export function decideOccupancyAction( } /** - * Whether a client-presence push event (a `clientEnter`) should trigger an - * auto-resume, WITHOUT consulting a clientlist query. - * - * Why event-driven: the full-client `clientlist`/`channellist` commands time - * out whenever ≥2 clients are connected to the server (a library limitation) — - * which is exactly the moment a listener returns. So occupancy cannot be - * re-queried to confirm the return; we must act on the push event itself. - * This is sound because the bot only ever auto-pauses while it is alone on the - * server (the sole state in which the occupancy query succeeds and pause - * fires). Therefore, while `autoPaused` is true, the only way occupancy can - * return is a fresh connection — delivered reliably as `clientEnter`. - * - * Gating on `autoPaused` (not merely "paused") guarantees we never revive a - * track the user paused by hand, and makes the bot's own `clientEnter` at - * connect a no-op (autoPaused is cleared to false on connect). This predicate - * NEVER pauses — pause stays on the authoritative clientlist path. + * Whether a confirmed arrival in the bot's channel should resume playback. + * An arrival does not resume a pause requested manually. */ export function shouldResumeOnReturn( autoPaused: boolean, diff --git a/src/bot/instance-recovery.test.ts b/src/bot/instance-recovery.test.ts index 630b2c2..97dd94f 100644 --- a/src/bot/instance-recovery.test.ts +++ b/src/bot/instance-recovery.test.ts @@ -40,6 +40,7 @@ function makeBot() { const tsClient = new EventEmitter() as any; tsClient.getClientsInChannel = vi.fn(async () => [{ id: 1 }, { id: 2 }]); tsClient.getClientId = () => 1; + tsClient.getChannelId = () => 3n; tsClient.sendVoiceData = vi.fn(); tsClient.connect = vi.fn(async () => { tsClient.emit("connected"); }); tsClient.disconnect = vi.fn(() => { tsClient.emit("disconnected"); }); @@ -330,7 +331,7 @@ describe("occupancy responses belong to the current request and connection", () const older = deferred(); bot.tsClient.getClientsInChannel.mockReturnValueOnce(older.promise).mockResolvedValueOnce([]); const first = bot.refreshOccupancy(); - bot.tsClient.emit(event, { id: 2, targetChannelID: 3n }); + bot.tsClient.emit(event, { id: 2, channelID: 3n, targetChannelID: 3n }); await flush(); older.resolve([{ id: 1 }]); await first; @@ -339,6 +340,28 @@ describe("occupancy responses belong to the current request and connection", () expect(bot.idleTimer).toBeNull(); }); + it("does not resume for a client entering another channel", async () => { + const bot = makeBot(); + bot.player.state = "paused"; + bot.autoPaused = true; + bot.tsClient.getClientsInChannel.mockResolvedValue([]); + bot.tsClient.emit("clientEnter", { id: 2, channelID: 4n }); + await flush(); + expect(bot.player.getState()).toBe("paused"); + expect(bot.autoPaused).toBe(true); + }); + + it("resumes when a listener moves into the channel and the occupancy query fails", async () => { + const bot = makeBot(); + bot.player.state = "paused"; + bot.autoPaused = true; + bot.tsClient.getClientsInChannel.mockResolvedValue([]); + bot.tsClient.emit("clientMoved", { id: 2, targetChannelID: 3n }); + await flush(); + expect(bot.player.getState()).toBe("playing"); + expect(bot.autoPaused).toBe(false); + }); + it("a listener's return cannot be undone by an earlier alone response", async () => { const bot = makeBot(); bot.player.state = "paused"; @@ -346,7 +369,7 @@ describe("occupancy responses belong to the current request and connection", () const older = deferred(), newer = deferred(); bot.tsClient.getClientsInChannel.mockReturnValueOnce(older.promise).mockReturnValueOnce(newer.promise); const first = bot.refreshOccupancy(); - bot.tsClient.emit("clientEnter"); + bot.tsClient.emit("clientEnter", { id: 2, channelID: 3n }); expect(bot.player.getState()).toBe("playing"); older.resolve([{ id: 1 }]); await first; diff --git a/src/bot/instance.test.ts b/src/bot/instance.test.ts index 5e5df91..e991ff1 100644 --- a/src/bot/instance.test.ts +++ b/src/bot/instance.test.ts @@ -2232,6 +2232,7 @@ describe("BotInstance — OpenProtocol & command routing", () => { sweepLocalAudio: vi.fn(), emit: vi.fn(), refreshOccupancy: vi.fn(async () => {}), + _resumeIfReturning: vi.fn(), profileManager: { onChannelMoved: vi.fn(async () => {}) }, logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }, } as Record; diff --git a/src/bot/instance.ts b/src/bot/instance.ts index 5dcff88..233fe0f 100755 --- a/src/bot/instance.ts +++ b/src/bot/instance.ts @@ -558,12 +558,10 @@ export class BotInstance extends EventEmitter { // React near-instantly to channel membership changes. The 30s idle // poller remains the fallback if any of these events are missed. // - // clientEnter additionally arms auto-RESUME directly from the event, - // because the occupancy query (clientlist) times out whenever another - // client is present — i.e. exactly when a listener returns — so it cannot - // be used to confirm the return. See _resumeIfReturning(). - this.tsClient.on("clientEnter", () => { - this._resumeIfReturning(); + // Enter and move events can confirm a listener's arrival even if the + // occupancy query fails. Only arrivals in this channel can resume playback. + this.tsClient.on("clientEnter", (event: { id: number; channelID: bigint }) => { + this._resumeIfReturning(event.id, event.channelID); void this.refreshOccupancy(); }); this.tsClient.on("clientLeave", (event: { id: number }) => { @@ -583,6 +581,7 @@ export class BotInstance extends EventEmitter { this.openProtocol?.scheduleAnnounce(); } else { this.voiceDucking.removeSpeaker(event.id); + this._resumeIfReturning(event.id, event.targetChannelID); } void this.refreshOccupancy(); }); @@ -628,20 +627,10 @@ export class BotInstance extends EventEmitter { } } - /** - * Resume playback when a listener returns after an auto-pause, driven by the - * clientEnter push event rather than a (timing-out) occupancy query. - * - * We only auto-pause while alone on the server, so `autoPaused` is a reliable - * "paused because empty" flag; any client appearing while it's set means a - * listener returned. Delegating to handleOccupancy(1) routes through - * decideOccupancyAction (resume iff autoPaused && paused) and also cancels the - * idle-disconnect timer. This path NEVER pauses — userCount is always > 0 — - * so a spurious or unrelated enter can only (harmlessly) resume, never stop - * playback. Pause remains exclusively on the authoritative clientlist path. - */ - private _resumeIfReturning(): void { - if (!this.connected) return; + /** Resume an automatic pause after a listener arrives in the bot's channel. */ + private _resumeIfReturning(listenerId: number, channelId: bigint): void { + if (!this.connected || listenerId <= 0 || listenerId === this.tsClient.getClientId() || + channelId === 0n || channelId !== this.tsClient.getChannelId()) return; if (shouldResumeOnReturn(this.autoPaused, this.player.getState())) { this.handleOccupancy(1); }