Yves/commands/now_playing.py
2025-10-07 03:09:42 -03:00

49 lines
1.9 KiB
Python

import nerimity
import pylast
import bot
import utils as u
def setup(bot: bot.Bot):
@bot.command(name="fm", aliases=["np"])
@bot.slash_command(name="fm", description="Shows what you're currently playing")
async def fm(ctx: nerimity.Context, lookup: str = None):
if lookup: username = lookup
else:
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 a Last.fm username (or set yours with `/setfm`)"))
else:
try:
user = bot.lastfm.get_user(username)
track = user.get_now_playing()
if track == None:
now_playing = False
played_track = user.get_recent_tracks(limit=1)
track = played_track[0].track
else:
now_playing = True
track_url = track.get_url()
track_name = track.get_name()
track_artist = track.get_artist().get_name()
track_artist_url = track.get_artist().get_url()
if now_playing == True:
await ctx.send(f"Now playing for **{username}**: [{track_name}]({track_url}) by [{track_artist}]({track_artist_url})")
else:
await ctx.send(f"Last played for **{username}**: [{track_name}]({track_url}) by [{track_artist}]({track_artist_url})")
except IndexError:
await ctx.send(u.error_msg(f"User **{username}** has no recent tracks or they're set to private."))
except pylast.WSError:
await ctx.send(u.error_msg(f"User **{username}** not found."))
except Exception as e:
print(e)
await ctx.send(u.error_msg("Unknown error."))