move collage method inside setup

This commit is contained in:
yuki 2025-10-09 01:21:58 -03:00
parent 6b7e38cd0f
commit a889b78efb

View file

@ -8,76 +8,76 @@ import utils as u
# from catbox_async_uploader.catbox_async_uploader.enums import LitterboxDuration # from catbox_async_uploader.catbox_async_uploader.enums import LitterboxDuration
# from pyzxz import ZeroXZero # from pyzxz import ZeroXZero
async def send_collage(bot: bot.Bot, ctx: nerimity.Context, entity: str, size: str = "3x3", timeframe: str = "7day", username: str = None): def setup(bot: bot.Bot):
temp_msg = None async def send_collage(ctx: nerimity.Context, entity: str, size: str = "3x3", timeframe: str = "7day", username: str = None):
temp_msg = None
if 'x' not in size: if 'x' not in size:
await ctx.send(u.error_msg(f"Please provide a valid size.\nie `/chart{entity} 5x5`")) await ctx.send(u.error_msg(f"Please provide a valid size.\nie `/chart{entity} 5x5`"))
return
try:
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 return
except Exception: try:
await ctx.send(u.error_msg(f"Please provide a valid size.\nie `/chart{entity} 5x5`")) if int(size.split('x')[0]) > 5 or int(size.split('x')[1]) > 5:
return await ctx.send(u.error_msg("Maximum size allowed is 5x5."))
return
username = await bot.find_lastfm_username(ctx, username)
if not username: return
match timeframe: except Exception:
case "a" | "all" | "alltime" | "o": timeframe = "overall" await ctx.send(u.error_msg(f"Please provide a valid size.\nie `/chart{entity} 5x5`"))
case "y" | "year" | "yearly": timeframe = "12month" return
case "m" | "month" | "monthly": timeframe = "1month"
case "w" | "week" | "weekly": timeframe = "7day" username = await bot.find_lastfm_username(ctx, username)
case "d" | "day" | "daily": timeframe = "1day" if not username: 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": case _: "7day"
await ctx.send(u.error_msg("Daily charts are not yet supported. Sorry!!"))
return
try: if timeframe == "1day":
temp_msg = await ctx.send(f"Generating {entity} chart for **{username}**...") await ctx.send(u.error_msg("Daily charts are not yet supported. Sorry!!"))
return
image = bot.collage_generator.generate( try:
entity = entity, temp_msg = await ctx.send(f"Generating {entity} chart for **{username}**...")
username = username,
rows = int(size.split(sep="x")[0]),
cols = int(size.split(sep="x")[1]),
period = timeframe
)
img_bytes = io.BytesIO() image = bot.collage_generator.generate(
image.save(fp=img_bytes, format="png") entity = entity,
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( img_bytes = io.BytesIO()
# file_path_or_bytes = img_bytes, image.save(fp=img_bytes, format="png")
# file_name = "collage.png",
# duration = LitterboxDuration.H12
# )
# attachment = await u.construct_attachment_from_bytes( # link = await bot.catbox_uploader.upload_to_litterbox(
# filename = "chart", # file_path_or_bytes = img_bytes,
# file_type = "png", # file_name = "collage.png",
# bytes_arr = img_bytes, # duration = LitterboxDuration.H12
# ).upload() # )
link = await bot.zxz.upload_from_bytes(img_bytes.getvalue(), f"{entity}_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} {entity} chart for [{username}](https://last.fm/user/{username}) successfully generated:\n{link}")) link = await bot.zxz.upload_from_bytes(img_bytes.getvalue(), f"{entity}_chart.png")
except Exception as e: await ctx.send(u.good_msg(f"**{size}** {timeframe} {entity} chart for [{username}](https://last.fm/user/{username}) successfully generated:\n{link}"))
print(e)
await ctx.send(u.error_msg(f"I crashed and burned while generating the chart:\n`{e}`"))
finally: except Exception as e:
if temp_msg: temp_msg.delete() 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.command(name="chart", aliases=["c", "chartalbum", "albumchart", "collage"])
@bot.slash_command(name="chartalbum", description="Generate an album 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): async def chartalbum(ctx: nerimity.Context, size: str = "3x3", timeframe: str = "7day", username: str = None):