This repository has been archived by the owner on Oct 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_.py
55 lines (47 loc) · 1.79 KB
/
queue_.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from discord import FFmpegPCMAudio
import asyncio
from yt_dlp import YoutubeDL
from config import FFMPEG_OPTIONS, ydl_opts
from bot_instance import bot
from RandoFileToPreventCircularImport import queue, queue_count, current_song, song_count
ydl_opts = {
'format': 'bestaudio/best',
'quiet': True,
'outtmpl': '%(title)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
#queue error handling
def check_queue(ctx, error):
coro = play_next(ctx)
fut = asyncio.run_coroutine_threadsafe(coro, bot.loop)
try:
fut.result()
except:
pass
async def play_next(ctx):
global queue, voice_channel, queue_count, url, video_title, song_count, current_song_url, current_song_title, ydl_opts
voice_channel = ctx.guild.voice_client
if not queue.empty() and not voice_channel.is_playing():
song = await queue.get()
url = song['url']
video_title = song['title']
song_count += 1
if song_count > 1:
await ctx.reply('NEXT')
with YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=False)
video_title = info_dict.get('title', 'Unknown Title')
stream_url = info_dict.get('url')
if stream_url:
source = FFmpegPCMAudio(stream_url, **FFMPEG_OPTIONS)
voice_channel.play(source, after=lambda e: check_queue(ctx, e))
current_song[ctx.guild.id] = {'title': video_title, 'url': url}
print(f"Updated current song for guild {ctx.guild.id}: {current_song[ctx.guild.id]}") # Debug print
else:
print(f"Failed to extract stream URL for {url}")
if queue.empty():
song_count = 0