mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-02 04:52:50 +08:00
feat(autopause): re-emit client enter/leave/move for instant pause/resume
This commit is contained in:
1 parent
4ba4b013b0
commit
8f5bb26b3a
2 files changed
+32
No files matched your search
@@ -159,6 +159,22 @@ export class BotInstance extends EventEmitter {
|
|||||||
this.autoPaused = false;
|
this.autoPaused = false;
|
||||||
this._startIdlePoller();
|
this._startIdlePoller();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// React near-instantly to channel membership changes. The 30s idle
|
||||||
|
// poller remains the fallback if any of these events are missed.
|
||||||
|
this.tsClient.on("clientEnter", () => void this.refreshOccupancy());
|
||||||
|
this.tsClient.on("clientLeave", () => void this.refreshOccupancy());
|
||||||
|
this.tsClient.on("clientMoved", () => void this.refreshOccupancy());
|
||||||
|
}
|
||||||
|
|
||||||
|
private async refreshOccupancy(): Promise<void> {
|
||||||
|
if (!this.connected) return;
|
||||||
|
try {
|
||||||
|
const clients = await this.tsClient.getClientsInChannel();
|
||||||
|
this.handleOccupancy(clients.length - 1);
|
||||||
|
} catch {
|
||||||
|
// ignore — the 30s poll is the fallback
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async connect(): Promise<void> {
|
async connect(): Promise<void> {
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import {
|
|||||||
type Identity,
|
type Identity,
|
||||||
type TextMessage,
|
type TextMessage,
|
||||||
type ClientInfo,
|
type ClientInfo,
|
||||||
|
type ClientLeftViewEvent,
|
||||||
|
type ClientMovedEvent,
|
||||||
type FileUploadInfo,
|
type FileUploadInfo,
|
||||||
} from "@honeybbq/teamspeak-client";
|
} from "@honeybbq/teamspeak-client";
|
||||||
import type { Logger } from "../logger.js";
|
import type { Logger } from "../logger.js";
|
||||||
@@ -221,6 +223,20 @@ export class TS3Client extends EventEmitter {
|
|||||||
{ nickname: info.nickname, id: info.id },
|
{ nickname: info.nickname, id: info.id },
|
||||||
"Client entered"
|
"Client entered"
|
||||||
);
|
);
|
||||||
|
this.emit("clientEnter", info);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.client.on("clientLeave", (ev: ClientLeftViewEvent) => {
|
||||||
|
this.logger.debug({ id: ev.id }, "Client left");
|
||||||
|
this.emit("clientLeave", ev);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.client.on("clientMoved", (ev: ClientMovedEvent) => {
|
||||||
|
this.logger.debug(
|
||||||
|
{ id: ev.id, targetChannelID: ev.targetChannelID.toString() },
|
||||||
|
"Client moved"
|
||||||
|
);
|
||||||
|
this.emit("clientMoved", ev);
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.client.connect();
|
await this.client.connect();
|
||||||
|
|||||||
Reference in new issue
Block a user