0
votes

I am encountering an error when my discord bot is trying to play an MP3 file in a Voice channel.

My code:

if(message.member.voiceChannel){

        const connection = await message.member.voiceChannel.join();
        const dispatcher = connection.playFile('./resources/shamebell.mp3');

        dispatcher.on('finish', () => {
            console.log('Finished playing!');
          });

          dispatcher.destroy(); // end the stream
}

The bot encounters an error when trying to play the MP3 file. It joins the Voice Channel that the user is in just fine. The bot should join the VC, play the MP3 file then leave afterwards.

Error in dispatcher:

TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string

I have tried using both the absolute path as well as the relative path to the MP3. FFMPEG is installed and windows PATH set as well as NPM installed "ffmpeg-binaries": "^3.2.2-3".

Swapping from connection.playFile('./resources/shamebell.mp3'); to connection.play('./resources/shamebell.mp3'); Gives the error connection.play is not a function at line 14:43

Any help on resolving this issue will be appreciated :)

New code with Discordv12 installed:

const connection = await message.member.voice.channel.join();
const dispatcher = connection.play('resources/shamebell.mp3');

dispatcher.on('finish', () => {
    console.log('Finished playing!');
  });
  dispatcher.destroy();

Same issue:

TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string

Path to MP3 file: C:\Users\Test\Desktop\bot\BotTest\resources\shamebell.mp3

2

2 Answers

2
votes

I used the debugger in VSC and it turns out ffmpeg-static was the issue. The error was being masked.

Error: Cannot find module 'ffmpeg-static'

Installing ffmpeg-static via npm solved the issue.

Can now play MP3s

1
votes

To use .play() you need to install discord.js v12 wich is recommended for Voice Stuff by doing npm i discordjs/discord.js this will get you the newest version.

Warning discord.js master/v12 has breaking changes but a full voice rewrite and a lot of bug fixes.