My problem that if ‘Owner’ of voice channel leaves before someone else, then if in voice channel size of member = 0
nothing happens. I think that because my db, i’m quite bad in mongodb.
I have 3 files:
joinvoice.js:
const { SlashCommandBuilder, 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");
const voiceCooldown = new Map();
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 });
if (newVoiceState.channel && newVoiceState.channel.id === VoiceData?.ChannelID) {
const now = Date.now();
const cooldownTime = 60 * 1000;
if (voiceCooldown.has(newVoiceState.member.id)) {
const expirationTime = voiceCooldown.get(newVoiceState.member.id) + cooldownTime;
if (now < expirationTime) {
const remainingTime = (expirationTime - now) / 1000;
return newVoiceState.member.send(`Please wait ${remainingTime.toFixed(1)} seconds before creating another voice channel.`);
}
}
let voice_channel = await newVoiceState.guild.channels.create({
name: UserData?.ChannelName || `${newVoiceState.member.displayName}-voice`,
type: ChannelType.GuildVoice,
parent: newVoiceState.channel.parent
}).then(async (channel) => {
await newVoiceState.member.voice.setChannel(channel);
await UserVoice.findOneAndUpdate(
{ UserID: newVoiceState.member.id },
{
ChannelID: newVoiceState.channel.id
},
{
new: true,
upsert: true,
}
);
voiceCooldown.set(newVoiceState.member.id, Date.now());
});
} else if (oldVoiceState?.channel?.id === UserData?.ChannelID || newVoiceState?.channel?.id !== UserData?.ChannelID) {
const channel = oldVoiceState.guild.channels.cache.get(UserData?.ChannelID);
if (channel && channel.members.size === 0) {
await channel.delete();
await UserVoice.findOneAndDelete({ UserID: newVoiceState.member.id });
}
}
},
};
and 2 mongo:
UserVoice.js this one storing custom name of voice channel and id of the channel:
const { model, Schema } = require("mongoose");
let UserVoice = new Schema({
UserID: String,
ChannelID: String,
ChannelName: String,
});
module.exports = model("UserVoice", UserVoice);
VoiceSchema.js this one is for channel Join to create:
const { model, Schema } = require("mongoose");
let VoiceSchema = new Schema({
GuildID: String,
ChannelID: String,
});
module.exports = model("VoiceSchema", VoiceSchema);
Ty for help! ^_^