1
0
Fork 0
mirror of https://gitlab.jonasled.de/jonasled/discordbot synced 2024-07-06 09:18:35 +02:00

moved youtube-dl in background task (don't block bot)

This commit is contained in:
Jonas Leder 2020-04-10 20:32:49 +02:00
parent 0179277dba
commit f6e49e816b

47
bot.py
View file

@ -12,7 +12,8 @@ from os.path import isfile
from os import rename, remove
from youtube_dl import YoutubeDL
from time import time
import asyncio
from asyncio import sleep
from threading import Thread
polls = {}
player = None #the Player for the music Bot
@ -414,7 +415,7 @@ async def play(ctx, songURL : str):
async def skip(ctx):
global player
player.stop()
await ctx.message.channel.send("Skipped to next song")
await ctx.message.channel.send("Skipped current song")
@bot.command(brief="Prints the current que")
async def que(ctx):
@ -450,30 +451,36 @@ async def checkForNextSong():
playing = True
print("playing next song")
player.play(discord.FFmpegPCMAudio(playerQue[0]))
await asyncio.sleep(1)
await sleep(1)
async def downloadAudioFiles():
global downloadQue
global fileIndex
global titleQue
while True:
if(len(downloadQue) > 0):
ydl_opts = { #youtube-dl arguments
'format': 'worstaudio/worst',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'outtmpl': str(fileIndex) +'.%(ext)s',
}
with YoutubeDL(ydl_opts) as ydl:
ydl.download([downloadQue[0][0]])
playerQue.append(str(fileIndex) + ".mp3")
titleQue.append(downloadQue[0][1])
downloadQue.remove(downloadQue[0])
fileIndex = fileIndex + 1
await asyncio.sleep(1)
thread = Thread(target=backgroundDownloader, args=(downloadQue[0],))
thread.start()
downloadQue.remove(downloadQue[0])
await sleep(1)
def backgroundDownloader(downloadQue):
global playerQue
global fileIndex
ydl_opts = { #youtube-dl arguments
'format': 'worstaudio/worst',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'outtmpl': str(fileIndex) +'.%(ext)s',
}
with YoutubeDL(ydl_opts) as ydl:
ydl.download([downloadQue[0]])
playerQue.append(str(fileIndex) + ".mp3")
titleQue.append(downloadQue[1])
fileIndex = fileIndex + 1
bot.loop.create_task(checkForNextSong())
bot.loop.create_task(downloadAudioFiles())