diff --git a/README.md b/README.md index fb8d807..6fda80d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bot.py b/bot.py index 105a27e..e70e66e 100644 --- a/bot.py +++ b/bot.py @@ -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 diff --git a/commands/collage.py b/commands/collage.py index d481c68..e8a721e 100644 --- a/commands/collage.py +++ b/commands/collage.py @@ -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" diff --git a/commands/now_playing.py b/commands/now_playing.py index a7ffc24..2b8d38b 100644 --- a/commands/now_playing.py +++ b/commands/now_playing.py @@ -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)