Discord bot not playing audio - ytdl-core-discord issue

Hey everyone! I’m new to making Discord bots and I’m stuck with a problem. I made a music bot that should play YouTube audio, but it’s not working right. The bot joins the voice channel when I use the command, but there’s no sound at all. Here’s my code:

const { SlashCommandBuilder } = require('discord.js');
const { joinVoiceChannel, createAudioPlayer, createAudioResource } = require('@discordjs/voice');
const ytdl = require('ytdl-core-discord');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('music')
    .setDescription('Stream YouTube audio')
    .addStringOption(option =>
      option.setName('link')
        .setDescription('YouTube video link')
        .setRequired(true)),

  async execute(interaction, client) {
    const voiceChannel = interaction.member.voice.channel;
    if (!voiceChannel) return interaction.reply('Join a voice channel first!');

    const videoLink = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';

    try {
      const connection = joinVoiceChannel({
        channelId: voiceChannel.id,
        guildId: voiceChannel.guild.id,
        adapterCreator: voiceChannel.guild.voiceAdapterCreator,
      });

      const audioPlayer = createAudioPlayer();
      const videoStream = ytdl(videoLink, { filter: 'audioonly' });
      const audioResource = createAudioResource(videoStream);

      audioPlayer.play(audioResource);
      connection.subscribe(audioPlayer);

      interaction.reply('Started playing!');
    } catch (err) {
      console.error('Error:', err);
      interaction.reply('Oops! Something went wrong.');
    }
  }
};

The code runs without errors, but there’s no audio. Any ideas what I’m doing wrong?

I’ve dealt with this issue before, and it can be frustrating. One thing to check is your ffmpeg installation. Make sure it’s properly installed and accessible in your system’s PATH. Also, try adding a ‘ready’ event listener to your audioPlayer:

audioPlayer.on(‘error’, error => {
console.error(‘Error:’, error.message);
console.error(‘Audio player error:’, error);
});

This will help you catch any playback errors. Additionally, consider using a try-catch block around the ytdl function call, as it can sometimes throw errors silently. If all else fails, you might want to explore alternative libraries like discord-player or discord-music-player, which can simplify the audio playback process significantly.

hey alexj, welcome to bot dev! looks like ur using ytdl-core-discord, but its kinda outdated. try switching to play-dl instead. it works way better with discord.js v14. also, make sure ur discord.js and @discordjs/voice packages r up to date. that might solve ur audio issues. good luck!

I’ve encountered similar issues when working with Discord bots and YouTube audio. From my experience, the problem might be related to how you’re handling the audio stream. Instead of using ytdl directly, try wrapping it in a demuxProbe() function from @discordjs/voice. Here’s a snippet that worked for me:

const { demuxProbe } = require('@discordjs/voice');

// ... inside your execute function
const stream = await ytdl(videoLink, { filter: 'audioonly', highWaterMark: 1 << 25 });
const { stream: probeStream, type } = await demuxProbe(stream);
const resource = createAudioResource(probeStream, { inputType: type });

audioPlayer.play(resource);

This approach helped me get the audio playing correctly. Also, double-check your bot’s permissions in the server to ensure it can connect and speak in voice channels. Hope this helps you get your bot singing!