add setfm

This commit is contained in:
yuki 2025-10-07 03:09:42 -03:00
parent 6a9c1038d2
commit 4fa08e9e97
2 changed files with 56 additions and 27 deletions

18
commands/config.py Normal file
View file

@ -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."))

View file

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