I'm making a music bot is discord.py and I've been able to make the bot join a channel and download YouTube audio but when it tries to play the audio, this error occurs. Please help me in what to do as I have already tried to search through google and stack overflow and I can't find anything. The error happens when I try to run the play command.
import discord
from discord.ext import commands
import youtube_dl
import os
client = commands.Bot(command_prefix="?")
@client.event
async def on_ready():
print("Bot is ready.")
@client.command()
async def play(ctx, url: str):
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for the currently playing music to end or use the 'stop' command.")
channel = discord.utils.get(ctx.guild.voice_channels, name="testing")
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice is None:
if not voice.is_connected():
await channel.connect()
else:
await channel.connect()
ydl_opts = {
'format': "bestaudio",
'postprocessors': [{
'key': "FFmpegExtractAudio",
'preferredcodec': "mp3",
'preferredquality': "192"
}]
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"))
@client.command()
async def leave(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice is None:
if voice.is_connected():
await voice.disconnect()
else:
await ctx.send("There is no channel to leave.")
else:
await ctx.send("There is no channel to leave.")
@client.command()
async def pause(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice is None:
if voice.is_playing():
voice.pause()
else:
await ctx.send("No audio is playing.")
else:
await ctx.send("I am currently not in a channel.")
@client.command()
async def resume(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice is None:
if voice.is_paused():
voice.resume()
else:
await ctx.send("Audio is already playing.")
else:
await ctx.send("I am currently not in a channel.")
@client.command()
async def stop(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice is None:
if voice.is_playing:
voice.stop()
else:
await ctx.send("No audio is playing.")
else:
await ctx.send("I am currently not in a channel.")
client.run(Token)