Search code examples
discorddiscord.jsbotsvoiceytdl

How do I get my discord bot to join a vc that a user is in?


I'm trying to get my bot join a vc that a user is in, when saying "+sing". After the audio is finished the bot should disconnect from the vc. This is as far as i've gotten, I don't know what I have done wrong.

 client.on("message", msg => {
  if (message.content === '+sing') {
    const connection = msg.member.voice.channel.join()
  channel.join().then(connection => {
      console.log("Successfully connected.");
      connection.play(ytdl('https://www.youtube.com/watch?v=jRxSRyrTANY', { quality: 'highestaudio' }));
  }).catch(e => {
      console.error(e);
  connection.disconnect();
  })
});  

Btw I'm using discord.js.


Solution

  • You can get their GuildMember#VoiceState#channel and use that as the channel object to join

    client.on("message", msg => {
      if (message.content === '+sing') {
        // you should add a check to make sure they are in a voice channel
    
        // join their channel
        const connection = msg.member.voice.channel.join()
      }
    })