diff --git a/commands/config.py b/commands/config.py new file mode 100644 index 0000000..968892c --- /dev/null +++ b/commands/config.py @@ -0,0 +1,18 @@ +import nerimity + +import bot +import utils as u + +def setup(bot: bot.Bot): + @bot.command(name="setfm", aliases=["setuser", "setlastfm"]) + @bot.slash_command(name="setfm", description="Sets your Last.fm username.") + async def setfm(ctx: nerimity.Context, username: str = None): + if not username: + await ctx.send(u.error_msg("Please provide your Last.fm username.")) + else: + try: + await bot.set_lastfm(ctx.author.id, username) + await ctx.send(u.good_msg(f"Your Last.fm user has been set to **{username}**!")) + except Exception as e: + print(e) + await ctx.send(u.error_msg("Unknown database error.")) diff --git a/commands/now_playing.py b/commands/now_playing.py index 282f15b..bf3750d 100644 --- a/commands/now_playing.py +++ b/commands/now_playing.py @@ -1,38 +1,49 @@ import nerimity import pylast -import commands.utils as u +import bot +import utils as u -def setup(bot): +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, params: str = ""): - username = params if params else "kaaisudev" + 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.")) - try: - track = bot.lastfm.get_user(username).get_now_playing() - if track == None: - now_playing = False - played_track = bot.lastfm.get_user(username).get_recent_tracks(limit=1) - track = played_track[0].track + 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 + 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() + 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})") + 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("User has no recent tracks or they're set to private.")) - except pylast.WSError: - await ctx.send(u.error_msg("User not found.")) - except Exception as e: - print(e) - await ctx.send(u.error_msg("Unknown error.")) + 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."))