From 564d8f053d861a3578e440cb578f2f31dd484b73 Mon Sep 17 00:00:00 2001 From: top1055 <123alexfeetham@gmail.com> Date: Wed, 24 May 2023 10:03:45 +0100 Subject: [PATCH] added minimal play feature, ready for expantion --- cogs/music/main.py | 32 ++++++++++++++++++++++++++++++ cogs/music/queue.py | 48 ++++++++++++++++++++++++++++++++++++++++++++- help.py | 5 +++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/cogs/music/main.py b/cogs/music/main.py index f8ad3c0..af91194 100644 --- a/cogs/music/main.py +++ b/cogs/music/main.py @@ -2,10 +2,13 @@ import discord from discord.ext import commands from discord.ext.commands.context import Context import cogs.music.util as util +import cogs.music.queue as queue import datetime import pytz +import yt_dlp + from cogs.music.help import music_help class music(commands.Cog): @@ -38,4 +41,33 @@ class music(commands.Cog): await util.join_vc(ctx) await ctx.message.add_reaction('👍') + + @commands.command( + help="Leaves the voice chat if the bot is present", + aliases=['disconnect']) + async def leave(self, ctx: Context): + await util.leave_vc(ctx) + await ctx.message.add_reaction('👍') + @commands.command( + help="Queues a song into the bot", + aliases=['p', 'qeue', 'q']) + async def play(self, ctx: Context, *, url=None): + if url is None: + raise commands.CommandError("Must provide a link or search query") + + await util.join_vc(ctx) + + ydl_opts = { + 'format': 'bestaudio/best', + 'outtmpl': 'downloads/%(title)s.%(ext)s', + } + + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + info = ydl.extract_info(url, download=True) + filename = ydl.prepare_filename(info) + + ctx.voice_client.play(discord.FFmpegPCMAudio(executable="ffmpeg", source=filename), after=self.test) + + def test(self, error): + print("Hello") diff --git a/cogs/music/queue.py b/cogs/music/queue.py index 863fb8f..3a23a48 100644 --- a/cogs/music/queue.py +++ b/cogs/music/queue.py @@ -12,6 +12,7 @@ def initialize_tables(): # Create servers table if it doesn't exist cursor.execute('''CREATE TABLE IF NOT EXISTS servers ( server_id TEXT PRIMARY KEY, + is_playing INTEGER DEFAULT 0, )''') # Create queue table if it doesn't exist @@ -35,6 +36,8 @@ def add_song(server_id, song_link, queued_by): conn = sqlite3.connect(db_path) cursor = conn.cursor() + add_server(server_id, cursor, conn) + # Grab current index cursor.execute(f""" SELECT MAX(index) @@ -56,7 +59,7 @@ def add_song(server_id, song_link, queued_by): # Add server to db if first time queuing -def add_server(server_id, cursor): +def add_server(server_id, cursor, conn): # Check if the server exists cursor.execute('''SELECT COUNT(*) FROM servers @@ -69,6 +72,7 @@ def add_server(server_id, cursor): if not server_exists: cursor.execute('''INSERT INTO servers (server_id) VALUES (?)''', (server_id,)) + conn.commit() # set song as played and update indexes @@ -102,3 +106,45 @@ def mark_song_as_finished(server_id, order_num): # Close connection conn.commit() conn.close() + + +# Sets the playing variable in a server to true or false +def update_server(server_id, playing: bool): + # Connect to database + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + + # add server to db if not present + add_server(server_id, cursor, conn) + + value = 1 if playing else 0 + + # Update field + cursor.execute("""UPDATE servers + SET is_playing = ? + WHERE server_id = ? + """, (value, server_id)) + + # Close connection + conn.commit() + conn.close() + +def is_server_playing(server_id): + # Connect to db + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + + # add server to db if not present + add_server(server_id, cursor, conn) + + cursor.execute("""SELECT is_playing + FROM servers + WHERE server_id = ?""", + (server_id,)) + + result = cursor.fetchone() + + conn.commit() + conn.close() + + return result diff --git a/help.py b/help.py index 171644a..56a42b0 100644 --- a/help.py +++ b/help.py @@ -74,3 +74,8 @@ class AstroHelp(commands.MinimalHelpCommand): channel = self.get_destination() await channel.send(embed=embed) + +# TODO add error support see +# https://gist.github.com/InterStella0/b78488fb28cadf279dfd3164b9f0cf96 +# and +# https://gist.github.com/EvieePy/7822af90858ef65012ea500bcecf1612