2
votes

I have made a custom discord bot for a fan server, which is hosted by Heroku so it can stay online when I'm not. What I want to do is to have the bot play an mp3 file from its folder when someone gives a specific command for it, but I want the music to play in a specific voice channel.

First, I tried treating it, the same as an image file. For example:

if (message.content == "Play the Funky Tune.")
{ 
    message.reply("Of course, I hope you enjoy!", { files: ["./Music/FunkyTune.mp3"] });
}

However this causes the bot to put the music file there, and while you can click on it and play it, the moment you click on another channel, the music is paused. I want it to play the song in a specific voice channel so that it continues playing when the user clicks on another channel.

When doing research for this, every topic is always asking for a YouTube video link, which converts it into audio, but this is not what I want, since anything can happen to YouTube videos.

P.S. I am not interested in using already made bots, I want to integrate this music feature into my own bot.

1

1 Answers

5
votes
if (message.content == "Play the Funky Tune.") {
    // Checking if the message author is in a voice channel.
    if (!message.member.voice.channel) return message.reply("You must be in a voice channel.");
    // Checking if the bot is in a voice channel.
    if (message.guild.me.voice.channel) return message.reply("I'm already playing.");

    // Joining the channel and creating a VoiceConnection.
    message.member.voice.channel.join().then(VoiceConnection => {
        // Playing the music, and, on finish, disconnecting the bot.
        VoiceConnection.play("./Music/FunkyTune.mp3").on("finish", () => VoiceConnection.disconnect());
        message.reply("Playing...");
    }).catch(e => console.log(e))
};

Note that you must run the following commands in order for this to work:

  • npm install ffmpeg-static
  • npm install @discordjs/opus