const { ChannelType, ButtonInteraction, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, PermissionFlagsBits, ModalBuilder, TextInputBuilder, TextInputStyle, CommandInteraction, Client, joinVoiceChannel } = require("discord.js");
const UserVoice = require("../../Models/UserVoice");
const VoiceSchema = require("../../Models/VoiceSchema");
async function checkEmptyVoiceChannel(voice_channel) {
if (voice_channel && voice_channel.members.size === 0) {
await voice_channel.delete();
console.log(`Deleted voice channel ${voice_channel.name} (${voice_channel.id}) because it was empty`);
}
}
module.exports = {
name: "voiceStateUpdate",
async execute(oldVoiceState, newVoiceState) {
const VoiceData = await VoiceSchema.findOne({ GuildID: newVoiceState.guild.id });
const UserData = await UserVoice.findOne({ UserID: newVoiceState.member.id });
let voice_channel;
if (newVoiceState.channel && newVoiceState.channel.id === VoiceData.ChannelID) {
voice_channel = await newVoiceState.guild.channels.create({
name: `${newVoiceState.member.displayName}-voice`,
type: ChannelType.GuildVoice,
parent: newVoiceState.channel.parent
})
await UserVoice.findOneAndUpdate(
{ UserID: newVoiceState.member.id },
{
ChannelID: voice_channel.id,
},
{
new: true,
upsert: true,
}
)
await newVoiceState.member.voice.setChannel(voice_channel);
} else if (oldVoiceState.channel.id === voice_channel.id && oldVoiceState.channel.members.size === 0) {
await checkEmptyVoiceChannel(oldVoiceState.channel);
}
},
};