diff --git a/commands/now_playing.py b/commands/now_playing.py index bf3750d..325f720 100644 --- a/commands/now_playing.py +++ b/commands/now_playing.py @@ -1,3 +1,5 @@ +import requests + import nerimity import pylast @@ -47,3 +49,33 @@ def setup(bot: bot.Bot): except Exception as e: print(e) await ctx.send(u.error_msg("Unknown error.")) + + @bot.command(name="cover", aliases=["coverart","art"]) + @bot.slash_command(name="cover", description="Returns cover art of last played album or a custom query.") + async def cover(ctx: nerimity.Context, *album: str): + username = None + user = None + if not album: + try: username = await bot.get_lastfm(ctx.author.id) + except Exception as e: + print(e) + await ctx.send(u.error_msg("Unknown database error.")) + if not username: await ctx.send(u.error_msg("Please provide an `artist - album` query or set your Last.fm username with `/setfm`.")) + else: + try: + user = bot.lastfm.get_user(username) + + if not user.get_now_playing: + last_played = user.get_recent_tracks(limit=1) + album = last_played[0].get_album() + else: + album = user.get_now_playing().get_album() + + cover_art = album.get_cover_image() + + if not cover_art: await ctx.send(u.error_msg(f"**{album.get_name()}** does not seem to have cover art.")) + else: await ctx.send(f"Here's the cover art for **[{album.get_name()}]({album.get_url()})**:\n{cover_art}") + + except Exception as e: + print(e) + await ctx.send(u.error_msg("Unknown error.")) diff --git a/utils.py b/utils.py index 4437ab8..401ff68 100644 --- a/utils.py +++ b/utils.py @@ -1,3 +1,22 @@ +import requests +import mimetypes + +import nerimity + +# def construct_attachment_from_url(url: str = None): +# if not url: return None +# content = requests.get(url).content + +# attachment = nerimity.Attachment() +# attachment.internal_type = nerimity.AttachmentTypes.OUTGOING + +# attachment.data = content +# attachment.data_type, _ = mimetypes.guess_type(url=url) +# attachment.size = len(attachment.data) +# attachment.name = url.split("/")[-1] + +# return attachment + def error_msg(message: str): return f"[#e5323b][Error] [#reset]{message}"