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