Discord bot audio playback not working

I’m working on a Discord bot using Discord.js version 12 on Ubuntu 20.04 with Node.js 14.0.0. The bot connects to voice channels fine but won’t play any audio at all.

Here’s my current code setup:

const ytdlCore = require("ytdl-core-discord");
// ...
const audioStream = await ytdlCore(/* youtube video url */, { filter: "audioonly" });
const audioDispatcher = voiceChannel.play(audioStream);

I have all the required packages installed including [email protected], [email protected], @discordjs/[email protected] and [email protected].

When I listen for debug events on the dispatcher, I get this error after about 10 seconds:

Error: ffmpeg stream: write EPIPE
    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:92:16) {
  errno: -32,
  code: 'EPIPE',
  syscall: 'write'
}

I need help getting the bot to actually output sound in voice channels. The issue happens with both local files and YouTube URLs. What could be causing this problem?

yeah, epipe errors often suggest that discord’s voice connection dropped while ffmpeg was handling the audio. try a delay before playing audio, and ensure your bot has the right perms in the voice channel. also check if your ytdl-core-discord version matches discord.js v12.

I had the exact same EPIPE error when I was building my music bot last year. The problem turned out to be that ffmpeg was trying to write to a closed pipe because the voice connection wasn’t fully established yet. What fixed it for me was adding a connection state check before playing audio and implementing proper error handling on the voice connection itself. Try wrapping your play command in a try-catch block and add a connection.on(‘disconnect’) listener to handle unexpected disconnections. Also make sure you’re awaiting the voice connection properly before attempting to play anything. The ffmpeg version you’re using might also be outdated - I switched to a newer build and that resolved some compatibility issues with Discord.js v12.

This EPIPE error typically occurs when there’s a mismatch between your opus libraries or when the voice connection gets terminated unexpectedly. I encountered this exact issue when running Discord bots on Ubuntu servers. The problem was that I had multiple opus encoders installed which created conflicts. Try uninstalling all opus-related packages and reinstall only @discordjs/opus. Also, your [email protected] package is actually a wrapper - you need the actual ffmpeg binary installed on your system via apt-get install ffmpeg. Another thing that helped me was adding a small buffer time after joining the voice channel before attempting to play audio. The voice gateway needs a moment to fully establish the connection. Check your bot’s voice permissions too, specifically the ‘Use Voice Activity’ permission which is sometimes overlooked but necessary for proper audio streaming.