add username finder function

This commit is contained in:
yuki 2025-10-09 01:07:37 -03:00
parent 0d7271eee4
commit 6b7e38cd0f
4 changed files with 28 additions and 43 deletions

View file

@ -9,10 +9,11 @@ dead simple last.fm bot
(i cannot promise stable uptime)
**TODO:**
- add database username checker to utils.py
- ~~add database username checker to utils.py~~ ✅ (added to bot.py)
- ~~add collage generation~~
- fix attachment generation and upload
- ~~**temporary fix**: upload and link to catbox urls~~ __the asynchronous catbox library does not accept byte arrays__
- ~~**temporary fix**: construct the attachment object from scratch and upload manually(?)~~ __its still broken__
- ~~**temporary fix:** use 0x0.st~~
- **permanent fix**: fork and update nerimity library
- add list generation

22
bot.py
View file

@ -90,3 +90,25 @@ class Bot(nerimity.Client):
) as cursor:
row = await cursor.fetchone()
return row[0] if row else None
async def find_lastfm_username(self, ctx: nerimity.Context, username: str = None):
if not username:
try: username = await self.get_lastfm(ctx.author.id)
except Exception as e:
print(e)
await ctx.send(u.error_msg(f"Unknown database error:\n{e}"))
if not username:
await ctx.send(u.error_msg("Please provide a Last.fm username (or set yours with `/setfm`)."))
elif username.startswith("[@:"):
mentioned = self.get_user(username[3:-1])
try: username = await self.get_lastfm(mentioned.id)
except Exception as e:
print(e)
await ctx.send(u.error_msg(f"Unknown database error:\n{e}"))
if not username:
await ctx.send(u.error_msg(f"[@:{mentioned.id}] doesn't seem to have an account set. Do so with `/setfm`."))
return username

View file

@ -24,28 +24,8 @@ async def send_collage(bot: bot.Bot, ctx: nerimity.Context, entity: str, size: s
await ctx.send(u.error_msg(f"Please provide a valid size.\nie `/chart{entity} 5x5`"))
return
if not username:
try: username = await bot.get_lastfm(ctx.author.id)
except Exception as e:
print(e)
await ctx.send(u.error_msg(f"Unknown database error:\n{e}"))
if not username:
await ctx.send(u.error_msg("Please provide a Last.fm username (or set yours with `/setfm`)."))
print("returned")
return
if username.startswith("[@:"):
mentioned = bot.get_user(username[3:-1])
try: username = await bot.get_lastfm(mentioned.id)
except Exception as e:
print(e)
await ctx.send(u.error_msg(f"Unknown database error:\n{e}"))
if not username:
await ctx.send(u.error_msg(f"[@:{mentioned.id}] doesn't seem to have an account set. Do so with `/setfm`."))
print("returned")
return
username = await bot.find_lastfm_username(ctx, username)
if not username: return
match timeframe:
case "a" | "all" | "alltime" | "o": timeframe = "overall"

View file

@ -8,26 +8,8 @@ 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, username: str = None):
if not username:
try: username = await bot.get_lastfm(ctx.author.id)
except Exception as e:
await ctx.send(u.error_msg(f"Unknown database error:\n{e}"))
if not username:
await ctx.send(u.error_msg("Please provide a Last.fm username (or set yours with `/setfm`)"))
return
if username.startswith("[@:"):
mentioned = bot.get_user(username[3:-1])
try: username = await bot.get_lastfm(mentioned.id)
except Exception as e:
print(e)
await ctx.send(u.error_msg(f"Unknown database error:\n{e}"))
if not username:
await ctx.send(u.error_msg(f"[@:{mentioned.id}] doesn't seem to have an account set. Do so with `/setfm`."))
print("returned")
return
username = await bot.find_lastfm_username(ctx, username)
if not username: return
try:
user = bot.lastfm.get_user(username)