#Delete voice channel if 0 user

48 messages · Page 1 of 1 (latest)

meager zinc
#

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);```
copper bobcatBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • 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!
meager zinc
past drift
#

Do the check in voiceStateUpdate event instead of an interval. That way you can delete it when the last user leaves and not have to check repeatedly

meager zinc
# past drift Do the check in voiceStateUpdate event instead of an interval. That way you can ...

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}`);
            }
        }
    }
});```
past drift
#

That should error. Because Collection#fetch() doesn’t exist

#

Also you need Guilds intent

meager zinc
#

No?

#

Guilds intent?

past drift
#

You don’t have it, yes. But you need it

meager zinc
#

I already have it at the top of the file

past drift
#

Huh? Where? Did you define two clients?

meager zinc
#

No

#

I think

past drift
#

I only see GuildVoiceStates intent. Not Guilds

meager zinc
#

Oh

#

GatewayIntentBits.Guilds,

#

This?

past drift
#

Yes

meager zinc
#

The code looks like this now, but doesn't delete the channel as soon as I exit

sly quiverBOT
#

To share long code snippets, use a service like gist, sourcebin, starbin, or similar instead of posting them as large code blocks or files.

meager zinc
#

sry

past drift
#

Why is there an extra client defined in a command file? So you did define another client, didn’t you?

#

Also don’t add listeners inside command files, you‘ll add another listener every time the command is used

meager zinc
#

Mhm, wait

#

Dude wtf

past drift
#

Yes, now add the voiceStateUpdate listener in your index.js instead nvm, didn’t see you already did

#

And you don’t need the member fetch there. With GuildVoiceStates intent the channel.members will already be accurate

meager zinc
past drift
#

Because there is no interaction happening? It’s a voiceStateUpdate, not an interactionCreate event

meager zinc
#

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}`);
                }
            }
        }
    });```
#

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.

meager zinc
#

@past drift

analog otter
meager zinc
past drift
#

Do that check in a setTimeout in the command code then

analog otter
#

setTimeout should work

#

if someone joins then clear the interval

meager zinc
analog otter
#

setTimeout only runs the function once after the given timeout