Skip to content

Commit

Permalink
feat(player): option initialVolume can be function
Browse files Browse the repository at this point in the history
  • Loading branch information
larsrickert committed Jun 18, 2022
1 parent 992494c commit 2b3b9b2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
21 changes: 14 additions & 7 deletions src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { shuffle, validateVolume } from "./utils/player";
export class Player extends TypedEmitter<PlayerEvents> {
private readonly audioPlayer = createAudioPlayer();
private queue: Track[] = [];
private volume = 100;
private volume: number | undefined;
private audioResource: AudioResource<AudioPlayerMetadata> | undefined;
private repeatMode = PlayerRepeatMode.NONE;

Expand All @@ -40,10 +40,6 @@ export class Player extends TypedEmitter<PlayerEvents> {
) {
super();

if (options.initialVolume && validateVolume(options.initialVolume)) {
this.volume = options.initialVolume;
}

this.audioPlayer.on<"stateChange">(
"stateChange",
async (oldState, newState) => {
Expand Down Expand Up @@ -191,7 +187,18 @@ export class Player extends TypedEmitter<PlayerEvents> {
metadata,
});

this.audioResource.volume?.setVolume(this.volume / 100);
// set player volume
if (this.volume != null) {
this.setVolume(this.volume);
} else {
const initialVolume =
typeof this.options.initialVolume === "function"
? await this.options.initialVolume(options.channel.guildId)
: this.options.initialVolume;

this.setVolume(initialVolume ?? 100);
}

this.join(options.channel);
this.audioPlayer.play(this.audioResource);

Expand Down Expand Up @@ -303,7 +310,7 @@ export class Player extends TypedEmitter<PlayerEvents> {

/** Gets the current player volume. */
getVolume(): number {
return this.volume;
return this.volume ?? 100;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/types/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ export interface PlayerOptions {
* @default true
*/
inlineVolume?: boolean;
/** Initial player volume for all tracks between 0 and 200. */
initialVolume?: number;
/**
* Initial player volume for all tracks between 0 and 200. Can also be an (async) function that returns the volume.
*/
initialVolume?: number | ((guildId: string) => number | Promise<number>);
/**
* Path to the folder where local files should be playable from. Must be set if local files should be playable.
* For security reasons files outside this directory are refused to play.
Expand Down

0 comments on commit 2b3b9b2

Please sign in to comment.