From 66c9bdada02dc91033ddfb282c582c3d0ca55a42 Mon Sep 17 00:00:00 2001 From: yuki Date: Thu, 9 Oct 2025 04:58:22 -0300 Subject: [PATCH] account for exceeding max number of characters in message --- commands/collage.py | 2 +- commands/top.py | 43 +++++++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/commands/collage.py b/commands/collage.py index 6c03ef5..15593e3 100644 --- a/commands/collage.py +++ b/commands/collage.py @@ -9,7 +9,7 @@ import utils as u # from pyzxz import ZeroXZero def setup(bot: bot.Bot): - async def send_collage(ctx: nerimity.Context, entity: str, size: str = "3x3", timeframe: str = "7day", username: str = None): + async def send_collage(ctx: nerimity.Context, entity: str, size: str, timeframe: str, username: str): temp_msg = None if 'x' not in size: diff --git a/commands/top.py b/commands/top.py index b615b50..411b377 100644 --- a/commands/top.py +++ b/commands/top.py @@ -6,6 +6,8 @@ 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`.")) @@ -47,9 +49,11 @@ def setup(bot: bot.Bot): case _: timeframe = "7day" - period = "in the past week (default)" + period = "in the past week" try: + temp_msg = await ctx.send(f"Generating top {entity}s list for **{username}**...") + user = bot.lastfm.get_user(username) msg_content = "" @@ -57,23 +61,35 @@ def setup(bot: bot.Bot): case "artist": top_list = user.get_top_artists(period=timeframe, limit=count) for index, i in enumerate(top_list): - msg_content += f"\n{index+1}. [{i.item.get_name(properly_capitalized=True)}]({i.item.get_url()}) | {i.weight} plays" + 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): - msg_content += f"\n{index+1}. [{i.item.get_title(properly_capitalized=True)}]({i.item.get_url()}) by [{i.item.get_artist().get_name(properly_capitalized=True)}]({i.item.get_artist().get_url()}) | {i.weight} plays" + 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): - msg_content += f"\n{index+1}. [{i.item.get_title(properly_capitalized=True)}]({i.item.get_url()}) by [{i.item.get_artist().get_name(properly_capitalized=True)}]({i.item.get_artist().get_url()}) | {i.weight} plays" + 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 requested is True: - msg += f"\n### Requested by [@:{ctx.author.id}]" + 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) @@ -86,35 +102,38 @@ def setup(bot: bot.Bot): 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, countstr: str = "10"): + 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 = countstr + 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, countstr: str = "10"): + 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 = countstr + 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, countstr: str = "10"): + 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 = countstr + countstr = count )