Skip to content

Commit

Permalink
refactor: rename event disconnect to destroyed
Browse files Browse the repository at this point in the history
BREAKING CHANGE: rename event `disconnect` to `destroyed`
  • Loading branch information
larsrickert committed May 5, 2022
1 parent 9e7ab69 commit 2975507
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
22 changes: 10 additions & 12 deletions src/includes/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,29 @@ export class Player extends TypedEmitter<PlayerEvents> {
adapterCreator: channel.guild.voiceAdapterCreator,
});

// subscribing to audioPlayer allows playing music
const subscription = connection.subscribe(this.audioPlayer);

connection.removeAllListeners();

const closePlayer = () => {
this.audioPlayer.stop();
subscription?.unsubscribe();
this.audioResource = undefined;
this.emit("disconnect");
};

connection
.on(VoiceConnectionStatus.Disconnected, async () => {
try {
await Promise.race([
entersState(connection, VoiceConnectionStatus.Signalling, 5_000),
entersState(connection, VoiceConnectionStatus.Connecting, 5_000),
]);
// Seems to be reconnecting to a new channel - ignore disconnect
// seems to be reconnecting to a new channel - ignore disconnect
} catch (error) {
// Seems to be a real disconnect which SHOULDN'T be recovered from
closePlayer();
// seems to be a real disconnect
connection.destroy();
}
})
.on(VoiceConnectionStatus.Destroyed, () => closePlayer());
.on(VoiceConnectionStatus.Destroyed, () => {
this.audioPlayer.stop();
subscription?.unsubscribe();
this.audioResource = undefined;
this.emit("destroyed");
});

return connection;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SearchType, Track } from "./tracks";
export interface PlayerEvents {
trackStart: (track: Track) => void;
trackEnd: () => void;
disconnect: () => void;
destroyed: () => void;
queueEnd: () => void;
}

Expand Down

0 comments on commit 2975507

Please sign in to comment.