diff --git a/src/commands/messages/game/tienda.ts b/src/commands/messages/game/tienda.ts index 0c99d62..6cc8f5a 100644 --- a/src/commands/messages/game/tienda.ts +++ b/src/commands/messages/game/tienda.ts @@ -100,12 +100,14 @@ export const command: CommandMessage = { } // Estado inicial - let currentPage = 1; - let selectedOfferId: string | null = null; + const sessionState = { + currentPage: 1, + selectedOfferId: null as string | null + }; const shopMessage = await message.reply({ flags: MessageFlags.SuppressEmbeds | 32768, - components: await buildShopPanel(filteredOffers, currentPage, wallet.coins, selectedOfferId) + components: await buildShopPanel(filteredOffers, sessionState.currentPage, wallet.coins, sessionState.selectedOfferId) }); // Collector para interacciones @@ -120,8 +122,7 @@ export const command: CommandMessage = { await handleButtonInteraction( interaction as ButtonInteraction, filteredOffers, - currentPage, - selectedOfferId, + sessionState, userId, guildId, shopMessage, @@ -131,22 +132,12 @@ export const command: CommandMessage = { await handleSelectInteraction( interaction as StringSelectMenuInteraction, filteredOffers, - currentPage, + sessionState.currentPage, userId, guildId, shopMessage ); } - - // Actualizar página y selección basado en customId - if (interaction.customId === 'shop_prev_page') { - currentPage = Math.max(1, currentPage - 1); - } else if (interaction.customId === 'shop_next_page') { - const totalPages = Math.ceil(filteredOffers.length / ITEMS_PER_PAGE); - currentPage = Math.min(totalPages, currentPage + 1); - } else if (interaction.customId === 'shop_select_item') { - // El select menu ya maneja la selección - } } catch (error: any) { console.error('Error handling shop interaction:', error); if (!interaction.replied && !interaction.deferred) { @@ -341,8 +332,7 @@ async function buildShopPanel( async function handleButtonInteraction( interaction: ButtonInteraction, offers: any[], - currentPage: number, - selectedOfferId: string | null, + sessionState: { currentPage: number; selectedOfferId: string | null }, userId: string, guildId: string, shopMessage: Message, @@ -354,15 +344,17 @@ async function handleButtonInteraction( if (customId.startsWith('shop_view_')) { const offerId = customId.replace('shop_view_', ''); const wallet = await getOrCreateWallet(userId, guildId); + sessionState.selectedOfferId = offerId; await interaction.update({ - components: await buildShopPanel(offers, currentPage, wallet.coins, offerId) + components: await buildShopPanel(offers, sessionState.currentPage, wallet.coins, sessionState.selectedOfferId) }); return; } // Comprar if (customId === 'shop_buy_1' || customId === 'shop_buy_5') { + const selectedOfferId = sessionState.selectedOfferId; if (!selectedOfferId) { await interaction.reply({ content: '❌ Primero selecciona un item.', @@ -385,7 +377,7 @@ async function handleButtonInteraction( // Actualizar tienda await shopMessage.edit({ - components: await buildShopPanel(offers, currentPage, wallet.coins, selectedOfferId) + components: await buildShopPanel(offers, sessionState.currentPage, wallet.coins, sessionState.selectedOfferId) }); } catch (error: any) { await interaction.followUp({ @@ -400,7 +392,7 @@ async function handleButtonInteraction( if (customId === 'shop_refresh') { const wallet = await getOrCreateWallet(userId, guildId); await interaction.update({ - components: await buildShopPanel(offers, currentPage, wallet.coins, selectedOfferId) + components: await buildShopPanel(offers, sessionState.currentPage, wallet.coins, sessionState.selectedOfferId) }); return; } @@ -417,17 +409,19 @@ async function handleButtonInteraction( // Navegación de páginas (ya manejado en el collect) if (customId === 'shop_prev_page' || customId === 'shop_next_page') { const wallet = await getOrCreateWallet(userId, guildId); - let newPage = currentPage; + let newPage = sessionState.currentPage; if (customId === 'shop_prev_page') { - newPage = Math.max(1, currentPage - 1); + newPage = Math.max(1, sessionState.currentPage - 1); } else { const totalPages = Math.ceil(offers.length / ITEMS_PER_PAGE); - newPage = Math.min(totalPages, currentPage + 1); + newPage = Math.min(totalPages, sessionState.currentPage + 1); } + sessionState.currentPage = newPage; + await interaction.update({ - components: await buildShopPanel(offers, newPage, wallet.coins, selectedOfferId) + components: await buildShopPanel(offers, sessionState.currentPage, wallet.coins, sessionState.selectedOfferId) }); return; } diff --git a/src/game/economy/service.ts b/src/game/economy/service.ts index b19c043..bdd3cdb 100644 --- a/src/game/economy/service.ts +++ b/src/game/economy/service.ts @@ -33,7 +33,7 @@ export async function getOrCreateWallet(userId: string, guildId: string) { return prisma.economyWallet.upsert({ where: { userId_guildId: { userId, guildId } }, update: {}, - create: { userId, guildId }, + create: { userId, guildId, coins: 25 }, }); }