import nerimity import pylast import bot import utils as u def setup(bot: bot.Bot): async def send_top(ctx: nerimity.Context, entity: str, timeframe: str, username: str, countstr: str): temp_msg = None try: count = int(countstr) except ValueError: await ctx.send(u.error_msg("Please input a valid number for `count`.")) return except Exception as e: print(e) await ctx.send(u.error_msg(f"Unknown error:\n`{e}`")) return if count > 10 or count < 1: await ctx.send(u.error_msg("Please enter a count number between 1 and 10.")) return requested = True if username else False username = await bot.find_lastfm_username(ctx, username) if not username: return match timeframe: case "a" | "all" | "alltime" | "o": timeframe = "overall" period = timeframe case "y" | "year" | "yearly": timeframe = "12month" period = "in the past year" case "m" | "month" | "monthly": timeframe = "1month" period = "in the past month" case "w" | "week" | "weekly": timeframe = "7day" period = "in the past week" case "d" | "day" | "daily": timeframe = "1day" period = "in the past day" case _: timeframe = "7day" period = "in the past week" if timeframe == "1day": await ctx.send(u.error_msg("Daily lists are not yet supported. Sorry!!")) return try: temp_msg = await ctx.send(f"Generating top {entity}s list for **{username}**...") user = bot.lastfm.get_user(username) msg_content = "" match entity: case "artist": top_list = user.get_top_artists(period=timeframe, limit=count) for index, i in enumerate(top_list): entry = f"\n{index+1}. [{i.item.get_name(properly_capitalized=True)}]({i.item.get_url()}) | {i.weight} plays" if len(msg_content) + len(entry) < 2000: msg_content += entry case "album": top_list = user.get_top_albums(period=timeframe, limit=count) for index, i in enumerate(top_list): entry = f"\n{index+1}. **{i.item.get_title(properly_capitalized=True)}** by [{i.item.get_artist().get_name(properly_capitalized=True)}]({i.item.get_artist().get_url()}) | {i.weight} plays" if len(msg_content) + len(entry) < 2000: msg_content += entry case "track": top_list = user.get_top_tracks(period=timeframe, limit=count) for index, i in enumerate(top_list): entry = f"\n{index+1}. **{i.item.get_title(properly_capitalized=True)}** by [{i.item.get_artist().get_name(properly_capitalized=True)}]({i.item.get_artist().get_url()}) | {i.weight} plays" if len(msg_content) + len(entry) < 2000: msg_content += entry msg_head = f"Top {len(top_list)} {entity}s {period} for [{username}]({user.get_url()}):\n" msg = msg_head + msg_content if len(msg) > 2000: await ctx.send(msg_head) await ctx.send(msg_content) return requested_msg = f"\n\n###### (requested by [@:{ctx.author.id}])" if requested is True and len(msg) + len(requested_msg) < 1940: msg += requested_msg await ctx.send(msg) except pylast.WSError as e: print(e) await ctx.send(u.error_msg(f"Last.fm error:\n`{e}`")) return except Exception as e: print(e) await ctx.send(u.error_msg(f"Unknown error:\n`{e}`")) return finally: if temp_msg: temp_msg.delete() @bot.command(name="topartists", aliases=["ta","topartist"]) @bot.slash_command(name="topartists", description="Generate a list of your most played artists.") async def topartists(ctx: nerimity.Context, timeframe: str = "7day", username: str = None, count: str = "10"): await send_top( ctx = ctx, entity = "artist", timeframe = timeframe, username = username, countstr = count ) @bot.command(name="topalbums", aliases=["t","top","topalbum"]) @bot.slash_command(name="topalbums", description="Generate a list of your most played albums.") async def topalbums(ctx: nerimity.Context, timeframe: str = "7day", username: str = None, count: str = "10"): await send_top( ctx = ctx, entity = "album", timeframe = timeframe, username = username, countstr = count ) @bot.command(name="toptracks", aliases=["tt","toptrack"]) @bot.slash_command(name="toptracks", description="Generate a list of your most played songs.") async def toptracks(ctx: nerimity.Context, timeframe: str = "7day", username: str = None, count: str = "10"): await send_top( ctx = ctx, entity = "track", timeframe = timeframe, username = username, countstr = count )