mirror of
https://github.com/BluemediaDev/muse.git
synced 2025-06-28 01:32:42 +02:00
Co-authored-by: Max Isom <hi@maxisom.me>
This commit is contained in:
parent
786e6fd8f5
commit
6baaffb730
8 changed files with 141 additions and 11 deletions
|
@ -8,7 +8,7 @@ import shuffle from 'array-shuffle';
|
|||
import {
|
||||
AudioPlayer,
|
||||
AudioPlayerState,
|
||||
AudioPlayerStatus,
|
||||
AudioPlayerStatus, AudioResource,
|
||||
createAudioPlayer,
|
||||
createAudioResource, DiscordGatewayAdapterCreator,
|
||||
joinVoiceChannel,
|
||||
|
@ -59,6 +59,8 @@ export interface PlayerEvents {
|
|||
|
||||
type YTDLVideoFormat = videoFormat & {loudnessDb?: number};
|
||||
|
||||
export const DEFAULT_VOLUME = 100;
|
||||
|
||||
export default class {
|
||||
public voiceConnection: VoiceConnection | null = null;
|
||||
public status = STATUS.PAUSED;
|
||||
|
@ -69,6 +71,9 @@ export default class {
|
|||
private queue: QueuedSong[] = [];
|
||||
private queuePosition = 0;
|
||||
private audioPlayer: AudioPlayer | null = null;
|
||||
private audioResource: AudioResource | null = null;
|
||||
private volume?: number;
|
||||
private defaultVolume: number = DEFAULT_VOLUME;
|
||||
private nowPlaying: QueuedSong | null = null;
|
||||
private playPositionInterval: NodeJS.Timeout | undefined;
|
||||
private lastSongURL = '';
|
||||
|
@ -83,6 +88,11 @@ export default class {
|
|||
}
|
||||
|
||||
async connect(channel: VoiceChannel): Promise<void> {
|
||||
// Always get freshest default volume setting value
|
||||
const settings = await getGuildSettings(this.guildId);
|
||||
const {defaultVolume = DEFAULT_VOLUME} = settings;
|
||||
this.defaultVolume = defaultVolume;
|
||||
|
||||
this.voiceConnection = joinVoiceChannel({
|
||||
channelId: channel.id,
|
||||
guildId: channel.guild.id,
|
||||
|
@ -120,6 +130,7 @@ export default class {
|
|||
|
||||
this.voiceConnection = null;
|
||||
this.audioPlayer = null;
|
||||
this.audioResource = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -155,9 +166,7 @@ export default class {
|
|||
},
|
||||
});
|
||||
this.voiceConnection.subscribe(this.audioPlayer);
|
||||
this.audioPlayer.play(createAudioResource(stream, {
|
||||
inputType: StreamType.WebmOpus,
|
||||
}));
|
||||
this.playAudioPlayerResource(this.createAudioStream(stream));
|
||||
this.attachListeners();
|
||||
this.startTrackingPosition(positionSeconds);
|
||||
|
||||
|
@ -220,11 +229,7 @@ export default class {
|
|||
},
|
||||
});
|
||||
this.voiceConnection.subscribe(this.audioPlayer);
|
||||
const resource = createAudioResource(stream, {
|
||||
inputType: StreamType.WebmOpus,
|
||||
});
|
||||
|
||||
this.audioPlayer.play(resource);
|
||||
this.playAudioPlayerResource(this.createAudioStream(stream));
|
||||
|
||||
this.attachListeners();
|
||||
|
||||
|
@ -408,6 +413,17 @@ export default class {
|
|||
return this.queue[this.queuePosition + to];
|
||||
}
|
||||
|
||||
setVolume(level: number): void {
|
||||
// Level should be a number between 0 and 100 = 0% => 100%
|
||||
this.volume = level;
|
||||
this.setAudioPlayerVolume(level);
|
||||
}
|
||||
|
||||
getVolume(): number {
|
||||
// Only use default volume if player volume is not already set (in the event of a reconnect we shouldn't reset)
|
||||
return this.volume ?? this.defaultVolume;
|
||||
}
|
||||
|
||||
private getHashForCache(url: string): string {
|
||||
return hasha(url);
|
||||
}
|
||||
|
@ -610,4 +626,24 @@ export default class {
|
|||
resolve(returnedStream);
|
||||
});
|
||||
}
|
||||
|
||||
private createAudioStream(stream: Readable) {
|
||||
return createAudioResource(stream, {
|
||||
inputType: StreamType.WebmOpus,
|
||||
inlineVolume: true,
|
||||
});
|
||||
}
|
||||
|
||||
private playAudioPlayerResource(resource: AudioResource) {
|
||||
if (this.audioPlayer !== null) {
|
||||
this.audioResource = resource;
|
||||
this.setAudioPlayerVolume();
|
||||
this.audioPlayer.play(this.audioResource);
|
||||
}
|
||||
}
|
||||
|
||||
private setAudioPlayerVolume(level?: number) {
|
||||
// Audio resource expects a float between 0 and 1 to represent level percentage
|
||||
this.audioResource?.volume?.setVolume((level ?? this.getVolume()) / 100);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue