diff --git a/audio.py b/audio.py index 893e402..24a228e 100644 --- a/audio.py +++ b/audio.py @@ -120,8 +120,8 @@ async def fetch_and_queue_song(interaction: discord.Interaction, query: str): async def play_next(interaction: discord.Interaction): guild_id = interaction.guild.id - # If no songs are left in the queue if guild_id not in music_queues or not music_queues[guild_id]: + # If no songs are left in the queue if guild_id in now_playing_messages and now_playing_messages[guild_id]: try: message = await interaction.channel.fetch_message(now_playing_messages[guild_id]) @@ -139,10 +139,6 @@ async def play_next(interaction: discord.Interaction): song_url, title, duration = music_queues[guild_id].pop(0) current_tracks[guild_id] = (song_url, title) - # Stop any existing progress bar task - if guild_id in progress_tasks: - progress_tasks[guild_id].cancel() - # Prepare FFmpeg options ffmpeg_options = { 'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', @@ -163,29 +159,33 @@ async def play_next(interaction: discord.Interaction): ), ) - # Update or create the "Now Playing" embed + # Create the enhanced embed embed = discord.Embed( title="🎵 Now Playing", description=f"**{title}**\n\n`00:00 / {time.strftime('%M:%S', time.gmtime(duration))}`", color=discord.Color.green() ) + embed.set_thumbnail(url="https://img.youtube.com/vi/{}/0.jpg".format(song_url.split("?v=")[-1])) # Thumbnail + embed.add_field(name="Artist", value=artist, inline=True) + embed.set_footer(text="React with 👍 or 👎 to rate this song!") view = PlaybackControls(interaction, guild_id) + # Update or send the embed if guild_id in now_playing_messages and now_playing_messages[guild_id]: try: message = await interaction.channel.fetch_message(now_playing_messages[guild_id]) await message.edit(embed=embed, view=view) except discord.NotFound: + # If the message was deleted, send a new one message = await interaction.followup.send(embed=embed, view=view) now_playing_messages[guild_id] = message.id else: message = await interaction.followup.send(embed=embed, view=view) now_playing_messages[guild_id] = message.id - - # Start the progress bar task + # Start updating the progress bar start_time = time.time() - progress_tasks[guild_id] = asyncio.create_task(update_progress_bar(interaction, duration, start_time)) + asyncio.create_task(update_progress_bar(interaction, duration, start_time)) except Exception as e: await interaction.followup.send(f"❌ **Error:** Could not play the next song. {str(e)}") @@ -215,7 +215,7 @@ async def update_progress_bar(interaction: discord.Interaction, duration: int, s await message.edit(embed=embed) except discord.NotFound: break - await asyncio.sleep(2) + await asyncio.sleep(1) except asyncio.CancelledError: pass