added db functions and join/leave vc
This commit is contained in:
4
cogs/music/help.py
Normal file
4
cogs/music/help.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from discord.ext import commands
|
||||
|
||||
class music_help(commands.MinimalHelpCommand):
|
||||
pass #TODO
|
||||
@@ -1,23 +1,41 @@
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from discord.ext.commands.context import Context
|
||||
import cogs.music.util as util
|
||||
|
||||
import datetime
|
||||
import pytz
|
||||
|
||||
from cogs.music.help import music_help
|
||||
|
||||
class music(commands.Cog):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
self.name = "🎶 Music"
|
||||
self.emoji = "🎶"
|
||||
self.client = client
|
||||
|
||||
help_command = music_help()
|
||||
help_command.cog = self
|
||||
self.help_command = help_command
|
||||
|
||||
@commands.command(
|
||||
help="Displays latency from the bot",
|
||||
aliases=['delay'])
|
||||
async def ping(self, e: Context):
|
||||
async def ping(self, ctx: Context):
|
||||
start_time = datetime.datetime.now(pytz.utc)
|
||||
end_time = e.message.created_at
|
||||
end_time = ctx.message.created_at
|
||||
|
||||
delay = int((end_time - start_time).total_seconds() * 1000)
|
||||
|
||||
await e.send(f"Pong! `{delay}MS`")
|
||||
await ctx.send(f"Pong! `{delay}MS`")
|
||||
|
||||
|
||||
@commands.command(
|
||||
help="Connects to your current voice channel",
|
||||
aliases=['connect'])
|
||||
async def join(self, ctx: Context):
|
||||
await util.join_vc(ctx)
|
||||
await ctx.message.add_reaction('👍')
|
||||
|
||||
|
||||
|
||||
104
cogs/music/queue.py
Normal file
104
cogs/music/queue.py
Normal file
@@ -0,0 +1,104 @@
|
||||
import sqlite3
|
||||
|
||||
db_path = "./data/music.db"
|
||||
|
||||
|
||||
# Creates the tables if they don't exist
|
||||
def initialize_tables():
|
||||
# Connect to the database
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Create servers table if it doesn't exist
|
||||
cursor.execute('''CREATE TABLE IF NOT EXISTS servers (
|
||||
server_id TEXT PRIMARY KEY,
|
||||
)''')
|
||||
|
||||
# Create queue table if it doesn't exist
|
||||
cursor.execute('''CREATE TABLE IF NOT EXISTS queue (
|
||||
server_id TEXT,
|
||||
song_link TEXT,
|
||||
queued_by TEXT,
|
||||
index INTEGER,
|
||||
has_played INTEGER DEFAULT 0,
|
||||
PRIMARY KEY (server_id, order_num)
|
||||
)''')
|
||||
|
||||
# Commit the changes and close the connection
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
# Queue a song in the db
|
||||
def add_song(server_id, song_link, queued_by):
|
||||
# Connect to db
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 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
|
||||
|
||||
cursor.execute("""
|
||||
INSERT INTO queue (server_id, song_link, queued_by, index)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""", (server_id, song_link, queued_by, max_order_num))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
# Add server to db if first time queuing
|
||||
def add_server(server_id, cursor):
|
||||
# Check if the server exists
|
||||
cursor.execute('''SELECT COUNT(*)
|
||||
FROM servers
|
||||
WHERE server_id = ?''', (server_id,))
|
||||
|
||||
result = cursor.fetchone()
|
||||
server_exists = result[0] > 0
|
||||
|
||||
# If the server doesn't exist, add it
|
||||
if not server_exists:
|
||||
cursor.execute('''INSERT INTO servers (server_id)
|
||||
VALUES (?)''', (server_id,))
|
||||
|
||||
|
||||
# set song as played and update indexes
|
||||
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 = ?''',
|
||||
(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()
|
||||
47
cogs/music/util.py
Normal file
47
cogs/music/util.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from discord.ext.commands.context import Context
|
||||
from discord.ext.commands.converter import CommandError
|
||||
|
||||
|
||||
# Joining/moving to the user's vc in a guild
|
||||
async def join_vc(ctx: Context):
|
||||
|
||||
# Get the user's vc
|
||||
author_voice = getattr(ctx.author, "voice")
|
||||
if author_voice is None:
|
||||
# Raise exception if user is not in vc
|
||||
raise CommandError("User is not in voice channel")
|
||||
|
||||
# Get user's vc
|
||||
vc = getattr(author_voice, "channel")
|
||||
if vc is None:
|
||||
raise CommandError("Unable to find voice channel")
|
||||
|
||||
# Join or move to the user's vc
|
||||
if ctx.voice_client is None:
|
||||
await vc.connect()
|
||||
else:
|
||||
# Safe to ignore type error for now
|
||||
await ctx.voice_client.move_to(vc)
|
||||
|
||||
|
||||
# Leaving the voice channel of a user
|
||||
async def leave_vc(ctx: Context):
|
||||
# If the bot is not in a vc of this server
|
||||
if ctx.voice_client is None:
|
||||
raise CommandError("I am not in a voice channel")
|
||||
|
||||
# if user is not in voice of the server
|
||||
author_voice = getattr(ctx.author, "voice")
|
||||
if author_voice is None:
|
||||
raise CommandError("You are not in a voice channel")
|
||||
|
||||
# Make sure both bot and User are in same vc
|
||||
vc = ctx.voice_client.channel
|
||||
author_vc = getattr(author_voice, "channel")
|
||||
if author_vc is None or vc != author_vc:
|
||||
raise CommandError("You are not in this voice channel")
|
||||
|
||||
# Disconnect
|
||||
await ctx.voice_client.disconnect(force=False)
|
||||
Reference in New Issue
Block a user