5
votes

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.

3
bot.run should be outside of your function. See the examples in the documentationPatrick Haugh
it is, i just added it to this thread wrong. sorry!ropke
Do you have FFmpeg installed?Benjin
yes, i did pip install ffmpeg and it installed correctly. still cant get it to work thoughropke
You need to download FFmpeg separately You can find it here: ffmpeg.org. After you have downloaded it, make sure to add the exe to your path environment variables, otherwise it won't work. See here: discordpy.readthedocs.io/en/rewrite/…Benjin

3 Answers

6
votes

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.')
0
votes

This is how you would do it for the rewritten version which I use for my bot to play mp3 files. You also need opus to be loaded which is easy and also to have FFMPEG.

OPUS_LIBS = ['libopus-0.x86.dll', 'libopus-0.x64.dll', 'libopus-0.dll', 'libopus.so.0', 'libopus.0.dylib']


def load_opus_lib(opus_libs=OPUS_LIBS):
    if opus.is_loaded():
        return True

    for opus_lib in opus_libs:
        try:
            opus.load_opus(opus_lib)
            return
        except OSError:
            pass

        raise RuntimeError('Could not load an opus lib. Tried %s' % (', '.join(opus_libs)))
@bot.command(aliases=['paly', 'queue', 'que'])
async def play(ctx):
    guild = ctx.guild
    voice_client: discord.VoiceClient = discord.utils.get(bot.voice_clients, guild=guild)
    audio_source = discord.FFmpegPCMAudio('vuvuzela.mp3')
    if not voice_client.is_playing():
        voice_client.play(audio_source, after=None)
0
votes

I would do something like this:

voice.play(discord.FFmpegPCMAudio(executable="C:/path/ffmpeg.exe", source="C:/songpath"))