const {client, dir} = require("../index")
const {Events} = require("discord.js")
const {createAudioPlayer, createAudioResource, joinVoiceChannel, getVoiceConnection} = require("@discordjs/voice")
const {players} = require("../commands/voice")

client.on(Events.VoiceStateUpdate, (oldState, newState) => {
    if (newState.channel) {
        return // Return if still connected
    }
    const connection = getVoiceConnection(oldState.guild.id)

    if (!connection || (connection && oldState.guild.members.me.voice.channelId != oldState.channel.id)) {
        return // Return if bot is not connected or connected to a diff vc.
    }

    const channel = oldState.channel
    if (channel.members > 1) {
        return // Return if theres still someone in vc
    }

    
    connection.disconnect() // Disconnect if noone is in vc
})