Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

WIP - ✨v1.2.5 #9

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
config.json
venv
.cache
__pycache__
build/
dist/
bot.spec
74 changes: 72 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@

import json

import time

import re

from threading import Timer

import spotipy
from spotipy.oauth2 import SpotifyOAuth

Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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()