fixed youtube's api interaction, fixed discord connection handshake errors

This commit is contained in:
2025-11-27 12:46:46 +00:00
parent 669e9c19fc
commit c7e033acb6
6 changed files with 217 additions and 79 deletions

View File

@@ -206,14 +206,14 @@ async def get_current_song(server_id):
WHERE server_id = ?
LIMIT 1;''',
(server_id,))
result = cursor.fetchone()
# Close connection
conn.commit()
conn.close()
return result[0]
return result[0] if result else "Nothing"
# Grab max order from server
@@ -312,34 +312,51 @@ async def grab_songs(server_id):
return max, songs
# call play on ffmpeg exit
class AstroPlayer(discord.FFmpegPCMAudio):
def __init__(self, ctx, source, options) -> None:
self.ctx = ctx
super().__init__(source, **options)
def _kill_process(self):
super()._kill_process()
if self.ctx.voice_client.is_playing():
return
asyncio.run(play(self.ctx))
# Play and loop songs in server
async def play(ctx):
"""Main playback loop - plays songs from queue sequentially"""
server_id = ctx.guild.id
voice_client = ctx.voice_client
# Wait until song is stopped playing fully
while ctx.voice_client.is_playing():
await asyncio.sleep(1)
# Safety check
if voice_client is None:
await update_server(server_id, False)
return
# check next song
# Wait until current song finishes
while voice_client.is_playing():
await asyncio.sleep(0.5)
# Get next song
url = await pop(server_id)
# if no other song update server and return
# If no songs left, update status and return
if url is None:
await update_server(server_id, False)
return
# else play next song and call play again
ctx.voice_client.play(
AstroPlayer(ctx, url, FFMPEG_OPTS))
try:
# Create audio source
audio_source = discord.FFmpegPCMAudio(url, **FFMPEG_OPTS)
# Play with callback to continue queue
def after_playing(error):
if error:
print(f"Player error: {error}")
# Schedule the next song in the event loop
if voice_client and not voice_client.is_connected():
return
coro = play(ctx)
fut = asyncio.run_coroutine_threadsafe(coro, ctx.bot.loop)
try:
fut.result()
except Exception as e:
print(f"Error playing next song: {e}")
voice_client.play(audio_source, after=after_playing)
except Exception as e:
print(f"Error starting playback: {e}")
# Try to continue with next song
await play(ctx)