Tie playback to the listener and keep the panel's view live
Four things people hit while using the panel: - The bot could be sent into a channel the requester was not in, and playback could be started from nowhere. Playback now follows the listener (REQUIRE_LISTENER, on by default), and the voice row shows where you and the bot are instead of offering a free channel picker. - Voice presence only refreshed on reload, because the SDK updates channel participants without emitting an event. The socket now watches that view and pushes changes. - The bot left the channel whenever the queue ran dry. It now leaves only after the last person does, EMPTY_TIMEOUT_SECONDS later (120 by default), and stays put while anyone is still listening. - A search that yielded nothing said nothing: yt-dlp can exit 0 with an empty result, so that case now reports the reason (or "nothing found"), and searches are logged with their result count. The queue moved under the player so search owns the left column, and elapsed time no longer renders as "LIVE" — formatDuration treated 0 as a live stream, which also affected the chat's progress bar. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
f22b08b350
commit
3e53f34374
+26
-20
@@ -77,7 +77,7 @@ export class GuildPlayer extends EventEmitter<GuildPlayerEvents> {
|
||||
private seekOffset = 0;
|
||||
/** Set while we tear playback down ourselves, so the resulting `finish` is ignored. */
|
||||
private expectingStop = false;
|
||||
private idleTimer: NodeJS.Timeout | null = null;
|
||||
private leaveTimer: NodeJS.Timeout | null = null;
|
||||
private ticker: NodeJS.Timeout | null = null;
|
||||
private readonly log;
|
||||
|
||||
@@ -182,7 +182,7 @@ export class GuildPlayer extends EventEmitter<GuildPlayerEvents> {
|
||||
});
|
||||
connection.on("userleave", () => this.checkEmptyChannel());
|
||||
connection.on("userLeave", () => this.checkEmptyChannel());
|
||||
connection.on("userJoin", () => this.clearIdleTimer());
|
||||
connection.on("userJoin", () => this.cancelLeaveTimer());
|
||||
|
||||
const media = new MediaPlayer(true);
|
||||
// revoice's #cleanUp() dereferences this.fProc unconditionally, so a second
|
||||
@@ -211,11 +211,12 @@ export class GuildPlayer extends EventEmitter<GuildPlayerEvents> {
|
||||
await connection.play(media);
|
||||
|
||||
this.setStatus("idle");
|
||||
this.checkEmptyChannel();
|
||||
this.log.info({ channelId }, "voice connection established");
|
||||
}
|
||||
|
||||
async leaveVoice(): Promise<void> {
|
||||
this.clearIdleTimer();
|
||||
this.cancelLeaveTimer();
|
||||
this.stopTicker();
|
||||
this.teardownPlayback();
|
||||
this.current = null;
|
||||
@@ -242,8 +243,11 @@ export class GuildPlayer extends EventEmitter<GuildPlayerEvents> {
|
||||
|
||||
private checkEmptyChannel(): void {
|
||||
if (!this.connection) return;
|
||||
if (this.connection.getUsers().length > 0) return;
|
||||
this.startIdleTimer("В канале никого не осталось");
|
||||
if (this.connection.getUsers().length > 0) {
|
||||
this.cancelLeaveTimer();
|
||||
return;
|
||||
}
|
||||
this.startLeaveTimer();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------- playback ---
|
||||
@@ -272,7 +276,7 @@ export class GuildPlayer extends EventEmitter<GuildPlayerEvents> {
|
||||
|
||||
private async startPlayback(track: Track, seekSeconds = 0): Promise<void> {
|
||||
const media = this.assertReady();
|
||||
this.clearIdleTimer();
|
||||
this.cancelLeaveTimer();
|
||||
this.teardownPlayback();
|
||||
|
||||
this.current = track;
|
||||
@@ -361,7 +365,6 @@ export class GuildPlayer extends EventEmitter<GuildPlayerEvents> {
|
||||
this.setStatus("idle");
|
||||
this.publish();
|
||||
if (finished) this.notify("⏹️ Очередь закончилась.");
|
||||
this.startIdleTimer();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -385,7 +388,6 @@ export class GuildPlayer extends EventEmitter<GuildPlayerEvents> {
|
||||
this.stopTicker();
|
||||
this.setStatus("idle");
|
||||
this.publish();
|
||||
this.startIdleTimer();
|
||||
}
|
||||
|
||||
pause(): void {
|
||||
@@ -501,21 +503,25 @@ export class GuildPlayer extends EventEmitter<GuildPlayerEvents> {
|
||||
this.ticker = null;
|
||||
}
|
||||
|
||||
private clearIdleTimer(): void {
|
||||
if (!this.idleTimer) return;
|
||||
clearTimeout(this.idleTimer);
|
||||
this.idleTimer = null;
|
||||
private cancelLeaveTimer(): void {
|
||||
if (!this.leaveTimer) return;
|
||||
clearTimeout(this.leaveTimer);
|
||||
this.leaveTimer = null;
|
||||
}
|
||||
|
||||
private startIdleTimer(reason?: string): void {
|
||||
this.clearIdleTimer();
|
||||
if (config.IDLE_TIMEOUT_SECONDS <= 0 || !this.connection) return;
|
||||
this.idleTimer = setTimeout(() => {
|
||||
if (this.current) return;
|
||||
this.notify(`👋 ${reason ?? "Нет активности"}, выхожу из голосового канала.`);
|
||||
/**
|
||||
* Leaving is tied to the channel being empty, never to an idle queue: the bot
|
||||
* stays put with people around, waiting for the next request.
|
||||
*/
|
||||
private startLeaveTimer(): void {
|
||||
this.cancelLeaveTimer();
|
||||
if (config.EMPTY_TIMEOUT_SECONDS <= 0 || !this.connection) return;
|
||||
this.leaveTimer = setTimeout(() => {
|
||||
if (this.connection && this.connection.getUsers().length > 0) return;
|
||||
this.notify("👋 В канале никого не осталось, выхожу.");
|
||||
void this.leaveVoice();
|
||||
}, config.IDLE_TIMEOUT_SECONDS * 1000);
|
||||
this.idleTimer.unref?.();
|
||||
}, config.EMPTY_TIMEOUT_SECONDS * 1000);
|
||||
this.leaveTimer.unref?.();
|
||||
}
|
||||
|
||||
async destroy(): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user