1
0
Fork 0
mirror of https://gitlab.jonasled.de/jonasled/discordbot synced 2024-10-03 07:48:58 +02:00

new song will be directly downloaded (no que) and add volume command

This commit is contained in:
Jonas Leder 2020-04-11 11:57:55 +02:00
parent 8663128b91
commit 989663c3e5

35
bot.py
View file

@ -19,7 +19,7 @@ from threading import Thread
polls = {}
player = None #the Player for the music Bot
playerServer = None #The Server the player is active at the moment
downloadQue = []
audioPlayer = None
playerQue = []
titleQue = []
fileIndex = 0
@ -415,7 +415,7 @@ async def play(ctx, songURL : str):
await ctx.message.channel.send("Can't find the song")
return
await ctx.message.channel.send("Adding `" + loads(r.text)["title"] + "` to que.")#print the user a status message
downloadQue.append([loads(r.text)["html"].split("src=")[1][1:].split("\"")[0], loads(r.text)["title"]])#add the title + the url to the download que
backgroundDownloader(loads(r.text)["html"].split("src=")[1][1:].split("\"")[0], loads(r.text)["title"])
@bot.command(brief="skipps the current song in playlist")
async def skip(ctx):
@ -455,6 +455,14 @@ async def stop(ctx):
if files.endswith(".mp3"):
remove(files)
@bot.command(brief="changes the volume of the music bot")
async def volume(ctx, volume : int):
global audioPlayer
if(volume < 0) or (volume > 100):
ctx.message.channel.send("Volume has to be between 0 and 100")
else:
audioPlayer.volume = volume / 100
async def checkForNextSong():#background task, that runns every seccond and checks if the song ended
@ -463,6 +471,7 @@ async def checkForNextSong():#background task, that runns every seccond and chec
global playerServer
global playing
global titleQue
global audioPlayer
while True:
if(player != None) and not player.is_playing() and (len(playerQue) > 0):#check if the player is active and the song ended
@ -480,21 +489,12 @@ async def checkForNextSong():#background task, that runns every seccond and chec
remove(files)
else:
playing = True#set playing to true and play the next file
player.play(discord.FFmpegPCMAudio(playerQue[0]))
print("playing " + playerQue[0])
audioPlayer = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(playerQue[0]))
player.play(audioPlayer)
await sleep(1)#sleep 1 seccond and allo the bot to run different tasks
async def downloadAudioFiles():
global downloadQue#import the global variables
global titleQue
while True:#run forever
if(len(downloadQue) > 0):#check if files ready to download
thread = Thread(target=backgroundDownloader, args=(downloadQue[0],))#if yes make a background task to download it
thread.start()#run the background task
downloadQue.remove(downloadQue[0])#remove the currently downloading file from the que
await sleep(1)#sleep 1 seccond and allo the bot to run different tasks
def backgroundDownloader(downloadQue):
def backgroundDownloader(url, title):
global playerQue#import the global variables
global fileIndex
ydl_opts = { #youtube-dl arguments
@ -508,10 +508,9 @@ def backgroundDownloader(downloadQue):
}
fileIndex = fileIndex + 1 #add 1 to the file index counter (used to find the mp3 files easy later and that there are no files with the same name)
with YoutubeDL(ydl_opts) as ydl:#download the song with the arguments from up
ydl.download([downloadQue[0]])
ydl.download([url])
playerQue.append(str(fileIndex - 1) + ".mp3")#add the file to the que
titleQue.append(downloadQue[1])#add the title to the que (for que list)
titleQue.append(title)#add the title to the que (for que list)
bot.loop.create_task(checkForNextSong())#start the music bot tasks in background
bot.loop.create_task(downloadAudioFiles())
bot.run(token, bot=botAccount)#start the bot with the options in config.py