Delete voice channel if 0 user

Guys, could anyone help me make sure the bot doesn't delete the channel if someone is there? At the moment it works, but it doesn't check if anyone has entered since the command was executed, so it deletes it! I'm not very good at API yet, I'm doing my best...
const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
intents: [
GatewayIntentBits.GuildVoiceStates
],
});

setTimeout(async () => {
if (channel.members.size === 0) {
await channel.delete();
const emptyChannelEmbed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle('Salon vocal vide supprimé :x:')
.setDescription(`Le salon vocal ${channel.name} a été supprimé car il était vide.`);
await interaction.channel.send({ embeds: [emptyChannelEmbed] });
}
}, 10000);
const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
intents: [
GatewayIntentBits.GuildVoiceStates
],
});

setTimeout(async () => {
if (channel.members.size === 0) {
await channel.delete();
const emptyChannelEmbed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle('Salon vocal vide supprimé :x:')
.setDescription(`Le salon vocal ${channel.name} a été supprimé car il était vide.`);
await interaction.channel.send({ embeds: [emptyChannelEmbed] });
}
}, 10000);
13 Replies
d.js toolkit
d.js toolkit9mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Maybe Ange
Maybe AngeOP9mo ago
Maybe Ange
Maybe AngeOP9mo ago
Still nothing happens with my new code, I removed the setTimout, but as soon as I leave the channel it doesn't delete
client.on('voiceStateUpdate', async (oldState, newState) => {
const channel = newState.channel || oldState.channel;

if (channel) {
const members = await channel.members.fetch();
const numberOfMembers = members.size;

if (numberOfMembers === 0) {
try {
await channel.delete();

const emptyChannelEmbed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle('Salon vocal vide supprimé :x:')
.setDescription(`Le salon vocal ${channel.name} a été supprimé car il était vide.`);
await interaction.channel.send({ embeds: [emptyChannelEmbed] });
} catch (error) {
console.error(`Error deleting voice channel : ${error}`);
}
}
}
});
client.on('voiceStateUpdate', async (oldState, newState) => {
const channel = newState.channel || oldState.channel;

if (channel) {
const members = await channel.members.fetch();
const numberOfMembers = members.size;

if (numberOfMembers === 0) {
try {
await channel.delete();

const emptyChannelEmbed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle('Salon vocal vide supprimé :x:')
.setDescription(`Le salon vocal ${channel.name} a été supprimé car il était vide.`);
await interaction.channel.send({ embeds: [emptyChannelEmbed] });
} catch (error) {
console.error(`Error deleting voice channel : ${error}`);
}
}
}
});
const client = new Client({
intents: [
GatewayIntentBits.GuildVoiceStates
],
});
const client = new Client({
intents: [
GatewayIntentBits.GuildVoiceStates
],
});
No? Guilds intent? I already have it at the top of the file No I think Oh GatewayIntentBits.Guilds, This? I put it in but still nothing happens or maybe I'm crazy The code looks like this now, but doesn't delete the channel as soon as I exit
d.js docs
d.js docs9mo ago
To share long code snippets, use a service like gist, sourcebin, starbin, or similar instead of posting them as large code blocks or files.
Maybe Ange
Maybe AngeOP9mo ago
https://starb.in/meGsmP.js sry Mhm, wait Dude wtf index.js https://starb.in/sgChW2.js commands/unility/create-channel.js https://starb.in/Y7T6qn.js I succeeded but I can't send the integration to the channel where the command was executed, why? https://starb.in/R6iRqH.js
Maybe Ange
Maybe AngeOP9mo ago
No description
Maybe Ange
Maybe AngeOP9mo ago
True ALL DONE
client.on('voiceStateUpdate', async (oldState, newState) => {
console.log('Voice state updated.');

const channel = newState.channel || oldState.channel;

if (channel) {
const members = channel.members;
const numberOfMembers = members.size;

console.log(`Number of members in the voice channel: ${numberOfMembers}`);

if (numberOfMembers === 0) {
try {
await channel.delete();

console.log(`Voice channel ${channel.name} deleted because it was empty.`);

const commandTextChannel = client.channels.cache.get(newState.guild.systemChannelId);

if (commandTextChannel) {
const emptyChannelEmbed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Salon vocal vide supprimé :x:')
.setDescription(`Le salon vocal ${channel.name} a été supprimé car il était vide.`);

await commandTextChannel.send({ embeds: [emptyChannelEmbed] });
} else {
console.error('Command text channel not found.');
}
} catch (error) {
console.error(`Error deleting voice channel : ${error}`);
}
}
}
});
client.on('voiceStateUpdate', async (oldState, newState) => {
console.log('Voice state updated.');

const channel = newState.channel || oldState.channel;

if (channel) {
const members = channel.members;
const numberOfMembers = members.size;

console.log(`Number of members in the voice channel: ${numberOfMembers}`);

if (numberOfMembers === 0) {
try {
await channel.delete();

console.log(`Voice channel ${channel.name} deleted because it was empty.`);

const commandTextChannel = client.channels.cache.get(newState.guild.systemChannelId);

if (commandTextChannel) {
const emptyChannelEmbed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Salon vocal vide supprimé :x:')
.setDescription(`Le salon vocal ${channel.name} a été supprimé car il était vide.`);

await commandTextChannel.send({ embeds: [emptyChannelEmbed] });
} else {
console.error('Command text channel not found.');
}
} catch (error) {
console.error(`Error deleting voice channel : ${error}`);
}
}
}
});
But one question, I would like to make it so that if no one joins the voice channel within 20 seconds of creating it, it also deletes the channel. @Qjuh
Retropod
Retropod9mo ago
You can change the permissions so that people can't join
Maybe Ange
Maybe AngeOP9mo ago
But what are you telling me? It is not the goal !! I only want to delete the room after 20 seconds if no one joins it after creation.
Retropod
Retropod9mo ago
my bad i misread the message, ^^ setTimeout should work if someone joins then clear the interval
Maybe Ange
Maybe AngeOP9mo ago
A setTimeout, but it will check constantly, it's not good!
Retropod
Retropod9mo ago
i think you're confusing it with setInterval setTimeout only runs the function once after the given timeout
Maybe Ange
Maybe AngeOP9mo ago
I see what I can do, thx
Want results from more Discord servers?
Add your server