Este es mi codigo para "salir de un canal de voz, pero como ven no esta funcional, cuando uso el comando, si no esta en un canal de voz me manda el mensaje" no estoy en un canal de voz.
Quiero hacer que salga del canal de voz sin usar la conexión const = ya que esta es la razón por la que se produce un error en la conexión y desconexión.
soy nuevo y estoy experimentando con esto, entiendo la logica de la programacion pero en este caso no se que hacer
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");Así es como funciona
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!'); } });Aquí está el código completo
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!');