finished queue system, overwrites discord kill process to loop
This commit is contained in:
@@ -1,7 +1,17 @@
|
||||
import sqlite3
|
||||
|
||||
import discord
|
||||
import asyncio
|
||||
|
||||
db_path = "./data/music.db"
|
||||
|
||||
FFMPEG_OPTS = {
|
||||
'before_options':
|
||||
'-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
|
||||
|
||||
'options':
|
||||
'-vn'
|
||||
}
|
||||
|
||||
# Creates the tables if they don't exist
|
||||
def initialize_tables():
|
||||
@@ -12,18 +22,24 @@ 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,
|
||||
)''')
|
||||
is_playing INTEGER DEFAULT 0
|
||||
);''')
|
||||
|
||||
# Set all to not playing
|
||||
cursor.execute("UPDATE servers SET is_playing = 0;")
|
||||
|
||||
# Create queue table if it doesn't exist
|
||||
cursor.execute('''CREATE TABLE IF NOT EXISTS queue (
|
||||
server_id TEXT,
|
||||
server_id TEXT NOT NULL,
|
||||
song_link TEXT,
|
||||
queued_by TEXT,
|
||||
index INTEGER,
|
||||
position INTEGER NOT NULL,
|
||||
has_played INTEGER DEFAULT 0,
|
||||
PRIMARY KEY (server_id, order_num)
|
||||
)''')
|
||||
PRIMARY KEY (position),
|
||||
FOREIGN KEY (server_id) REFERENCES servers(server_id)
|
||||
);''')
|
||||
# Clear all entries
|
||||
cursor.execute("DELETE FROM queue;")
|
||||
|
||||
# Commit the changes and close the connection
|
||||
conn.commit()
|
||||
@@ -31,26 +47,17 @@ def initialize_tables():
|
||||
|
||||
|
||||
# Queue a song in the db
|
||||
def add_song(server_id, song_link, queued_by):
|
||||
async def add_song(server_id, song_link, queued_by):
|
||||
# Connect to db
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
add_server(server_id, cursor, conn)
|
||||
await add_server(server_id, cursor, conn)
|
||||
|
||||
# Grab current index
|
||||
cursor.execute(f"""
|
||||
SELECT MAX(index)
|
||||
FROM queue
|
||||
WHERE server_id = ?
|
||||
""", (server_id,))
|
||||
result = cursor.fetchone()
|
||||
|
||||
# Highnest number or 0
|
||||
max_order_num = result[0] + 1 if result[0] is not None else 0
|
||||
max_order_num = await get_max(server_id, cursor) + 1
|
||||
|
||||
cursor.execute("""
|
||||
INSERT INTO queue (server_id, song_link, queued_by, index)
|
||||
INSERT INTO queue (server_id, song_link, queued_by, position)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""", (server_id, song_link, queued_by, max_order_num))
|
||||
|
||||
@@ -59,7 +66,7 @@ def add_song(server_id, song_link, queued_by):
|
||||
|
||||
|
||||
# Add server to db if first time queuing
|
||||
def add_server(server_id, cursor, conn):
|
||||
async def add_server(server_id, cursor, conn):
|
||||
# Check if the server exists
|
||||
cursor.execute('''SELECT COUNT(*)
|
||||
FROM servers
|
||||
@@ -76,46 +83,73 @@ def add_server(server_id, cursor, conn):
|
||||
|
||||
|
||||
# set song as played and update indexes
|
||||
def mark_song_as_finished(server_id, order_num):
|
||||
async def mark_song_as_finished(server_id, order_num):
|
||||
# Connect to the database
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Update the song as finished
|
||||
cursor.execute('''DELETE FROM queue
|
||||
WHERE server_id = ? AND order_num = ?''',
|
||||
WHERE server_id = ? AND position = ?''',
|
||||
(server_id, order_num))
|
||||
#cursor.execute('''UPDATE queue
|
||||
# SET is_finished = 1
|
||||
# WHERE server_id = ? AND index = ?''',
|
||||
# (server_id, order_num))
|
||||
|
||||
# Get the order numbers of unplayed songs
|
||||
cursor.execute('''SELECT index
|
||||
FROM queue
|
||||
WHERE server_id = ? AND is_finished = 0''', (server_id,))
|
||||
unplayed_order_nums = [row[0] for row in cursor.fetchall()]
|
||||
|
||||
# Update the order numbers of unplayed songs
|
||||
for new_order, old_order in enumerate(unplayed_order_nums, start=1):
|
||||
cursor.execute('''UPDATE queue
|
||||
SET order_num = ?
|
||||
WHERE server_id = ? AND order_num = ?''',
|
||||
(new_order, server_id, old_order))
|
||||
|
||||
# Close connection
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
# Grab max order from server
|
||||
async def get_max(server_id, cursor):
|
||||
cursor.execute(f"""
|
||||
SELECT MAX(position)
|
||||
FROM queue
|
||||
WHERE server_id = ?
|
||||
""", (server_id,))
|
||||
result = cursor.fetchone()
|
||||
|
||||
# Highnest number or 0
|
||||
max_order_num = result[0] if result[0] is not None else -1
|
||||
|
||||
return max_order_num
|
||||
|
||||
|
||||
# Pop song from server
|
||||
async def pop(server_id):
|
||||
# Connect to db
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# JUST INCASE!
|
||||
await add_server(server_id, cursor, conn)
|
||||
|
||||
max_order = await get_max(server_id, cursor)
|
||||
if max_order == -1:
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return None
|
||||
|
||||
cursor.execute('''SELECT song_link
|
||||
FROM queue
|
||||
WHERE server_id = ? AND position = ?
|
||||
''', (server_id, max_order))
|
||||
result = cursor.fetchone()
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
await mark_song_as_finished(server_id, max_order)
|
||||
|
||||
return result[0]
|
||||
|
||||
|
||||
# Sets the playing variable in a server to true or false
|
||||
def update_server(server_id, playing: bool):
|
||||
async 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)
|
||||
await add_server(server_id, cursor, conn)
|
||||
|
||||
value = 1 if playing else 0
|
||||
|
||||
@@ -129,13 +163,14 @@ def update_server(server_id, playing: bool):
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def is_server_playing(server_id):
|
||||
|
||||
async 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)
|
||||
await add_server(server_id, cursor, conn)
|
||||
|
||||
cursor.execute("""SELECT is_playing
|
||||
FROM servers
|
||||
@@ -143,8 +178,55 @@ def is_server_playing(server_id):
|
||||
(server_id,))
|
||||
|
||||
result = cursor.fetchone()
|
||||
print(result)
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return result
|
||||
return True if result[0] == 1 else False
|
||||
|
||||
|
||||
# Delete all songs from a server
|
||||
async def clear(server_id):
|
||||
# Connect to db
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
await add_server(server_id, cursor, conn)
|
||||
await update_server(server_id, False)
|
||||
|
||||
# Delete all songs from the server
|
||||
cursor.execute('''DELETE FROM queue WHERE server_id = ?''', (server_id,))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
# Play and loop songs in server
|
||||
async def play(ctx):
|
||||
server_id = ctx.guild.id
|
||||
|
||||
# Wait until song is stopped playing
|
||||
#while ctx.voice_client.is_playing():
|
||||
#await asyncio.sleep(1)
|
||||
|
||||
# check next song
|
||||
url = await pop(server_id)
|
||||
|
||||
# if no other song update server and return
|
||||
if url is None:
|
||||
await update_server(server_id, False)
|
||||
return
|
||||
|
||||
# else play next song and call play again
|
||||
ctx.voice_client.play(
|
||||
AstroPlayer(ctx, url, FFMPEG_OPTS))
|
||||
|
||||
class AstroPlayer(discord.FFmpegPCMAudio):
|
||||
def __init__(self, ctx, source, options) -> None:
|
||||
self.ctx = ctx
|
||||
super().__init__(source, **options)
|
||||
|
||||
def _kill_process(self):
|
||||
super()._kill_process
|
||||
asyncio.run(play(self.ctx))
|
||||
|
||||
Reference in New Issue
Block a user