A strange bug in discord.py music bot - python-3.x

I'm making a discord.py bot that can play music. When I run it on my local machine sometimes everything works just fine but sometimes when I try running it with a playlist, it just cant find songs in the playlist.
Here is the play command:
#commands.command(name='play',aliases=["p","sing"])
async def _play(self, ctx: commands.Context, *, song1: str):
"""Plays a song.
If there are songs in the queue, this will be queued until the
other songs finished playing.
This command automatically searches from various sites if no URL is provided.
A list of these sites can be found here: https://rg3.github.io/youtube-dl/supportedsites.html
"""
if not ctx.voice_state.voice:
await ctx.invoke(self._join)
x = False
if "playlist?" not in song1:
songs = [song1]
else:
x = True
"""html_content = urllib.request.urlopen(song1)
html_content = html_content.read().decode()"""
r = requests.get(song1)
html_content = r.text
pattern = "href=\"\/watch\?v=(.{11})"
songs = re.findall(pattern,html_content)
print(f"Before: {songs}")
songs = list(dict.fromkeys(songs))
print(f"after: {songs}")
#songs.append(f"https://www.youtube.com/watch?v={code}")
for search in songs:
s = search if not x else f"https://www.youtube.com/watch?v={search}"
try:
source = await YTDLSource.create_source(ctx, s, loop=self.bot.loop)
except YTDLError as e:
await ctx.send('An error occurred while processing this request: {}'.format(str(e)))
else:
song = Song(source)
await ctx.voice_state.songs.put(song)
if not x:
await ctx.send('Enqueued {}'.format(str(source)))
if x:
await ctx.send(f"Succesfully queued the playlist `{song1}`")
Here what is in the console
Command that I run I discord:
pplay https://www.youtube.com/playlist?list=PL2n_fVXKImKlfv3PlcHZTzLk3CoGUV9Hm
output when it works:
Before: ['gEbRqpFkTBk', 'gEbRqpFkTBk', 'gEbRqpFkTBk', 'gEbRqpFkTBk', '4ZvnbsfXRk0', '4ZvnbsfXRk0', 'besNDPvEwQw', 'besNDPvEwQw', 'QglaLzo_aPk', 'QglaLzo_aPk', 'YJTae5ScvQA', 'YJTae5ScvQA', '9Va88Kt0NN0', '9Va88Kt0NN0']
after: ['gEbRqpFkTBk', '4ZvnbsfXRk0', 'besNDPvEwQw', 'QglaLzo_aPk', 'YJTae5ScvQA', '9Va88Kt0NN0']
output most of the time:
Before: []
after: []
It doesn't give any error.

You can use the extract_info method from youtube-dl :
video_list = []
with youtube_dl.YoutubeDL() as ydl:
playlist = ydl.extract_info(url='playlist url', download=False)['entries']
for video in playlist:
video_list.append(video['webpage_url'])
video_list will contain every URLs from your playlist's videos.

Related

Discord music bot not reading the command

So, I'm trying to make a discord music bot and I keep getting this one error whenever I use the play command I think its not loading the cog or has something to do with that. this is my main function
and this is my command inside my music_player classthe error that I'm getting once I run the code
import discord
from discord.ext import commands
import os
from youtube_dl import YoutubeDL
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(
command_prefix=commands.when_mentioned_or("!"),
description='Relatively simple music bot example',
intents=intents,
)
#bot.event
async def on_ready():
print(f'Logged in as {bot.user} (ID: {bot.user.id})')
print('------')
bot.add_cog("cogs.music_player")
music_player.py
import os
import discord
from discord.ext import commands
from youtube_dl import YoutubeDL
class music_player(commands.Cog):
def __init__(self, client):
self.client = client
# Checks whether the song is playing or not
self.isplaying = False
self.ispaused = False
# The music queue ( this contains the song and the channel)
self.musicque = []
# The code below is taken from github to get the best quality of sound possible
self.ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0', # bind to ipv4 since ipv6 addresses cause issues sometimes
}
self.ffmpeg_options = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
self.vc = None
# This small function searches a song on youtube
def search_yt(self, song):
# with youtube open as
with YoutubeDL(self.ytdl_format_options) as ydl:
# This will basically search youtube and return the entries we get from our search
try:
info = ydl.extract_info("ytsearch:%s" % song, download=False)['entries'][0]
except Exception:
return False
# Returns the info as source
return {'source': info['formats'][0]['url'], 'title': info['title']}
def play_next(self):
if len(self.musicque) > 0:
self.isplaying = True
# Get the link of the first song in the que as we did in the play song function
music_link = self.musicque[0][0]['source']
# Remove the song currently playing same way we did in the play_song function
self.musicque.pop(0)
# same lambda function we used the play_song function
self.vc.play(discord.FFmpegPCMAudio(music_link, **self.ffmpeg_options), after=lambda e: self.play_next())
else:
self.isplaying = False
async def play_song(self, ctx):
if len(self.musicque) > 0:
self.isplaying = True
# Get the link of the first song in the que
music_link = self.musicque[0][0]['source']
# Connect to the voice channel the user is currently in if bot is not already connected
if self.vc == None or not self.vc.is_connected():
self.vc = await self.musicque[0][1].connect()
# if we fail to connect to the vc for whatever reason
if self.vc == None:
await ctx.send("Could not connect to the voice channel")
return
# Else if the bot is already in voice
else:
await self.vc.move_to(self.musicque[0][1])
# Remove the first song in the que using the built in pop function in python as we're already playing the song
self.musicque.pop(0)
# Took this lambda play function from github
self.vc.play(discord.FFmpegPCMAudio(music_link, **self.ffmpeg_options), after=lambda e: self.next_song())
"""WENT AHEAD AND MOVED NEXT_SONG FUNCTION ABOVE AS I REALIZED IT WOULD NOT WORK IF IT WAS BELOW"""
"""ALL THE FUNCTIONS WE NEEDED FOR OUR COMMANDS TO FUNCTION HAVE BEEN DEFINED NOW ONTO THE COMMAND"""
#commands.command()
async def play(self, ctx, *, args):
# This is the song that the user will search and we will look up
using the yt-search function that we made earlier
query = " ".join(args)
# If user is not in the voice channel
voice_channel = ctx.author.voice_channel
if voice_channel is None:
await ctx.send("You're not in a voice channel you dummy")
# If any song in the que is currently paused resume it
elif self.ispaused == True:
self.vc.resume()
else:
# assign song to the search result of the youtube song
song = self.search_yt(query)
if type(song) == type(True):
await ctx.send("Incorrect format of song could not play")
else:
await ctx.send("Song added")
self.musicque.append([song, voice_channel])
if self.isplaying == False:
await self.play_song(ctx)
I was expecting the program to play a song or atleast join thet voice channel but apparently it says the command is not found I've tried changing stuff with the cog but it didn't help so I'm fully lost at what I'm doing wrong.
The add_cog method doesn't work that way; it takes a cog class as an argument, not the path to the cog file. That's the load_extension's job. The load_extension will go to the given path and call the setup function inside the file, and you have to add the cog inside that setup function. For example:
 
cogs/cog_file.py
class ACogClass(discord.ext.commands.Cog):
    ...
 
async def setup(bot: discord.ext.commands.Bot): # as of discord.py 2, the "setup" function needs to be an async function
    bot.add_cog(ACogClass(bot))
 
main.py
bot = discord.ext.commands.Bot(...)
 
async def setup_hook():
    await bot.load_extension("cogs.cog_file") # as of discord.py 2, the "load_extension" method is now an async function
 
bot.setup_hook = setup_hook # set the bot's default "setup_hook" to our custom "setup_hook"

How to queue music by using the play command

I'm working on a Discord music Bot and I'm now at the part that I want the bot to be able to add a song to the queue whenever someone commands it to play a song while it is already playing one.
(I don't want to have a seperate "queue" command, I want it to do it automatically)
So far this is my code (only the functions that matter):
async def play_queue(self, ctx: discord.ext.commands.context.Context):
audio = await self.get_audio(self.queue.pop(0))
vc = ctx.voice_client
if not self.is_playing(ctx):
vc.play(audio)
if len(self.queue) > 0:
await self.play_queue(ctx)
#commands.command(name="play", pass_ctx=True)
async def play(self, ctx: discord.ext.commands.context.Context, url=""):
await self.handle_connected(ctx)
self.queue.append(url)
# send a message to inform that a song has been queued or being played
await self.send_playing(ctx)
await self.play_queue(ctx)
I can Q 1 song at a time but can't queue multiple because it's stuck in the while loop
Done it by using the modules: threading, asyncio
This is the code segment that works:
async def idle_speaker(self, ctx: discord.ext.commands.context.Context):
while self.is_playing(ctx):
time.sleep(1)
await self.play_queue(ctx)
def wait_for_idle(self, ctx: discord.ext.commands.context.Context):
self.thread = True
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.idle_speaker(ctx))
loop.close()
self.thread = False
async def play_queue(self, ctx: discord.ext.commands.context.Context):
if not self.is_playing(ctx):
audio = await self.get_audio(self.queue.pop(0))
ctx.voice_client.play(audio)
if len(self.queue) > 0:
playing_t = threading.Thread(target=self.wait_for_idle, name="Song_Listener", args=[ctx])
playing_t.start()
#commands.command(name="play", pass_ctx=True)
async def play(self, ctx: discord.ext.commands.context.Context, url=""):
await self.handle_connected(ctx)
self.queue.append(url)
print(self.queue, self.is_playing(ctx))
# send a message to inform that a song has been queued or being played
await self.send_playing(ctx)
if not self.thread:
await self.play_queue(ctx)

How to make the discord bot queue local mp3?

I'm new to python, so I wonder if there's a way to do so.
Here's my play mp3 command:
#bot.command()
async def play_song1(ctx):
global voice
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
voice.play(discord.FFmpegPCMAudio('./mp3/song1.mp3'))
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.1
await ctx.send ('playing')
while voice.is_playing():
await asyncio.sleep(.1)
await voice.disconnect()
I made 2 more same commands but for song2 and song3, and now I want to queue the mp3 when someone calls them.
Seen as if you're not using cogs, you could try something like this:
guild_queues = {} # Multi-server support as a dict, just use a list for one server
# EDIT: Map song names in a dict
play_messages = {
"song1": "Now playing something cool!",
"song2": "And something even cooler just started playing!",
"song3": "THE COOLEST!"
}
async def handle_queue(ctx, song):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
channel = ctx.author.voice.channel
if voice and channel and voice.channel != channel:
await voice.move_to(channel)
elif not voice and channel:
voice = await channel.connect()
if not voice.is_playing():
audio = discord.FFmpegPCMAudio(f"./{song}.mp3")
source = discord.PCMVolumeTransformer(audio)
source.volume = 0.1
voice.play(audio)
await ctx.send(play_messages[song])
while voice.is_playing():
await asyncio.sleep(.1)
if len(guild_queues[ctx.guild.id]) > 0:
next_song = guild_queues[ctx.guild.id].pop(0)
await handle_queue(ctx, next_song)
else:
await voice.disconnect()
#bot.command()
async def play(ctx, *, song): # I'd recommend adding the filenames as an arg, but it's up to you
# Feel free to add a check if the filename exists
try:
# Get the current guild's queue
queue = guild_queues[ctx.guild.id]
except KeyError:
# Create a queue if it doesn't already exist
guild_queues[ctx.guild.id] = []
queue = guild_queues[ctx.guild.id]
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
queue.append(song)
# The one song would be the one currently playing
if voice and len(queue) > 0:
await ctx.send("Added to queue!")
else:
current_song = queue.pop(0)
await handle_queue(ctx, current_song)
References:
utils.get()
Client.voice_clients
Context.guild
Member.voice
VoiceState.channel
VoiceClient.move_to()
VoiceClient.is_playing()
VoiceClient.play()
discord.FFmpegPCMAudio()
discord.PCMVolumeTransformer()
Context.send()

Playing music with a bot from Youtube without downloading the file

How would i go about playing music using a discord bot from Youtube without downloading the song as a file?
I've already had a look at the included music bot in the discord.py documentation but that one downloads a file to the directory. Is there any way to avoid this? Code from the documentation example:
ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}
ffmpeg_options = {
'options': '-vn'
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, volume=0.5):
super().__init__(source, volume)
self.data = data
self.title = data.get('title')
self.url = data.get('url')
#classmethod
async def from_url(cls, url, *, loop=None, stream=False):
loop = loop or asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download= not stream))
if 'entries' in data:
# take first item from a playlist
data = data['entries'][0]
filename = data['url'] if stream else ytdl.prepare_filename(data)
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
#client.command()
async def play(ctx, url):
voice = await ctx.author.voice.channel.connect()
player = await YTDLSource.from_url(url, loop=client.loop)
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
To play music without downloading it, simply use this code into your play function :
ydl_opts = {'format': 'bestaudio'}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(video_link, download=False)
URL = info['formats'][0]['url']
voice = get(self.bot.voice_clients, guild=ctx.guild)
voice.play(discord.FFmpegPCMAudio(URL))
Here is what each line is used for :
ydl_opts = {'format': 'bestaudio'} : get the best possible audio
with youtube_dl.YoutubeDL(ydl_opts) as ydl: : initialize youtube-dl
info = ydl.extract_info(video_link, download=False) : get a dictionary, named info, containing all the video information (title, duration, uploader, description, ...)
URL = info['formats'][0]['url'] : get the URL which leads to the audio file of the video
voice = get(self.bot.voice_clients, guild=ctx.guild) : initialize a new audio player
voice.play(discord.FFmpegPCMAudio(URL)) : play the right music
However, Playing audio from an URL without downloading it causes a known issue explained here
To fix it, just add a variable, for instance, FFMPEG_OPTIONS which will contain options for FFMPEG:
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
Once you've created the variable, you just have to add one argument to the FFmpegPCMAudio method:
voice.play(discord.FFmpegPCMAudio(URL, **FFMPEG_OPTIONS))

Discord.py rewrite - what is the source for YoutubeDL to play music?

As mentioned in the docs here, I need to use a source to play music using the play() command, I am trying to use YoutubeDL but I can't figure it out.
I have checked the rapptz discord.py basic voice example, but since I'm not using object-oriented programming its confusing me quite alot. Everywhere I have looked, their example is using the v0.16 discord.py, and I can't work out how to convert this player = await voice_client.create_ytdl_player(url) into the rewrite.
My play function at the moment looks like this:
async def play(ctx, url = None):
...
player = await YTDLSource(url)
await ctx.voice_client.play(player)
await ctx.send("Now playing: " + player.title())
"YTDLSource" being a placeholder for the source.
Any help greatly appreciated, thanks.
I am sure there are better ways of doing this with the rewrite, but I am in the same boat as you. I could not figure it out for the longest time.
After looking through youtube-dl documents and the rewrite documents this is the best I could come up with. Keep in mind I do not know if this will work with a queue system (probably not). Also I don't know if it's a bug or something I'm doing wrong when the bot joins and then you use the play command it does not output the music, but if the bot leaves then joins again the music will play. To fix I made my join command join, leave, and join.
Join command:
#bot.command(pass_context=True, brief="Makes the bot join your channel", aliases=['j', 'jo'])
async def join(ctx):
channel = ctx.message.author.voice.channel
if not channel:
await ctx.send("You are not connected to a voice channel")
return
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await voice.disconnect()
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await ctx.send(f"Joined {channel}")
play command:
#bot.command(pass_context=True, brief="This will play a song 'play [url]'", aliases=['pl'])
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 current playing music end or use the 'stop' command")
return
await ctx.send("Getting everything ready, playing audio soon")
print("Someone wants to play music let me get that ready for them...")
voice = get(bot.voice_clients, guild=ctx.guild)
ydl_opts = {
'format': 'bestaudio/best',
'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"))
voice.volume = 100
voice.is_playing()
Leave command:
#bot.command(pass_context=True, brief="Makes the bot leave your channel", aliases=['l', 'le', 'lea'])
async def leave(ctx):
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.disconnect()
await ctx.send(f"Left {channel}")
else:
await ctx.send("Don't think I am in a voice channel")
All that needs to be imported (I think):
import discord
import youtube_dl
import os
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
from os import system
you also might need to download ffmpeg off their website (there are youtube tutorials on how to do so and install it)
With the Play command post with a youtube url ('/play www.youtube.com') it will first look for a 'song.mp3' and delete it if there is one, download the new song rename it to 'song.mp3' then plays the mp3 file. The mp3 file will be put in them same directory as your bot.py
Like I said before there is probably a batter way to do this allowing a queue command, but I don't know that way as of now.
hope this helps!
The discord docs now have a full example on how to make a voice bot that implements ytdl!
Check out the yt method in https://github.com/Rapptz/discord.py/blob/master/examples/basic_voice.py :
#commands.command()
async def yt(self, ctx, *, url):
"""Plays from a url (almost anything youtube_dl supports)"""
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.bot.loop)
ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send('Now playing: {}'.format(player.title))
And the YTDLSource class it depends on:
ytdl_format_options = {
'format': 'bestaudio/best',
'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}
ffmpeg_options = {
'options': '-vn'
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, volume=0.5):
super().__init__(source, volume)
self.data = data
self.title = data.get('title')
self.url = data.get('url')
#classmethod
async def from_url(cls, url, *, loop=None, stream=False):
loop = loop or asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
if 'entries' in data:
# take first item from a playlist
data = data['entries'][0]
filename = data['url'] if stream else ytdl.prepare_filename(data)
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
Change player = await YTDLSource.from_url(url, loop=self.bot.loop) to player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True) if you'd rather stream audio from youtube instead of predownloading it
pastebin archive: https://pastebin.com/nEiJ5YrD

Resources