diff --git a/src/audio-manager.ts b/src/audio-manager.ts index 0271f35..756f397 100644 --- a/src/audio-manager.ts +++ b/src/audio-manager.ts @@ -89,7 +89,14 @@ export default class AudioManager { }); this.connection.subscribe(this.audioPlayer); - await entersState(this.connection, VoiceConnectionStatus.Ready, this.options.connectTimeoutMs); + try { + await entersState(this.connection, VoiceConnectionStatus.Ready, this.options.connectTimeoutMs); + } catch (error) { + this.connection.destroy(); + this.connection = undefined; + this.playbackState = 'stopped'; + throw error; + } this.playbackState = 'ready'; this.scheduleRenewal(); } diff --git a/tests/audio-manager.test.ts b/tests/audio-manager.test.ts index f874041..36f12e1 100644 --- a/tests/audio-manager.test.ts +++ b/tests/audio-manager.test.ts @@ -101,7 +101,7 @@ describe('AudioManager', () => { expect(mockAudioPlayer.play).toHaveBeenCalledWith(mockAudioResource); }); - it('keeps failed connection startup observable', async () => { + it('cleans up when connection startup fails', async () => { jest.useFakeTimers(); const manager = new AudioManager({ @@ -112,10 +112,10 @@ describe('AudioManager', () => { await expect(manager.connect()).rejects.toThrow('voice connection timeout'); - expect(manager.state).toBe('connecting'); - expect(manager.isConnected).toBe(true); + expect(manager.state).toBe('stopped'); + expect(manager.isConnected).toBe(false); expect(mockConnection.subscribe).toHaveBeenCalledWith(mockAudioPlayer); - expect(mockConnection.destroy).not.toHaveBeenCalled(); + expect(mockConnection.destroy).toHaveBeenCalledTimes(1); expect(jest.getTimerCount()).toBe(0); });