tidy up error returns

This commit is contained in:
yuki 2025-10-07 08:15:28 -03:00
parent 36485fbda8
commit feb10e85f3

View file

@ -17,37 +17,38 @@ def setup(bot: bot.Bot):
if not username: if not username:
await ctx.send(u.error_msg("Please provide a Last.fm username (or set yours with `/setfm`)")) await ctx.send(u.error_msg("Please provide a Last.fm username (or set yours with `/setfm`)"))
else: print("returned")
try: return
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: try:
now_playing = True user = bot.lastfm.get_user(username)
track = user.get_now_playing()
track_url = track.get_url() if track == None:
track_name = track.get_name() now_playing = False
track_artist = track.get_artist().get_name() played_track = user.get_recent_tracks(limit=1)
track_artist_url = track.get_artist().get_url() track = played_track[0].track
else: now_playing = True
if now_playing == True: track_url = track.get_url()
await ctx.send(f"Now playing for **{username}**: [{track_name}]({track_url}) by [{track_artist}]({track_artist_url})") track_name = track.get_name()
else: track_artist = track.get_artist().get_name()
await ctx.send(f"Last played for **{username}**: [{track_name}]({track_url}) by [{track_artist}]({track_artist_url})") track_artist_url = track.get_artist().get_url()
except IndexError: if now_playing == True:
await ctx.send(u.error_msg(f"User **{username}** has no recent tracks or they're set to private.")) await ctx.send(f"Now playing for **{username}**: [{track_name}]({track_url}) by [{track_artist}]({track_artist_url})")
except pylast.WSError as e: else:
print(e) await ctx.send(f"Last played for **{username}**: [{track_name}]({track_url}) by [{track_artist}]({track_artist_url})")
await ctx.send(u.error_msg(f"Last.fm error:\n`{e}`"))
except Exception as e: except IndexError:
print(e) await ctx.send(u.error_msg(f"User **{username}** has no recent tracks or they're set to private."))
await ctx.send(u.error_msg(f"Unknown error:\n`{e}`")) except pylast.WSError as e:
print(e)
await ctx.send(u.error_msg(f"Last.fm error:\n`{e}`"))
except Exception as e:
print(e)
await ctx.send(u.error_msg(f"Unknown error:\n`{e}`"))
@bot.command(name="cover", aliases=["coverart","art"]) @bot.command(name="cover", aliases=["coverart","art"])
@bot.slash_command(name="cover", description="Returns cover art of last played album or a custom query.") @bot.slash_command(name="cover", description="Returns cover art of last played album or a custom query.")
@ -59,44 +60,54 @@ def setup(bot: bot.Bot):
except Exception as e: except Exception as e:
print(e) print(e)
await ctx.send(u.error_msg("Unknown database error.")) 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: if not username:
last_played = user.get_recent_tracks(limit=1) await ctx.send(u.error_msg("Please provide an `artist | album` query or set your Last.fm username with `/setfm`."))
albumobj = last_played[0].get_album() print("returned")
else: return
albumobj = user.get_now_playing().get_album()
try:
user = bot.lastfm.get_user(username)
cover_art = albumobj.get_cover_image() if not user.get_now_playing:
last_played = user.get_recent_tracks(limit=1)
albumobj = last_played[0].get_album()
else:
albumobj = user.get_now_playing().get_album()
if not cover_art: await ctx.send(u.error_msg(f"**{albumobj.get_name()}** does not seem to have cover art.")) cover_art = albumobj.get_cover_image()
else: await ctx.send(f"Here's the cover art for **[{albumobj.get_name()}]({albumobj.get_url()})**:\
{cover_art}") if not cover_art: await ctx.send(u.error_msg(f"**{albumobj.get_name()}** does not seem to have cover art."))
else: await ctx.send(f"Here's the cover art for **[{albumobj.get_name()}]({albumobj.get_url()})**:\
{cover_art}")
except Exception as e:
print(e)
await ctx.send(u.error_msg(f"Unknown error:\n`{e}`"))
except Exception as e:
print(e)
await ctx.send(u.error_msg(f"Unknown error:\n`{e}`"))
else: else:
try: try:
if "|" not in album: if "|" not in album:
await ctx.send(u.error_msg("Please search using the format `artist | album name`.")) await ctx.send(u.error_msg("Please search using the format `artist | album name`."))
else: print("returned")
artist = ' '.join(album).split(sep="|")[0] return
albumtitle = ' '.join(album).split(sep="|")[1]
if not artist or not albumtitle: artist = ' '.join(album).split(sep="|")[0]
await ctx.send(u.error_msg("Please enter a valid artist and album name.")) albumtitle = ' '.join(album).split(sep="|")[1]
else:
albumobj = bot.lastfm.get_album(artist=artist, title=albumtitle) if not artist or not albumtitle:
cover_art = albumobj.get_cover_image() await ctx.send(u.error_msg("Please enter a valid artist and album name."))
print("returned")
if not cover_art: await ctx.send(u.error_msg(f"**{albumobj.get_name()}** does not seem to have cover art.")) return
else: await ctx.send(
f"Here's the cover art for **[{albumobj.get_name(properly_capitalized=True)}]({albumobj.get_url()})**:\ albumobj = bot.lastfm.get_album(artist=artist, title=albumtitle)
{cover_art}" cover_art = albumobj.get_cover_image()
)
if not cover_art: await ctx.send(u.error_msg(f"**{albumobj.get_name()}** does not seem to have cover art."))
else: await ctx.send(
f"Here's the cover art for **[{albumobj.get_name(properly_capitalized=True)}]({albumobj.get_url()})**:\
{cover_art}"
)
except pylast.WSError as e: except pylast.WSError as e:
print(e) print(e)