javascriptdiscord.jsplaysound

Discord.js bot trying to play mp3 file on voice channel - no error and also no sound


My bot joins a voice channel, and then leaves and sends the message "Playing ok!", but does not play the mp3. I'm on Windows and I already checked that I could play the mp3 file with ffmpeg by command line. Does anyone know what's wrong?

const path = require('path')

client.channels.fetch('Channel_ID').then(channel => {
     console.log(channel.name)
     console.log(path.join(__dirname, 'music.mp3'))
     channel.join().then((connection) => {
         const dispatcher = connection.play(path.join(__dirname, 'music.mp3'));
         dispatcher.on("speaking", speaking => {
             if (!speaking) {
               channel.leave()
               msg.channel.send("Playing ok!")
             }
        });         
   });
});

Update: I changed the 'speaking' for the 'finish' event, and also tried the audio file in the comments. Now, the bot joins to the voice channel, stays the same time of the duration of mp3, and then leave the voice channel, but no sound yet.

Update 2: I added ffmpeg to my PATH now. The bot showed a green circle like its playing something, but no sound again. Just in case, I put my bot under Admin permission, but also it was not enough.

Thanks for the help.

Using discord.js v12.5.3.


Solution

  • For discord.js voice to work, you need to have following packages installed (as stated in the docs under the installation section): either @discordjs/opus or opusscript.

    npm install @discordjs/opus
    

    Also discord.js depends on prism-media and you should have ffmpeg installed for this to work properly.

    npm install ffmpeg-static
    

    If you don't want to install ffmpeg as a npm package and you already have ffmpeg installed, make sure to add it to the PATH environmental variable and restart your working environment.

    The OP said, that restarting his computer after adding ffmpeg to the PATH worked.

    Rest of the info from comments:

    Also your message "Playing ok!" is wrong, it means that the bot is not speaking... See above you have if (!speaking) { ... }. So when you get this message after the bot joined the voice channel, something is wrong, because the StreamDispatcher ended right away.

    To debug this, take a look at the error event of StreamDispatcher.