From d130a1a3c5cabd6fcb696e0c6eb933231daaabb8 Mon Sep 17 00:00:00 2001 From: yuki Date: Wed, 8 Oct 2025 22:53:38 -0300 Subject: [PATCH] restructure collage generation --- commands/collage.py | 135 +++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 63 deletions(-) diff --git a/commands/collage.py b/commands/collage.py index 777a107..60e6618 100644 --- a/commands/collage.py +++ b/commands/collage.py @@ -8,89 +8,98 @@ import utils as u # from catbox_async_uploader.catbox_async_uploader.enums import LitterboxDuration # from pyzxz import ZeroXZero -def setup(bot: bot.Bot): - @bot.command(name="chart", aliases=["c", "chartalbum", "albumchart", "collage"]) - @bot.slash_command(name="chartalbum", description="Generate an album collage.") - async def chartalbum(ctx: nerimity.Context, size: str = "3x3", timeframe: str = "7day", username: str = None): - temp_msg = None +async def create_chart(ctx: nerimity.Context, entity: str, size: str = "3x3", timeframe: str = "7day", username: str = None): + temp_msg = None - if 'x' not in size: - await ctx.send(u.error_msg("Please provide a valid size.\nie `/chartalbum 5x5`")) - return + if 'x' not in size: + await ctx.send(u.error_msg("Please provide a valid size.\nie `/chartalbum 5x5`")) + return + + if int(size.split('x')[0]) > 5 or int(size.split('x')[1]) > 5: + await ctx.send(u.error_msg("Maximum size allowed is 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 int(size.split('x')[0]) > 5 or int(size.split('x')[1]) > 5: - await ctx.send(u.error_msg("Maximum size allowed is 5x5.")) - return - if not username: - try: username = await bot.get_lastfm(ctx.author.id) + 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("Please provide a Last.fm username (or set yours with `/setfm`).")) + await ctx.send(u.error_msg(f"[@:{mentioned.id}] doesn't seem to have an account set. Do so 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}")) + match timeframe: + case "a" | "all" | "alltime" | "o": timeframe = "overall" + case "y" | "year" | "yearly": timeframe = "12month" + case "m" | "month" | "monthly": timeframe = "1month" + case "w" | "week" | "weekly": timeframe = "7day" + case "d" | "day" | "daily": timeframe = "1day" - 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 + case _: "7day" - match timeframe: - case "a" | "all" | "alltime" | "o": timeframe = "overall" - case "y" | "year" | "yearly": timeframe = "12month" - case "m" | "month" | "monthly": timeframe = "1month" - case "w" | "week" | "weekly": timeframe = "7day" - case "d" | "day" | "daily": timeframe = "1day" + if timeframe == "1day": + await ctx.send(u.error_msg("Daily charts are not yet supported. Sorry!!")) + return - case _: "7day" + try: + temp_msg = await ctx.send(f"Generating album chart for **{username}**...") - if timeframe == "1day": - await ctx.send(u.error_msg("Daily charts are not yet supported. Sorry!!")) - return + image = bot.collage_generator.generate( + entity = entity, + username = username, + rows = int(size.split(sep="x")[0]), + cols = int(size.split(sep="x")[1]), + period = timeframe + ) - try: - temp_msg = await ctx.send(f"Generating album chart for **{username}**...") + img_bytes = io.BytesIO() + image.save(fp=img_bytes, format="png") - image = bot.collage_generator.generate( - entity = "album", - username = username, - rows = int(size.split(sep="x")[0]), - cols = int(size.split(sep="x")[1]), - period = timeframe - ) + # link = await bot.catbox_uploader.upload_to_litterbox( + # file_path_or_bytes = img_bytes, + # file_name = "collage.png", + # duration = LitterboxDuration.H12 + # ) - img_bytes = io.BytesIO() - image.save(fp=img_bytes, format="png") + # attachment = await u.construct_attachment_from_bytes( + # filename = "chart", + # file_type = "png", + # bytes_arr = img_bytes, + # ).upload() - # link = await bot.catbox_uploader.upload_to_litterbox( - # file_path_or_bytes = img_bytes, - # file_name = "collage.png", - # duration = LitterboxDuration.H12 - # ) + link = await bot.zxz.upload_from_bytes(img_bytes.getvalue(), "chart.png") - # attachment = await u.construct_attachment_from_bytes( - # filename = "chart", - # file_type = "png", - # bytes_arr = img_bytes, - # ).upload() + await ctx.send(u.good_msg(f"**{size}** {timeframe} chart for [{username}](https://last.fm/user/{username}) successfully generated:\n{link}")) - link = await bot.zxz.upload_from_bytes(img_bytes.getvalue(), "chart.png") + except Exception as e: + print(e) + await ctx.send(u.error_msg(f"I crashed and burned while generating the chart:\n`{e}`")) - await ctx.send(u.good_msg(f"**{size}** {timeframe} chart for [{username}](https://last.fm/user/{username}) successfully generated:\n{link}")) + finally: + if temp_msg: temp_msg.delete() - except Exception as e: - print(e) - await ctx.send(u.error_msg(f"I crashed and burned while generating the chart:\n`{e}`")) - - finally: - if temp_msg: temp_msg.delete() +def setup(bot: bot.Bot): + @bot.command(name="chart", aliases=["c", "chartalbum", "albumchart", "collage"]) + @bot.slash_command(name="chartalbum", description="Generate an album collage.") + async def chartalbum(ctx: nerimity.Context, size: str = "3x3", timeframe: str = "7day", username: str = None): + await create_chart( + ctx = ctx, + entity = "album", + size = size, + timeframe = timeframe, + username = username + )