0
votes

I want my bot to join the voice channel where the commanding person is located and say something then leave, I tried to do this but I failed. The bot joins the sound channel but does not say anything. How can I do that?

Codes:

    client.on('message', async message => {
      if (!message.guild) return;

      if (message.content.toLowerCase() === prefix + "bruh" ) {
        if (message.member.voice.channel) {
          const connection = await message.member.voice.channel.join();
        } else {
          message.reply('First of all you have to join an audio channel !');
          const ytdl = require('ytdl-core');
          connection.play(ytdl('https://www.youtube.com/watch?v=2ZIpFytCSVc', { filter: 'audioonly' }));
        }
      }
    });
1

1 Answers

0
votes

Your bot is not playing that YouTube video because you did not tell it to since you put the play() method in the else statement, not the if statement where it belongs.

Also, when importing a package, it is important you do so at the top of your file, not within the code, this is bad practice.

const ytdl = require('ytdl-core');

client.on('message', async message => {
  if (!message.guild) return;

  if (message.content.toLowerCase() === prefix + "bruh") {
    if (message.member.voice.channel) {
      const connection = await message.member.voice.channel.join();
      connection.play(ytdl('https://www.youtube.com/watch?v=2ZIpFytCSVc', { filter: 'audioonly' }));
    } else {
      message.reply('First of all you have to join an audio channel !');
    }
  }
});