diff --git a/.gitignore b/.gitignore index a8e1600..4518dce 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,7 @@ config.json venv .cache +__pycache__ +build/ +dist/ +bot.spec \ No newline at end of file diff --git a/bot.py b/bot.py index d65952c..f99c91a 100644 --- a/bot.py +++ b/bot.py @@ -12,8 +12,12 @@ import json +import time + import re +from threading import Timer + import spotipy from spotipy.oauth2 import SpotifyOAuth @@ -49,7 +53,8 @@ def __init__(self): ) self.token = os.environ.get("SPOTIFY_AUTH") - self.version = "1.2.3" + self.version = "1.2.4" + self.queue = [] async def event_ready(self): print("\n" * 100) @@ -69,6 +74,9 @@ async def ping_command(self, ctx): @commands.command(name="np", aliases=["nowplaying", "song"]) async def np_command(self, ctx): + await self.np(ctx) + + async def np(ctx): data = sp.currently_playing() song_artists = data["item"]["artists"] song_artists_names = [artist["name"] for artist in song_artists] @@ -85,6 +93,33 @@ async def np_command(self, ctx): f"🎶Now Playing - {data['item']['name']} by {', '.join(song_artists_names)} | Link: {data['item']['external_urls']['spotify']} | {time_through} - {time_total}" ) + def np_song_length(self): + data = sp.currently_playing() + ms = data["item"]["duration_ms"] + ms_through = data["progress_ms"] + + subtract = ms - ms_through + + return subtract / 1000 + + @commands.command(name="queue") + async def queue_command(self, ctx): + if self.queue: + await ctx.send(f"🎶The queue contains: {', '.join(self.queue)}") + else: + await ctx.send(f"🎶The queue is empty!") + + @commands.command(name="clearqueue") + async def clearqueue_command(self, ctx): + if ctx.author.is_mod: + if self.queue != []: + self.queue.clear() + await ctx.send(f"🎶The queue has been cleared!") + else: + await ctx.send(f"🎶The queue is already empty!") + else: + await ctx.send(f"🎶You don't have permission to do that!") + @commands.command(name="songrequest", aliases=["sr", "addsong"]) async def songrequest_command(self, ctx, *, song: str): song_uri = None @@ -134,11 +169,46 @@ async def song_request(self, ctx, song, song_uri, album: bool): song_artists_names = [artist["name"] for artist in song_artists] if song_uri != "not found": - sp.add_to_queue(song_uri) + # sp.add_to_queue(song_uri) + self.queue.append( + f"{song_name} by {', '.join(song_artists_names)} [{ctx.author.name}]" + ) + + await self.queue_up_song(ctx, song_uri) + await ctx.send( f"Your song ({song_name} by {', '.join(song_artists_names)}) [ {data['external_urls']['spotify']} ] has been added to {ctx.channel.name}'s queue!" ) + async def queue_up_song(self, ctx, song_uri): + song_id = song_uri.replace("spotify:track:", "") + + data = sp.track(song_id) + song_name = data["name"] + song_artists = data["artists"] + song_artists_names = [artist["name"] for artist in song_artists] + seconds = self.np_song_length() + queue_str = ( + f"{song_name} by {', '.join(song_artists_names)} [{ctx.author.name}]" + ) + + if queue_str == self.queue[0]: + print(self.queue[0]) + print(queue_str) + sp.add_to_queue(song_uri) + sp.next_track() + + else: + t = Timer(seconds, self.add_to_queue(song_uri)) + t.start() + + def add_to_queue(self, song_uri): + sp.add_to_queue(song_uri) + + # self.queue.remove( + # f"{song_name} by {', '.join(song_artists_names)} [{ctx.author.name}]" + # ) + bot = Bot() bot.run()