Skip to content

Commit

Permalink
Persist volume level on player
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Feb 2, 2024
1 parent 012bc2f commit 19b3e03
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/commands/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class implements Command {
const player = this.playerManager.get(interaction.guild!.id);

if (!player.canControlVolume()) {
throw new Error('Ope, sorry but I\'m not configured to allow volume control! You can enable it by starting the bot with env VOLUME_CONTROL=true');
throw new Error('sorry but I\'m not configured to allow volume control! You can enable it by starting the bot with env VOLUME_CONTROL=true');
}

const currentSong = player.getCurrent();
Expand Down
7 changes: 5 additions & 2 deletions src/services/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default class {
private queuePosition = 0;
private audioPlayer: AudioPlayer | null = null;
private audioResource: AudioResource | null = null;
private volume = 100;
private nowPlaying: QueuedSong | null = null;
private playPositionInterval: NodeJS.Timeout | undefined;
private lastSongURL = '';
Expand Down Expand Up @@ -406,15 +407,16 @@ export default class {
setVolume(level: number): void {
// Level should be a number between 0 and 100 = 0% => 100%
// Spotify expects a float between 0 and 1 to represent this percentage
this.audioResource?.volume?.setVolume(level / 100);
this.volume = level;
this.audioResource?.volume?.setVolume(this.volume / 100);
}

getVolume(): number | undefined {
if (!this.volumeControl || this.audioResource === null || this.audioResource.volume === undefined) {
return undefined;
}

return this.audioResource.volume.volume * 100;
return this.volume;
}

canControlVolume(): boolean {
Expand Down Expand Up @@ -626,6 +628,7 @@ export default class {
private playAudioPlayerResource(resource: AudioResource) {
if (this.audioPlayer !== null) {
this.audioResource = resource;
this.audioResource?.volume?.setVolume(this.volume / 100);
this.audioPlayer.play(resource);
}
}
Expand Down

0 comments on commit 19b3e03

Please sign in to comment.