import { levels } from "@prisma/client"; import { AttachmentBuilder } from "discord.js"; import nodeHtmlToImage from "node-html-to-image"; import { ExtendedClient } from "../../interfaces/ExtendedClient"; import { errorHandler } from "../../utils/errorHandler"; /** * Creates an image from the user's profile settings, converts it into a Discord * attachment, and returns it. * * @param {ExtendedClient} CamperChan The CamperChan's Discord instance. * @param {levels} record The user's record from the database. * @returns {AttachmentBuilder} The attachment, or null on error. */ export const generateProfileImage = async ( CamperChan: ExtendedClient, record: levels ): Promise => { try { const { avatar, backgroundColour, backgroundImage, colour, username, level, points } = record; const html = `

${username}

Level ${level} (${points.toLocaleString("en-GB")}xp)

`; const alt = `${username} is at level ${level} with ${points.toLocaleString("en-GB")} experience points.`; const image = await nodeHtmlToImage({ html, selector: "body", transparent: true }); if (!(image instanceof Buffer)) { return null; } const attachment = new AttachmentBuilder(image, { name: `${username}.png`, description: alt }); return attachment; } catch (err) { await errorHandler(CamperChan, "generate profile image module", err); return null; } }; /** * Generates the image for the leaderboard. * * @param {ExtendedClient} CamperChan The CamperChan's Discord instance. * @param {levels} levels The user's record from the database. * @returns {AttachmentBuilder} The attachment, or null on error. */ export const generateLeaderboardImage = async ( CamperChan: ExtendedClient, levels: (levels & { index: number })[] ): Promise => { try { const html = ` ${levels.map( (l) => `

#${l.index}. ${l.username}

Level ${l.level} (${l.points}xp)

` )} `; const alt = levels .map( (l) => `${l.username} is rank ${l.index} at ${l.level} with ${l.points.toLocaleString("en-GB")} experience points.` ) .join(", "); const image = await nodeHtmlToImage({ html, selector: "body", transparent: true }); if (!(image instanceof Buffer)) { return null; } const attachment = new AttachmentBuilder(image, { name: `leaderboard-${levels[0]?.index}.png`, description: alt }); return attachment; } catch (err) { await errorHandler(CamperChan, "generate leaderboard image module", err); return null; } };