Configurable voice channel leave behavior (#514)

Co-authored-by: Max Isom <hi@maxisom.me>
This commit is contained in:
Johannes Vääräkangas 2022-02-12 04:05:02 +02:00 committed by GitHub
parent 8e5b3cfa43
commit 4dbb55a721
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 123 additions and 11 deletions

View file

@ -3,15 +3,23 @@ import container from '../inversify.config.js';
import {TYPES} from '../types.js';
import PlayerManager from '../managers/player.js';
import {getSizeWithoutBots} from '../utils/channels.js';
import {prisma} from '../utils/db.js';
export default (oldState: VoiceState, _: VoiceState): void => {
export default async (oldState: VoiceState, _: VoiceState): Promise<void> => {
const playerManager = container.get<PlayerManager>(TYPES.Managers.Player);
const player = playerManager.get(oldState.guild.id);
if (player.voiceConnection) {
const voiceChannel: VoiceChannel = oldState.guild.channels.cache.get(player.voiceConnection.joinConfig.channelId!) as VoiceChannel;
if (!voiceChannel || getSizeWithoutBots(voiceChannel) === 0) {
const settings = await prisma.setting.findUnique({where: {guildId: player.guildId}});
if (!settings) {
throw new Error('Could not find settings for guild');
}
const {leaveIfNoListeners} = settings;
if (!voiceChannel || (getSizeWithoutBots(voiceChannel) === 0 && leaveIfNoListeners)) {
player.disconnect();
}
}