mirror of
https://gitlab.jonasled.de/jonasled/discordbot
synced 2024-09-28 13:38:52 +02:00
added musicbot
This commit is contained in:
parent
fdb28e30de
commit
26ca6cd0fa
1 changed files with 105 additions and 3 deletions
106
bot.py
106
bot.py
|
@ -1,14 +1,27 @@
|
|||
#!/usr/bin/env python3
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from discord.ext import commands, tasks
|
||||
from discord.utils import get
|
||||
from discord import FFmpegPCMAudio
|
||||
from sqlite3 import connect
|
||||
from time import sleep, time
|
||||
from datetime import datetime, timedelta
|
||||
from config import *
|
||||
from requests import get
|
||||
from json import loads
|
||||
from os.path import isfile
|
||||
from os import rename, remove
|
||||
from youtube_dl import YoutubeDL
|
||||
from time import time
|
||||
import asyncio
|
||||
|
||||
polls = {}
|
||||
player = None #the Player for the music Bot
|
||||
playerServer = None #The Server the player is active at the moment
|
||||
downloadQue = []
|
||||
playerQue = []
|
||||
titleQue = []
|
||||
fileIndex = 0
|
||||
playing = False
|
||||
|
||||
bot = commands.Bot(command_prefix=prefix)
|
||||
|
||||
|
@ -361,7 +374,96 @@ async def result(ctx, id):#this function prints the result of a poll
|
|||
dbcursor.execute("DELETE FROM poll where pollID = ?", [id])#delete the entries from the poll in the database
|
||||
dbconnection.commit()#write the database to disk
|
||||
|
||||
# ___ ___ _ ______ _
|
||||
# | \/ | (_) | ___ \ | |
|
||||
# | . . | _ _ ___ _ ___ | |_/ / ___ | |_
|
||||
# | |\/| || | | |/ __|| | / __|| ___ \ / _ \ | __|
|
||||
# | | | || |_| |\__ \| || (__ | |_/ /| (_) || |_
|
||||
# \_| |_/ \__,_||___/|_| \___|\____/ \___/ \__|
|
||||
#
|
||||
#
|
||||
|
||||
@bot.command(brief="adds a song to the playlist")
|
||||
async def play(ctx, songURL : str):
|
||||
global player
|
||||
global playerServer
|
||||
global downloadQue
|
||||
|
||||
try:
|
||||
channel = ctx.author.voice.channel#get the current voicechat the user is in
|
||||
except:
|
||||
await ctx.message.channel.send("To use the music bot you have to be in a voice chat")
|
||||
return
|
||||
|
||||
if(player == None):
|
||||
await channel.connect()#connect to this chat
|
||||
player = ctx.voice_client #save the voice client for later use
|
||||
playerServer = ctx.message.guild.id#save the music bot server id to variable
|
||||
|
||||
if(playerServer != ctx.message.guild.id):#check if the music bot is at the moment on this server
|
||||
await ctx.message.channel.send("The Bot is currently connected to a different channel, please wait until he is finished there")
|
||||
return
|
||||
r = get("https://www.youtube.com/oembed?format=json&url=" + songURL)
|
||||
if(r.status_code != 200):
|
||||
await ctx.message.channel.send("Can't find the song")
|
||||
return
|
||||
await ctx.message.channel.send("Adding `" + loads(r.text)["title"] + "` to que.")
|
||||
downloadQue.append(songURL)
|
||||
|
||||
@bot.command(brief="skipps the current song in playlist")
|
||||
async def skip(ctx):
|
||||
global player
|
||||
player.stop()
|
||||
ctx.message.channel.send("Skipped to next song")
|
||||
|
||||
|
||||
async def checkForNextSong():
|
||||
global player
|
||||
global playerQue
|
||||
global playerServer
|
||||
global playing
|
||||
|
||||
while True:
|
||||
if(player != None) and not player.is_playing() and (len(playerQue) > 0):
|
||||
if(playing):
|
||||
remove(playerQue[0])
|
||||
playerQue.remove(playerQue[0])
|
||||
playing = False
|
||||
if(len(playerQue) == 0):
|
||||
print("que is empty")
|
||||
player.disconnect()
|
||||
player = None
|
||||
playerServer = None
|
||||
else:
|
||||
playing = True
|
||||
print("playing next song")
|
||||
player.play(discord.FFmpegPCMAudio(playerQue[0]))
|
||||
else:
|
||||
await asyncio.sleep(1)
|
||||
|
||||
async def downloadAudioFiles():
|
||||
global downloadQue
|
||||
global fileIndex
|
||||
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]])
|
||||
downloadQue.remove(downloadQue[0])
|
||||
playerQue.append(str(fileIndex) + ".mp3")
|
||||
fileIndex = fileIndex + 1
|
||||
else:
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
bot.loop.create_task(checkForNextSong())
|
||||
bot.loop.create_task(downloadAudioFiles())
|
||||
bot.run(token, bot=botAccount)#start the bot with the options in config.py
|
Loading…
Reference in a new issue