Hey everyone! I’ve been working on a Discord bot with Discord.js and I’m having trouble with the audio playback feature. The bot should respond to a “!play” command followed by a YouTube URL and start playing the audio in a voice channel. Everything else on my bot works fine, but this specific functionality just won’t work anymore. I’ve been debugging for hours but can’t figure out what’s wrong.
if(command === '!play') {
const videoUrl = firstArg;
const ytdlCore = require('ytdl-core');
const audioSettings = { seek: 0, volume: 0.8 };
const voiceBroadcast = client.createVoiceBroadcast();
client.voiceChannel.join()
.then(voiceConnection => {
const audioStream = ytdlCore(firstArg, { filter: 'audioonly' });
voiceBroadcast.playStream(audioStream);
const audioDispatcher = voiceConnection.playBroadcast(voiceBroadcast);
})
.catch(console.error);
}
Any ideas what might be causing this issue?
It seems you’re encountering issues due to the use of deprecated methods in Discord.js. The createVoiceBroadcast() and playBroadcast() functions have been removed in the latest versions. Consider transitioning to the @discordjs/voice package, which replaces the older voice functionalities. You’ll want to implement createAudioPlayer() and createAudioResource() instead. Additionally, keep an eye on ytdl-core, as YouTube frequently alters its API, which can lead to problems. A better alternative may be to use libraries like discord-player or play-dl, which manage these changes more effectively. Although the migration process can be challenging, the new voice system offers much improved stability.
Your code has a basic problem with the voice channel connection. You’re calling client.voiceChannel.join() but client.voiceChannel is probably undefined. You need to grab the voice channel from the message author first - try message.member.voice.channel. You’re also mixing old Discord.js syntax with newer stuff. That voice broadcast approach got deprecated ages ago. I hit the same issue when YouTube broke ytdl-core last year for weeks. Your audio stream might be failing silently because ytdl-core can’t extract audio from certain videos anymore. Log the actual error messages instead of just using console.error - you’ll see what’s really breaking during connection.
you’re using ancient discord.js syntax. voicebroadcast was removed 2 years ago lol. also, you’re not grabbing the voice channel right - use message.member.voice.channel instead of client.voiceChannel. update to discord.js v14 and grab the new voice package. way more reliable than the old junk.