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

Commit

Permalink
song name implemented :)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmattbtw committed Jul 18, 2021
1 parent 6bbb530 commit 317d8fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@
5. run `tajjbot.py`

## how to use it
1. find a spotify "URI"
1. search for your song on spotify
2. hold control, right click the song, then click "Copy URI"
3. !sr <uri>
1. !sr {song name}
36 changes: 28 additions & 8 deletions tajjbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import dotenv

import json
import re

URL_REGEX = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"

with open("config.json") as config_file:
config = json.load(config_file)
Expand Down Expand Up @@ -42,14 +45,31 @@ async def test_command(self, ctx):
await ctx.send(f"FeelsDankMan 🔔 ding @{ctx.author.name} sr bot online")

@commands.command(name="songrequest", aliases=["sr", "addsong"])
async def songrequest_command(self, ctx, *, song_uri: str):
QUEUE_URL = f"https://api.spotify.com/v1/me/player/queue?uri={song_uri}"

async with request("POST", QUEUE_URL, headers={"Content-Type": "application/json" , "Authorization": f"Bearer {os.environ.get('SPOTIFY_AUTH')}"}) as resp:
if resp.status == 204:
await ctx.send(f"@{ctx.author.name}, your song has been added!")
else:
await ctx.send(f"Error: {resp.status}")
async def songrequest_command(self, ctx, *, song_name: str):
GET_SONG_URL = f"https://api.spotify.com/v1/search?q={song_name}&type=track&market=US"

if re.match(URL_REGEX, song_name):
await ctx.send("Please send a song name instead!")

else:
song_uri = "not found"
async with request("GET", GET_SONG_URL, headers={"Content-Type": "application/json" , "Authorization": f"Bearer {os.environ.get('SPOTIFY_AUTH')}"}) as resp:
data = await resp.json()
if resp.status == 200:
song_uri = data['tracks']['items'][0]['uri']

else:
print(data)
await ctx.send("Couldn't find that song :/")

QUEUE_URL = f"https://api.spotify.com/v1/me/player/queue?uri={song_uri}"

if song_uri != "not found":
async with request("POST", QUEUE_URL, headers={"Content-Type": "application/json" , "Authorization": f"Bearer {os.environ.get('SPOTIFY_AUTH')}"}) as resp:
if resp.status == 204:
await ctx.send(f"@{ctx.author.name}, your song ({data['tracks']['items'][0]['name']}) has been added!")
else:
await ctx.send(f"Error: {resp.status}")



Expand Down

0 comments on commit 317d8fb

Please sign in to comment.