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");
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!');