Implementing mp3 playback in a Discord bot for a specific voice channel

Hey everyone! I’ve got this Discord bot I made for my fan server. It’s running on Heroku to stay online 24/7. I’m trying to figure out how to make it play an mp3 file from its folder when someone types a certain command. The catch is I want the music to play in a specific voice channel.

I first tried treating it like an image file:

if message.content == 'Play the Groovy Beat':
    message.reply('Sure thing! Enjoy the music!', files=['./Tunes/GroovyBeat.mp3'])

But this just posts the file in chat. It stops when you switch channels. I want it to keep playing in the voice channel even if users are looking at other text channels.

I’ve looked online, but most solutions involve YouTube links. That’s not what I’m after. I want to use my own mp3 files.

Any ideas on how to make this work? I don’t want to use pre-made bots. I’m set on adding this feature to my own bot. Thanks!

I’ve developed several Discord bots and learned that playing local MP3 files is entirely feasible when using the discord.py library with voice support alongside FFmpeg. In my experience, the process begins by connecting to the specific voice channel and creating an audio source through FFmpeg before playing it using the voice client. The challenge usually lies in managing situations where the bot is already connected or when there are no users present. I also found that implementing a simple queue can help manage multiple tracks more effectively. Remember to handle disconnects gracefully to maintain smooth operation.

I’ve implemented a similar feature in my Discord bot using a voice client to connect to the designated audio channel. The process involves installing discord.py with voice support and then using discord.VoiceChannel.connect() to join the channel. Once connected, you can create an audio source using discord.FFmpegPCMAudio() with your mp3 file and play it with voice_client.play().

Be sure to handle scenarios such as the bot already being connected or the absence of users in the channel. Also, ensure FFmpeg is installed on your Heroku dyno for audio processing. This approach should guide you in integrating mp3 playback effectively.

yo, i’ve done this before! u gotta use FFmpeg and discord.py voice stuff. connect to the channel first, then make an audio source with the mp3. use voice_client.play() to start it up.

watch out for when the bot’s already in a channel or nobody’s there. heroku can be tricky with FFmpeg, so check that out too. good luck!