How do I make my discord.py bot play mp3 in voice channel?
Solution 1
I did something similar with my discord bot, here's some example code you can reference. If you're playing mp3 files make sure to install ffmpeg, I followed the instructions here when I was setting up my bot https://github.com/adaptlearning/adapt_authoring/wiki/Installing-FFmpeg
@client.command(
name='vuvuzela',
description='Plays an awful vuvuzela in the voice channel',
pass_context=True,
)
async def vuvuzela(context):
# grab the user who sent the command
user=context.message.author
voice_channel=user.voice.voice_channel
channel=None
# only play music if user is in a voice channel
if voice_channel!= None:
# grab user's voice channel
channel=voice_channel.name
await client.say('User is in channel: '+ channel)
# create StreamPlayer
vc= await client.join_voice_channel(voice_channel)
player = vc.create_ffmpeg_player('vuvuzela.mp3', after=lambda: print('done'))
player.start()
while not player.is_done():
await asyncio.sleep(1)
# disconnect after the player has finished
player.stop()
await vc.disconnect()
else:
await client.say('User is not in a channel.')
Solution 2
I would do something like this:
voice.play(discord.FFmpegPCMAudio(executable="C:/path/ffmpeg.exe", source="C:/songpath"))
ropke
Updated on December 18, 2020Comments
-
ropke over 3 years
I'm a beginner in Python and I have recently started making a discord bot for some friends and I. The idea is to type !startq and have the bot join the channel, play an mp3 file that is locally stored in the same folder that the bot.py is in also.
import discord, chalk from discord.ext import commands import time import asyncio bot = commands.Bot(command_prefix = "!") @bot.event async def on_ready(): print("Bot is ready!") @bot.command() async def q5(ctx): await ctx.send("@here QUEUE STARTING IN 5 MINUTES") @bot.command() async def q3(ctx): await ctx.send("@here QUEUE STARTING IN 3 MINUTES") @bot.command() async def q1(ctx): await ctx.send("@here QUEUE STARTING IN 1 MINUTES") @bot.command() async def ping(ctx): ping_ = bot.latency ping = round(ping_ * 1000) await ctx.send(f"my ping is {ping}ms") @bot.command() async def startq(ctx): voicechannel = discord.utils.get(ctx.guild.channels, name='queue') vc = await voicechannel.connect() vc.play(discord.FFmpegPCMAudio("countdown.mp3"), after=lambda e: print('done', e)) bot.run('TOKEN')
So far my bot joins the channel fine, but it doesn't actually play the mp3. I've asked countless people in the "Unofficial Discord API Discord" and a few other programming Discords, but I haven't gotten an answer yet.