🟢 spotify playlists now working

This commit is contained in:
2023-11-10 14:22:12 +00:00
parent 77e2175ab6
commit a40679ee11
5 changed files with 45 additions and 26 deletions

View File

@@ -7,7 +7,6 @@ import cogs.music.translate as translate
from cogs.music.help import music_help
import discord
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
@@ -39,16 +38,6 @@ def get_spotify_creds():
# call play on ffmpeg exit
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.create_task(play(self.ctx))
class music(commands.Cog):
def __init__(self, client):
self.client = client

View File

@@ -278,6 +278,15 @@ async def grab_songs(server_id):
return max, songs
# call play on ffmpeg exit
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))
# Play and loop songs in server
async def play(ctx):
@@ -297,14 +306,4 @@ async def play(ctx):
# else play next song and call play again
await ctx.voice_client.play(
AstroPlayer(ctx, url, FFMPEG_OPTS))
# call play on ffmpeg exit
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))
AstroPlayer(ctx, url, FFMPEG_OPTS))

View File

@@ -23,7 +23,7 @@ def main(url, sp):
if 'track' in url:
return spotify_song(url, sp)
elif 'playlist' in url:
return spotify_playlist(url)
return spotify_playlist(url, sp)
soundcloud_song = 'soundcloud' in url and 'sets' not in url
# Not implemented yet
@@ -75,8 +75,38 @@ def spotify_song(url, sp):
return search_song(query)
def spotify_playlist(url):
return []
def spotify_playlist(url, sp):
# Get the playlist uri code
code = url.split("/")[-1].split("?")[0]
# Grab the tracks if the playlist is correct
try:
results = sp.playlist_tracks(code)['items']
except spotipy.exceptions.SpotifyException:
return []
# Go through the tracks
songs = []
for track in results:
search = ""
# Fetch all artists
for artist in track['track']['artists']:
# Add all artists to search
search += f"{artist['name']}, "
# Remove last column
search = search[:-2]
search += f" - {track['track']['name']}"
searched_result = search_song(search)
if searched_result == []:
continue
songs.append(searched_result[0])
return songs
def song_download(url):

View File

@@ -69,7 +69,7 @@ async def display_server_queue(ctx: Context, songs, n):
title=f"{server.name}'s Queue!",
color=config.get_color("main"))
display = f"🔊 Currently playing: ``{await queue.get_current_song(ctx.guild.id)}``\n"
display = f"🔊 Currently playing: ``{await queue.get_current_song(ctx.guild.id)}``\n\n"
for i, song in enumerate(songs):
display += f"``{i + 1}.`` {song[0]} - {format_time(song[1])} Queued by {song[2]}\n"
msg.add_field(name="Songs:",