Skip to content

Commit 547a41c

Browse files
authored
Fixed issue with youtube url not playing
I need to do a full rework of the youtube commands. I dont like how laggy it is when loading playlists. I also had the bright idea to make a second queue for youtube. That was dumb and I want to revert it back.
1 parent 2c16585 commit 547a41c

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

plex_music_bot.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,9 @@ def __init__(self, options, interaction, music_queue):
884884
async def callback(self, interaction: discord.Interaction):
885885
interaction.response.defer()
886886
selected_option = self.values[0]
887-
song_info = await asyncio.to_thread(YouTube.get_song_info, {"link": selected_option})
887+
song_info = await YouTube.get_song_info(selected_option)
888+
if song_info is None:
889+
return await interaction.followup.send("❌ This is not a valid YouTube video link.")
888890
song = (song_info['url'], f"📺 {song_info['title']}", song_info['duration'] * 1000, song_info)
889891
await play_song(self.interaction, *song, send_message=True, music_queue=self.music_queue, play_called=False)
890892

@@ -920,10 +922,10 @@ async def get_raw_url(url):
920922
return None
921923

922924
@staticmethod
923-
def get_song_info(entry):
925+
async def get_song_info(entry):
924926
with yt_dlp.YoutubeDL(YouTube.ydl_opts) as ydl:
925927
try:
926-
info_dict = ydl.extract_info(entry["link"], download=False)
928+
info_dict = ydl.extract_info(entry, download=False)
927929
yt_thumbnail = "https://images.freeimages.com/fic/images/icons/820/simply_google/256/google_youtube.png"
928930
return {'title': info_dict['title'], 'duration': info_dict['duration'], 'thumbnail': info_dict.get('thumbnail', 'yt_thumbnail'), 'url': info_dict['url']}
929931
except Exception as e:
@@ -968,6 +970,8 @@ async def youtube(interaction: discord.Interaction, search: str = None, video_ur
968970
if raw_url is None:
969971
return await interaction.followup.send("❌ This is not a valid YouTube video link.")
970972
song_info = await YouTube.get_song_info(video_url)
973+
if song_info is None:
974+
return await interaction.followup.send("❌ This is not a valid YouTube video link.")
971975
song = (raw_url, f"📺 {song_info['title']}", song_info['duration'] * 1000, song_info)
972976
await play_song(interaction, *song, send_message=True, music_queue=music_queue, play_called=False)
973977
elif playlist_url:
@@ -980,6 +984,7 @@ async def youtube(interaction: discord.Interaction, search: str = None, video_ur
980984
if playlist_entries:
981985
# Process and add songs in the playlist in the background
982986
bot.loop.create_task(process_playlist(interaction, playlist_entries, music_queue))
987+
# Add a placeholder song to the queue
983988
placeholder_song = ("placeholder", f"📺 Playlist: {playlist['info']['title']} ({total_songs} songs)", 0, {})
984989
print(f"{playlist['info']['title']}")
985990
await music_queue.add_song(placeholder_song)
@@ -992,7 +997,7 @@ async def youtube(interaction: discord.Interaction, search: str = None, video_ur
992997

993998
async def process_playlist(interaction, playlist_entries, music_queue):
994999
for entry in playlist_entries:
995-
song_info = await asyncio.to_thread(YouTube.get_song_info, entry)
1000+
song_info = await YouTube.get_song_info(entry['link'])
9961001
if song_info is None:
9971002
await interaction.channel.send(f"❌ Error extracting audio from video {entry['link']}. Skipping.")
9981003
continue
@@ -1009,6 +1014,8 @@ async def process_playlist(interaction, playlist_entries, music_queue):
10091014

10101015

10111016

1017+
1018+
10121019
commands = {
10131020
"play": "Play a song or playlist. Usage: /play <song/playlist name>",
10141021
"playlist": "Toggle loop mode. Usage: /playlist",
@@ -1042,7 +1049,4 @@ async def help(interaction: discord.Interaction, command: str = None):
10421049

10431050

10441051

1045-
1046-
1047-
1048-
bot.run(TOKEN)
1052+
bot.run(TOKEN)

0 commit comments

Comments
 (0)