1
0
Fork 0
mirror of https://gitlab.jonasled.de/jonasled/discordbot synced 2024-07-11 19:58:33 +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 os import rename, remove
from youtube_dl import YoutubeDL from youtube_dl import YoutubeDL
from time import time from time import time
import asyncio from asyncio import sleep
from threading import Thread
polls = {} polls = {}
player = None #the Player for the music Bot player = None #the Player for the music Bot
@ -414,7 +415,7 @@ async def play(ctx, songURL : str):
async def skip(ctx): async def skip(ctx):
global player global player
player.stop() 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") @bot.command(brief="Prints the current que")
async def que(ctx): async def que(ctx):
@ -450,30 +451,36 @@ async def checkForNextSong():
playing = True playing = True
print("playing next song") print("playing next song")
player.play(discord.FFmpegPCMAudio(playerQue[0])) player.play(discord.FFmpegPCMAudio(playerQue[0]))
await asyncio.sleep(1) await sleep(1)
async def downloadAudioFiles(): async def downloadAudioFiles():
global downloadQue global downloadQue
global fileIndex global titleQue
while True: while True:
if(len(downloadQue) > 0): if(len(downloadQue) > 0):
ydl_opts = { #youtube-dl arguments thread = Thread(target=backgroundDownloader, args=(downloadQue[0],))
'format': 'worstaudio/worst', thread.start()
'postprocessors': [{ downloadQue.remove(downloadQue[0])
'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)
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(checkForNextSong())
bot.loop.create_task(downloadAudioFiles()) bot.loop.create_task(downloadAudioFiles())