mirror of
https://github.com/BluemediaDev/muse.git
synced 2025-06-27 09:12:43 +02:00
Refactor player service to use getGuildSettings instead of passing in the config.
This commit is contained in:
parent
ba3f1d60c3
commit
825c9a0c53
1 changed files with 12 additions and 11 deletions
|
@ -20,7 +20,7 @@ import FileCacheProvider from './file-cache.js';
|
||||||
import debug from '../utils/debug.js';
|
import debug from '../utils/debug.js';
|
||||||
import {getGuildSettings} from '../utils/get-guild-settings.js';
|
import {getGuildSettings} from '../utils/get-guild-settings.js';
|
||||||
import {buildPlayingMessageEmbed} from '../utils/build-embed.js';
|
import {buildPlayingMessageEmbed} from '../utils/build-embed.js';
|
||||||
import Config from './config.js';
|
import {Setting} from '@prisma/client';
|
||||||
|
|
||||||
export enum MediaSource {
|
export enum MediaSource {
|
||||||
Youtube,
|
Youtube,
|
||||||
|
@ -84,12 +84,10 @@ export default class {
|
||||||
private disconnectTimer: NodeJS.Timeout | null = null;
|
private disconnectTimer: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
private readonly channelToSpeakingUsers: Map<string, Set<string>> = new Map();
|
private readonly channelToSpeakingUsers: Map<string, Set<string>> = new Map();
|
||||||
private readonly config: Config;
|
|
||||||
|
|
||||||
constructor(fileCache: FileCacheProvider, guildId: string, config: Config) {
|
constructor(fileCache: FileCacheProvider, guildId: string) {
|
||||||
this.fileCache = fileCache;
|
this.fileCache = fileCache;
|
||||||
this.guildId = guildId;
|
this.guildId = guildId;
|
||||||
this.config = config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async connect(channel: VoiceChannel): Promise<void> {
|
async connect(channel: VoiceChannel): Promise<void> {
|
||||||
|
@ -105,6 +103,8 @@ export default class {
|
||||||
adapterCreator: channel.guild.voiceAdapterCreator as DiscordGatewayAdapterCreator,
|
adapterCreator: channel.guild.voiceAdapterCreator as DiscordGatewayAdapterCreator,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const guildSettings = await getGuildSettings(this.guildId);
|
||||||
|
|
||||||
// Workaround to disable keepAlive
|
// Workaround to disable keepAlive
|
||||||
this.voiceConnection.on('stateChange', (oldState, newState) => {
|
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 */
|
/* 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;
|
this.currentChannel = channel;
|
||||||
if (newState.status === VoiceConnectionStatus.Ready) {
|
if (newState.status === VoiceConnectionStatus.Ready) {
|
||||||
this.registerVoiceActivityListener();
|
this.registerVoiceActivityListener(guildSettings);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -311,8 +311,9 @@ export default class {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
registerVoiceActivityListener(): void {
|
registerVoiceActivityListener(guildSettings: Setting) {
|
||||||
if (!this.config.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK || !this.voiceConnection) {
|
const {turnDownVolumeWhenPeopleSpeak, turnDownVolumeWhenPeopleSpeakTarget} = guildSettings;
|
||||||
|
if (!turnDownVolumeWhenPeopleSpeak || !this.voiceConnection) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -332,7 +333,7 @@ export default class {
|
||||||
this.channelToSpeakingUsers.get(channelId)?.add(member.id);
|
this.channelToSpeakingUsers.get(channelId)?.add(member.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.suppressVoiceWhenPeopleAreSpeaking();
|
this.suppressVoiceWhenPeopleAreSpeaking(turnDownVolumeWhenPeopleSpeakTarget);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.voiceConnection.receiver.speaking.on('end', (userId: string) => {
|
this.voiceConnection.receiver.speaking.on('end', (userId: string) => {
|
||||||
|
@ -350,18 +351,18 @@ export default class {
|
||||||
this.channelToSpeakingUsers.get(channelId)?.delete(member.id);
|
this.channelToSpeakingUsers.get(channelId)?.delete(member.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.suppressVoiceWhenPeopleAreSpeaking();
|
this.suppressVoiceWhenPeopleAreSpeaking(turnDownVolumeWhenPeopleSpeakTarget);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
suppressVoiceWhenPeopleAreSpeaking(): void {
|
suppressVoiceWhenPeopleAreSpeaking(turnDownVolumeWhenPeopleSpeakTarget: number): void {
|
||||||
if (!this.currentChannel) {
|
if (!this.currentChannel) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const speakingUsers = this.channelToSpeakingUsers.get(this.currentChannel.id);
|
const speakingUsers = this.channelToSpeakingUsers.get(this.currentChannel.id);
|
||||||
if (speakingUsers && speakingUsers.size > 0) {
|
if (speakingUsers && speakingUsers.size > 0) {
|
||||||
this.setVolume(this.config.TURN_DOWN_VOLUME_WHEN_PEOPLE_SPEAK_TARGET);
|
this.setVolume(turnDownVolumeWhenPeopleSpeakTarget);
|
||||||
} else {
|
} else {
|
||||||
this.setVolume(this.defaultVolume);
|
this.setVolume(this.defaultVolume);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue