From 265251233aebcc4fa83c533c99d24999dee39c7c Mon Sep 17 00:00:00 2001 From: MythEclipse Date: Sat, 13 Jun 2026 15:03:21 +0700 Subject: [PATCH] chore(auto): task completed - unknown --- .../src/modules/voice-recording/player.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/services/discord-gateway/src/modules/voice-recording/player.ts b/services/discord-gateway/src/modules/voice-recording/player.ts index 58602dc..ac6bb02 100644 --- a/services/discord-gateway/src/modules/voice-recording/player.ts +++ b/services/discord-gateway/src/modules/voice-recording/player.ts @@ -19,6 +19,9 @@ export class DiscordPlayer { private owner: DiscordPlayerOwner = "none"; private resource: AudioResource | null = null; private musicVolume = 1; + private idleCallback: (() => void) | null = null; + /** Set before manual stop() calls to distinguish from natural track end. */ + private manualStop = false; constructor() { this.player = createAudioPlayer(); @@ -27,6 +30,19 @@ export class DiscordPlayer { logger.info("Audio player is now playing!"); }); + this.player.on(AudioPlayerStatus.Idle, () => { + // manualStop guards against both sync & async idle emission from stop() + if (this.manualStop || this.owner === "none") { + this.manualStop = false; + return; + } + // Natural track end — queue auto-advance + logger.info("Audio player finished — advancing queue"); + this.owner = "none"; + this.resource = null; + this.idleCallback?.(); + }); + this.player.on("error", (error) => { logger.error({ error: error.message }, "Audio player error"); this.owner = "none"; @@ -34,6 +50,11 @@ export class DiscordPlayer { }); } + /** Register a callback fired when the player goes idle after a natural track end. */ + public onIdle(cb: () => void): void { + this.idleCallback = cb; + } + public setConnection(connection: VoiceConnection) { this.connection = connection; this.connection.subscribe(this.player); @@ -64,6 +85,7 @@ export class DiscordPlayer { }); if (this.owner === owner) { + this.manualStop = true; this.player.stop(); } this.resource = resource; @@ -97,6 +119,7 @@ export class DiscordPlayer { public stop(owner?: DiscordPlayerOwner) { if (!this.canControl(owner)) return; + this.manualStop = true; this.player.stop(); this.owner = "none"; this.resource = null;