1
0
Fork 0
mirror of synced 2024-05-18 19:42:28 +12:00

Don't make command server only

This commit is contained in:
phxntxm 2018-05-07 10:02:02 -05:00
parent 6145ab3df3
commit 9e39888589

View file

@ -1,81 +1,79 @@
import asyncio import asyncio
import aiohttp import aiohttp
from discord.ext import commands from discord.ext import commands
from base64 import urlsafe_b64encode from base64 import urlsafe_b64encode
from . import utils from . import utils
class Spotify: class Spotify:
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self._token = None self._token = None
self._client_id = utils.spotify_id or "" self._client_id = utils.spotify_id or ""
self._client_secret = utils.spotify_secret or "" self._client_secret = utils.spotify_secret or ""
self._authorization = "{}:{}".format(self._client_id, self._client_secret) self._authorization = "{}:{}".format(self._client_id, self._client_secret)
self.headers = { self.headers = {
"Authorization": "Basic {}".format( "Authorization": "Basic {}".format(
urlsafe_b64encode(self._authorization.encode()).decode() urlsafe_b64encode(self._authorization.encode()).decode()
) )
} }
self.bot.loop.create_task(self.api_token_task()) self.bot.loop.create_task(self.api_token_task())
async def api_token_task(self): async def api_token_task(self):
url = "https://accounts.spotify.com/api/token" url = "https://accounts.spotify.com/api/token"
opts = {"grant_type": "client_credentials"} opts = {"grant_type": "client_credentials"}
while True: while True:
with aiohttp.ClientSession(headers=self.headers) as session: with aiohttp.ClientSession(headers=self.headers) as session:
response = await session.post(url, data=opts) response = await session.post(url, data=opts)
data = await response.json() data = await response.json()
self._token = data.get("access_token") self._token = data.get("access_token")
await asyncio.sleep(data.get("expires_in", 2400)) await asyncio.sleep(data.get("expires_in", 2400))
@commands.group(invoke_without_command=True) @commands.group(invoke_without_command=True)
@commands.guild_only() @utils.custom_perms(send_messages=True)
@utils.custom_perms(send_messages=True) @utils.check_restricted()
@utils.check_restricted() async def spotify(self, ctx, *, query):
async def spotify(self, ctx, *, query): """Searches Spotify for a song, giving you the link you can use to listen in. Give the query to search for
"""Searches Spotify for a song, giving you the link you can use to listen in. Give the query to search for and it will search by title/artist for the best match
and it will search by title/artist for the best match
EXAMPLE: !spotify Eminem
EXAMPLE: !spotify Eminem RESULT: Some Eminem song"""
RESULT: Some Eminem song"""
# Setup the headers with the token that should be here
# Setup the headers with the token that should be here headers = {"Authorization": "Bearer {}".format(self._token)}
headers = {"Authorization": "Bearer {}".format(self._token)} opts = {"q": query, "type": "track"}
opts = {"q": query, "type": "track"} url = "https://api.spotify.com/v1/search"
url = "https://api.spotify.com/v1/search" response = await utils.request(url, headers=headers, payload=opts)
response = await utils.request(url, headers=headers, payload=opts) try:
try: await ctx.send(response.get("tracks").get("items")[0].get("external_urls").get("spotify"))
await ctx.send(response.get("tracks").get("items")[0].get("external_urls").get("spotify")) except (KeyError, AttributeError, IndexError):
except (KeyError, AttributeError, IndexError): await ctx.send("Couldn't find a song for:\n{}".format(query))
await ctx.send("Couldn't find a song for:\n{}".format(query))
@spotify.command()
@spotify.command() @utils.custom_perms(send_messages=True)
@commands.guild_only() @utils.check_restricted()
@utils.custom_perms(send_messages=True) async def playlist(self, ctx, *, query):
@utils.check_restricted() """Searches Spotify for a playlist, giving you the link you can use to listen in. Give the query to search for
async def playlist(self, ctx, *, query): and it will search for the best match
"""Searches Spotify for a playlist, giving you the link you can use to listen in. Give the query to search for
and it will search for the best match EXAMPLE: !spotify Eminem
RESULT: Some Eminem song"""
EXAMPLE: !spotify Eminem # Setup the headers with the token that should be here
RESULT: Some Eminem song""" headers = {"Authorization": "Bearer {}".format(self._token)}
# Setup the headers with the token that should be here opts = {"q": query, "type": "playlist"}
headers = {"Authorization": "Bearer {}".format(self._token)} url = "https://api.spotify.com/v1/search"
opts = {"q": query, "type": "playlist"} response = await utils.request(url, headers=headers, payload=opts)
url = "https://api.spotify.com/v1/search" try:
response = await utils.request(url, headers=headers, payload=opts) await ctx.send(response.get("playlists").get("items")[0].get("external_urls").get("spotify"))
try: except (KeyError, AttributeError, IndexError):
await ctx.send(response.get("playlists").get("items")[0].get("external_urls").get("spotify")) await ctx.send("Couldn't find a song for:\n{}".format(query))
except (KeyError, AttributeError):
await ctx.send("Couldn't find a song for:\n{}".format(query))
def setup(bot):
bot.add_cog(Spotify(bot))
def setup(bot):
bot.add_cog(Spotify(bot))