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:
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
print("returned")
return
else:
now_playing = True
try:
user = bot.lastfm.get_user(username)
track = user.get_now_playing()
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 track == None:
now_playing = False
played_track = user.get_recent_tracks(limit=1)
track = played_track[0].track
else: now_playing = True
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})")
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()
except IndexError:
await ctx.send(u.error_msg(f"User **{username}** has no recent tracks or they're set to private."))
except pylast.WSError as e:
print(e)
await ctx.send(u.error_msg(f"Last.fm error:\n`{e}`"))
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 Exception as e:
print(e)
await ctx.send(u.error_msg(f"Unknown error:\n`{e}`"))
except IndexError:
await ctx.send(u.error_msg(f"User **{username}** has no recent tracks or they're set to private."))
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.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:
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)
albumobj = last_played[0].get_album()
else:
albumobj = user.get_now_playing().get_album()
if not username:
await ctx.send(u.error_msg("Please provide an `artist | album` query or set your Last.fm username with `/setfm`."))
print("returned")
return
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."))
else: await ctx.send(f"Here's the cover art for **[{albumobj.get_name()}]({albumobj.get_url()})**:\
{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()}]({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:
try:
if "|" not in album:
await ctx.send(u.error_msg("Please search using the format `artist | album name`."))
else:
artist = ' '.join(album).split(sep="|")[0]
albumtitle = ' '.join(album).split(sep="|")[1]
if not artist or not albumtitle:
await ctx.send(u.error_msg("Please enter a valid artist and album name."))
else:
albumobj = bot.lastfm.get_album(artist=artist, title=albumtitle)
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}"
)
print("returned")
return
artist = ' '.join(album).split(sep="|")[0]
albumtitle = ' '.join(album).split(sep="|")[1]
if not artist or not albumtitle:
await ctx.send(u.error_msg("Please enter a valid artist and album name."))
print("returned")
return
albumobj = bot.lastfm.get_album(artist=artist, title=albumtitle)
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:
print(e)