mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-03 03:12:51 +08:00
fix(teamspeak): resume only on arrivals in the bot channel
This commit is contained in:
1 parent
e5470b6b78
commit
d130652279
4 files changed
+37
-38
No files matched your search
+2
-16
@@ -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,
|
||||
|
||||
@@ -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<any[]>();
|
||||
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<any[]>(), newer = deferred<any[]>();
|
||||
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;
|
||||
|
||||
@@ -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<string, any>;
|
||||
|
||||
+9
-20
@@ -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user