From 825c9a0c530d39b2b1c2f2f02904a866b7398001 Mon Sep 17 00:00:00 2001 From: Hazzajenko Date: Fri, 1 Nov 2024 17:43:39 +1100 Subject: [PATCH] Refactor player service to use getGuildSettings instead of passing in the config. --- src/services/player.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/services/player.ts b/src/services/player.ts index 00081ca..b833022 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -20,7 +20,7 @@ import FileCacheProvider from './file-cache.js'; import debug from '../utils/debug.js'; import {getGuildSettings} from '../utils/get-guild-settings.js'; import {buildPlayingMessageEmbed} from '../utils/build-embed.js'; -import Config from './config.js'; +import {Setting} from '@prisma/client'; export enum MediaSource { Youtube, @@ -84,12 +84,10 @@ export default class { private disconnectTimer: NodeJS.Timeout | null = null; private readonly channelToSpeakingUsers: Map> = new Map(); - private readonly config: Config; - constructor(fileCache: FileCacheProvider, guildId: string, config: Config) { + constructor(fileCache: FileCacheProvider, guildId: string) { this.fileCache = fileCache; this.guildId = guildId; - this.config = config; } async connect(channel: VoiceChannel): Promise { @@ -105,6 +103,8 @@ export default class { adapterCreator: channel.guild.voiceAdapterCreator as DiscordGatewayAdapterCreator, }); + const guildSettings = await getGuildSettings(this.guildId); + // Workaround to disable keepAlive this.voiceConnection.on('stateChange', (oldState, newState) => { /* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call */ @@ -122,7 +122,7 @@ export default class { this.currentChannel = channel; if (newState.status === VoiceConnectionStatus.Ready) { - this.registerVoiceActivityListener(); + this.registerVoiceActivityListener(guildSettings); } }); } @@ -311,8 +311,9 @@ export default class { } } - registerVoiceActivityListener(): void { - if (!this.config.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK || !this.voiceConnection) { + registerVoiceActivityListener(guildSettings: Setting) { + const {turnDownVolumeWhenPeopleSpeak, turnDownVolumeWhenPeopleSpeakTarget} = guildSettings; + if (!turnDownVolumeWhenPeopleSpeak || !this.voiceConnection) { return; } @@ -332,7 +333,7 @@ export default class { this.channelToSpeakingUsers.get(channelId)?.add(member.id); } - this.suppressVoiceWhenPeopleAreSpeaking(); + this.suppressVoiceWhenPeopleAreSpeaking(turnDownVolumeWhenPeopleSpeakTarget); }); this.voiceConnection.receiver.speaking.on('end', (userId: string) => { @@ -350,18 +351,18 @@ export default class { this.channelToSpeakingUsers.get(channelId)?.delete(member.id); } - this.suppressVoiceWhenPeopleAreSpeaking(); + this.suppressVoiceWhenPeopleAreSpeaking(turnDownVolumeWhenPeopleSpeakTarget); }); } - suppressVoiceWhenPeopleAreSpeaking(): void { + suppressVoiceWhenPeopleAreSpeaking(turnDownVolumeWhenPeopleSpeakTarget: number): void { if (!this.currentChannel) { return; } const speakingUsers = this.channelToSpeakingUsers.get(this.currentChannel.id); if (speakingUsers && speakingUsers.size > 0) { - this.setVolume(this.config.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK_TARGET); + this.setVolume(turnDownVolumeWhenPeopleSpeakTarget); } else { this.setVolume(this.defaultVolume); }