Skip to content

Commit

Permalink
Better async set for default volume and lint fixes
Browse files Browse the repository at this point in the history
* Set default volume on connect so getVolume does not need to be async
* Fix missing Volume command removed by accident
* Various lint fixes
  • Loading branch information
FoxxMD committed Feb 7, 2024
1 parent 3e0c831 commit d41ac09
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/events/guild-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {REST} from '@discordjs/rest';
import registerCommandsOnGuild from '../utils/register-commands-on-guild.js';

export default async (guild: Guild): Promise<void> => {

const config = container.get<Config>(TYPES.Config);

// Setup slash commands
Expand Down
1 change: 1 addition & 0 deletions src/inversify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ container.bind<SpotifyAPI>(TYPES.Services.SpotifyAPI).to(SpotifyAPI).inSingleton
Skip,
Stop,
Unskip,
Volume,
].forEach(command => {
container.bind<Command>(TYPES.Command).to(command).inSingletonScope();
});
Expand Down
1 change: 0 additions & 1 deletion src/managers/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {inject, injectable} from 'inversify';
import {TYPES} from '../types.js';
import Player from '../services/player.js';
import FileCacheProvider from '../services/file-cache.js';
import Config from '../services/config';

@injectable()
export default class {
Expand Down
30 changes: 15 additions & 15 deletions src/services/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import FileCacheProvider from './file-cache.js';
import debug from '../utils/debug.js';
import {getGuildSetting, getGuildSettings} from '../utils/get-guild-settings.js';
import {GUILD_SETTINGS_MAP} from "./config";
import {GUILD_SETTINGS_DEFAULTS, GUILD_SETTINGS_MAP} from './config';

export enum MediaSource {
Youtube,
Expand Down Expand Up @@ -70,7 +70,7 @@ export default class {
private queuePosition = 0;
private audioPlayer: AudioPlayer | null = null;
private audioResource: AudioResource | null = null;
private volume: number | null = null;
private volume: number = GUILD_SETTINGS_DEFAULTS.defaultVolume;
private nowPlaying: QueuedSong | null = null;
private playPositionInterval: NodeJS.Timeout | undefined;
private lastSongURL = '';
Expand All @@ -85,6 +85,8 @@ export default class {
}

async connect(channel: VoiceChannel): Promise<void> {
this.volume = await getGuildSetting<number>(this.guildId, GUILD_SETTINGS_MAP.DEFAULT_VOLUME);

this.voiceConnection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
Expand Down Expand Up @@ -156,7 +158,7 @@ export default class {
},
});
this.voiceConnection.subscribe(this.audioPlayer);
await this.playAudioPlayerResource(this.createAudioStream(stream));
this.playAudioPlayerResource(this.createAudioStream(stream));
this.attachListeners();
this.startTrackingPosition(positionSeconds);

Expand Down Expand Up @@ -219,7 +221,7 @@ export default class {
},
});
this.voiceConnection.subscribe(this.audioPlayer);
await this.playAudioPlayerResource(this.createAudioStream(stream));
this.playAudioPlayerResource(this.createAudioStream(stream));

this.attachListeners();

Expand Down Expand Up @@ -405,18 +407,11 @@ 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.volume = level;
this.audioResource?.volume?.setVolume(this.volume / 100);
this.setAudioPlayerVolume();
}

async getVolume(): Promise<number> {
if (this.volume !== null) {
return this.volume;
}

this.volume = await getGuildSetting<number>(this.guildId, GUILD_SETTINGS_MAP.DEFAULT_VOLUME);

getVolume(): number {
return this.volume;
}

Expand Down Expand Up @@ -622,11 +617,16 @@ export default class {
});
}

private async playAudioPlayerResource(resource: AudioResource) {
private playAudioPlayerResource(resource: AudioResource) {
if (this.audioPlayer !== null) {
this.audioResource = resource;
this.audioResource?.volume?.setVolume(await this.getVolume() / 100);
this.setAudioPlayerVolume();
this.audioPlayer.play(resource);
}
}

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);
}
}

0 comments on commit d41ac09

Please sign in to comment.