From d41ac09fa1dc7e5dc290a8ed4b2972a591a41976 Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Wed, 7 Feb 2024 13:34:23 -0500 Subject: [PATCH] Better async set for default volume and lint fixes * Set default volume on connect so getVolume does not need to be async * Fix missing Volume command removed by accident * Various lint fixes --- src/events/guild-create.ts | 1 - src/inversify.config.ts | 1 + src/managers/player.ts | 1 - src/services/player.ts | 30 +++++++++++++++--------------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/events/guild-create.ts b/src/events/guild-create.ts index 27ca3b9db..5cf7a55f4 100644 --- a/src/events/guild-create.ts +++ b/src/events/guild-create.ts @@ -7,7 +7,6 @@ import {REST} from '@discordjs/rest'; import registerCommandsOnGuild from '../utils/register-commands-on-guild.js'; export default async (guild: Guild): Promise => { - const config = container.get(TYPES.Config); // Setup slash commands diff --git a/src/inversify.config.ts b/src/inversify.config.ts index c535a1345..a02a0a192 100644 --- a/src/inversify.config.ts +++ b/src/inversify.config.ts @@ -86,6 +86,7 @@ container.bind(TYPES.Services.SpotifyAPI).to(SpotifyAPI).inSingleton Skip, Stop, Unskip, + Volume, ].forEach(command => { container.bind(TYPES.Command).to(command).inSingletonScope(); }); diff --git a/src/managers/player.ts b/src/managers/player.ts index f9ab26e91..420cf4889 100644 --- a/src/managers/player.ts +++ b/src/managers/player.ts @@ -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 { diff --git a/src/services/player.ts b/src/services/player.ts index da9ac1726..4533d9a0e 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -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, @@ -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 = ''; @@ -85,6 +85,8 @@ export default class { } async connect(channel: VoiceChannel): Promise { + this.volume = await getGuildSetting(this.guildId, GUILD_SETTINGS_MAP.DEFAULT_VOLUME); + this.voiceConnection = joinVoiceChannel({ channelId: channel.id, guildId: channel.guild.id, @@ -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); @@ -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(); @@ -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 { - if (this.volume !== null) { - return this.volume; - } - - this.volume = await getGuildSetting(this.guildId, GUILD_SETTINGS_MAP.DEFAULT_VOLUME); - + getVolume(): number { return this.volume; } @@ -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); + } }