added minimal play feature, ready for expantion

This commit is contained in:
2023-05-24 10:03:45 +01:00
parent dd2ee80269
commit 564d8f053d
3 changed files with 84 additions and 1 deletions

View File

@@ -2,10 +2,13 @@ import discord
from discord.ext import commands from discord.ext import commands
from discord.ext.commands.context import Context from discord.ext.commands.context import Context
import cogs.music.util as util import cogs.music.util as util
import cogs.music.queue as queue
import datetime import datetime
import pytz import pytz
import yt_dlp
from cogs.music.help import music_help from cogs.music.help import music_help
class music(commands.Cog): class music(commands.Cog):
@@ -38,4 +41,33 @@ class music(commands.Cog):
await util.join_vc(ctx) await util.join_vc(ctx)
await ctx.message.add_reaction('👍') 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")

View File

@@ -12,6 +12,7 @@ def initialize_tables():
# Create servers table if it doesn't exist # Create servers table if it doesn't exist
cursor.execute('''CREATE TABLE IF NOT EXISTS servers ( cursor.execute('''CREATE TABLE IF NOT EXISTS servers (
server_id TEXT PRIMARY KEY, server_id TEXT PRIMARY KEY,
is_playing INTEGER DEFAULT 0,
)''') )''')
# Create queue table if it doesn't exist # 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) conn = sqlite3.connect(db_path)
cursor = conn.cursor() cursor = conn.cursor()
add_server(server_id, cursor, conn)
# Grab current index # Grab current index
cursor.execute(f""" cursor.execute(f"""
SELECT MAX(index) SELECT MAX(index)
@@ -56,7 +59,7 @@ def add_song(server_id, song_link, queued_by):
# Add server to db if first time queuing # 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 # Check if the server exists
cursor.execute('''SELECT COUNT(*) cursor.execute('''SELECT COUNT(*)
FROM servers FROM servers
@@ -69,6 +72,7 @@ def add_server(server_id, cursor):
if not server_exists: if not server_exists:
cursor.execute('''INSERT INTO servers (server_id) cursor.execute('''INSERT INTO servers (server_id)
VALUES (?)''', (server_id,)) VALUES (?)''', (server_id,))
conn.commit()
# set song as played and update indexes # set song as played and update indexes
@@ -102,3 +106,45 @@ def mark_song_as_finished(server_id, order_num):
# Close connection # Close connection
conn.commit() conn.commit()
conn.close() 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

View File

@@ -74,3 +74,8 @@ class AstroHelp(commands.MinimalHelpCommand):
channel = self.get_destination() channel = self.get_destination()
await channel.send(embed=embed) await channel.send(embed=embed)
# TODO add error support see
# https://gist.github.com/InterStella0/b78488fb28cadf279dfd3164b9f0cf96
# and
# https://gist.github.com/EvieePy/7822af90858ef65012ea500bcecf1612