diff --git a/extensions/utility/__init__.py b/extensions/utility/__init__.py index b64de3f..b6df703 100644 --- a/extensions/utility/__init__.py +++ b/extensions/utility/__init__.py @@ -5,9 +5,10 @@ from .source import SourceCog from .embed import EmbedCog from .download import DownloadCog +from .spotify import SpotifyCog -class Utility(ConfigCog, InfoCog, NeofetchCog, PingCog, SourceCog, EmbedCog, DownloadCog): +class Utility(ConfigCog, InfoCog, NeofetchCog, PingCog, SourceCog, EmbedCog, DownloadCog, SpotifyCog): pass diff --git a/extensions/utility/spotify.py b/extensions/utility/spotify.py new file mode 100644 index 0000000..b897c7c --- /dev/null +++ b/extensions/utility/spotify.py @@ -0,0 +1,37 @@ +import discord +from discord.ext import commands + +from bot import Korii +import config +from io import BytesIO + + +class SpotifyCog(commands.Cog): + def __init__(self, bot: Korii): + self.bot = bot + + @commands.hybrid_command() + async def spotify(self, ctx: commands.Context, member: discord.Member = commands.Author): + await ctx.typing() + + spotify: discord.Spotify | None = discord.utils.find( # type: ignore + lambda activity: isinstance(activity, discord.Spotify), member.activities + ) + + if not spotify: + return await ctx.send("❌ | no spotify activity found") + + headers = {"Authorization": f"Bearer {config.JEYY_API_TOKEN}"} + parameters = { + "title": spotify.title, + "cover_url": spotify.album_cover_url, + "duration_seconds": spotify.duration.seconds, + "start_timestamp": spotify.start.timestamp(), + "artists": spotify.artists, + } + + request = await self.bot.session.get("https://api.jeyy.xyz/v2/discord/spotify", parameters=parameters, headers=headers) + bytes = BytesIO(await request.read()) + file = discord.File(bytes, "spotify.png") + + return await ctx.send(f"<:spotify:1169757922532794368> **{member.display_name}** is listening to **{spotify.title}**", file=file)