import { ActionRowBuilder, ButtonBuilder, ButtonStyle, AttachmentBuilder } from "discord.js"; import { createBaseEmbed } from "@/lib/embeds"; import { resolveAssetUrl, isLocalAssetUrl } from "@shared/lib/assets"; import { join } from "path"; import { existsSync } from "fs"; export function getShopListingMessage(item: { id: number; name: string; description: string | null; formattedPrice: string; iconUrl: string | null; imageUrl: string | null; price: number | bigint }) { const files: AttachmentBuilder[] = []; let thumbnailUrl = resolveAssetUrl(item.iconUrl); let displayImageUrl = resolveAssetUrl(item.imageUrl); // Handle local icon if (item.iconUrl && isLocalAssetUrl(item.iconUrl)) { const iconPath = join(process.cwd(), "bot/assets/graphics", item.iconUrl.replace(/^\/?assets\//, "")); if (existsSync(iconPath)) { const iconName = defaultName(item.iconUrl); files.push(new AttachmentBuilder(iconPath, { name: iconName })); thumbnailUrl = `attachment://${iconName}`; } } // Handle local image (avoid duplicate attachments if same as icon) if (item.imageUrl && isLocalAssetUrl(item.imageUrl)) { // If image is same as icon, just use the same attachment reference if (item.imageUrl === item.iconUrl && thumbnailUrl?.startsWith("attachment://")) { displayImageUrl = thumbnailUrl; } else { const imagePath = join(process.cwd(), "bot/assets/graphics", item.imageUrl.replace(/^\/?assets\//, "")); if (existsSync(imagePath)) { const imageName = defaultName(item.imageUrl); // Check if we already attached this file (by name) if (!files.find(f => f.name === imageName)) { files.push(new AttachmentBuilder(imagePath, { name: imageName })); } displayImageUrl = `attachment://${imageName}`; } } } const embed = createBaseEmbed(`Shop: ${item.name}`, item.description || "No description available.", "Green") .addFields({ name: "Price", value: item.formattedPrice, inline: true }) .setThumbnail(thumbnailUrl) .setImage(displayImageUrl) .setFooter({ text: "Click the button below to purchase instantly." }); const buyButton = new ButtonBuilder() .setCustomId(`shop_buy_${item.id}`) .setLabel(`Buy for ${item.price} 🪙`) .setStyle(ButtonStyle.Success) .setEmoji("🛒"); const row = new ActionRowBuilder().addComponents(buyButton); return { embeds: [embed], components: [row], files }; } function defaultName(path: string): string { return path.split("/").pop() || "image.png"; }