Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
Connection.Destroy() Leave channel voice discordjs v13

This is my code to "leave a voice channel, but as you can see it is not functional, when I use the command, if it is not in a voice channel it sends me the message" I am not in a voice channel.

I want to make it get out of the voice channel without using the const connection = since this is the reason why the if connect and disconnect is bugged.

I am new and I am experimenting with this, I understand the logic of the programming but in this case I do not know what to do

if(message.member.voice.channel)
        {
            const connection = joinVoiceChannel({
                channelId: message.member.voice.channel.id,
                guildId: message.guild.id,
                adapterCreator: message.guild.voiceAdapterCreator,
                });
            message.channel.send("Disconected!");
        }
        message.channel.send("i am not in a channel voice");
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here's how it works

client.on('messageCreate', async (message) => {

    if (message.content.toLocaleLowerCase() === prefix + 'join') { //Here's how to join a voice channel 

        if (!message.member.voice.channel) return message.channel.send('You need to be a voice channel to execute this command!')
        if(!message.member.voice.channel.joinable) return message.channel.send('I need permission to join your voice channel!')

        const connection = joinVoiceChannel({
            channelId: message.member.voice.channel.id,
            guildId: message.member.guild.id,
            adapterCreator: message.channel.guild.voiceAdapterCreator
        })

        console.log('Connected to voice!');
    }

    if (message.content.toLocaleLowerCase() === prefix + 'leave') { //Here's how to leave from voice channel
        const connection = getVoiceConnection(message.guild.id)

        if(!connection) return message.channel.send("I'm not in a voice channel!")

        connection.destroy()

        console.log('Disconnected from voice!');
    }
});

Here's full code

const { Client, Intents } = require('discord.js')
const { joinVoiceChannel, getVoiceConnection } = require('@discordjs/voice')

const client = new Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MEMBERS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_VOICE_STATES /// <= Don't miss this :)
    ]
});

var prefix = '!'

client.on('ready', async () => {
    console.log('Client is Ready...');
});

client.on('messageCreate', async (message) => {

    if (message.content.toLocaleLowerCase() === prefix + 'join') { //Here's how to join a voice channel 

        if (!message.member.voice.channel) return message.channel.send('You need to be a voice channel to execute this command!')
        if(!message.member.voice.channel.joinable) return message.channel.send('I need permission to join your voice channel!')

        const connection = joinVoiceChannel({
            channelId: message.member.voice.channel.id,
            guildId: message.member.guild.id,
            adapterCreator: message.channel.guild.voiceAdapterCreator
        })

        console.log('Connected to voice!');
    }

    if (message.content.toLocaleLowerCase() === prefix + 'leave') { //Here's how to leave from voice channel
        const connection = getVoiceConnection(message.guild.id)

        if(!connection) return message.channel.send("I'm not in a voice channel!")

        connection.destroy()

        console.log('Disconnected from voice!');
    }
});

client.login('BOT TOKEN HERE!');
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!