import { ButtonInteraction, MessageFlags } from "discord.js"; import { inventoryService } from "@/modules/inventory/inventory.service"; import { userService } from "@/modules/user/user.service"; import { createErrorEmbed, createWarningEmbed } from "@/lib/embeds"; export async function handleShopInteraction(interaction: ButtonInteraction) { if (!interaction.customId.startsWith("shop_buy_")) return; try { await interaction.deferReply({ flags: MessageFlags.Ephemeral }); const itemId = parseInt(interaction.customId.replace("shop_buy_", "")); if (isNaN(itemId)) { await interaction.editReply({ embeds: [createErrorEmbed("Invalid Item ID.")] }); return; } const item = await inventoryService.getItem(itemId); if (!item || !item.price) { await interaction.editReply({ embeds: [createErrorEmbed("Item not found or not for sale.")] }); return; } const user = await userService.getOrCreateUser(interaction.user.id, interaction.user.username); // Double check balance here too, although service handles it, we want a nice message if ((user.balance ?? 0n) < item.price) { await interaction.editReply({ embeds: [createWarningEmbed(`You need ${item.price} 🪙 to buy this item. You have ${user.balance} 🪙.`)] }); return; } const result = await inventoryService.buyItem(user.id, item.id, 1n); await interaction.editReply({ content: `✅ **Success!** You bought **${item.name}** for ${item.price} 🪙.` }); } catch (error: any) { console.error("Shop Purchase Error:", error); await interaction.editReply({ embeds: [createErrorEmbed(error.message || "An error occurred while processing your purchase.")] }); } }