feat(wallet): initialize new wallets with a default coin amount

This commit is contained in:
2025-10-05 20:21:29 -05:00
parent b263b10b0f
commit 5bbc14a0cc
2 changed files with 20 additions and 26 deletions

View File

@@ -100,12 +100,14 @@ export const command: CommandMessage = {
} }
// Estado inicial // Estado inicial
let currentPage = 1; const sessionState = {
let selectedOfferId: string | null = null; currentPage: 1,
selectedOfferId: null as string | null
};
const shopMessage = await message.reply({ const shopMessage = await message.reply({
flags: MessageFlags.SuppressEmbeds | 32768, 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 // Collector para interacciones
@@ -120,8 +122,7 @@ export const command: CommandMessage = {
await handleButtonInteraction( await handleButtonInteraction(
interaction as ButtonInteraction, interaction as ButtonInteraction,
filteredOffers, filteredOffers,
currentPage, sessionState,
selectedOfferId,
userId, userId,
guildId, guildId,
shopMessage, shopMessage,
@@ -131,22 +132,12 @@ export const command: CommandMessage = {
await handleSelectInteraction( await handleSelectInteraction(
interaction as StringSelectMenuInteraction, interaction as StringSelectMenuInteraction,
filteredOffers, filteredOffers,
currentPage, sessionState.currentPage,
userId, userId,
guildId, guildId,
shopMessage 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) { } catch (error: any) {
console.error('Error handling shop interaction:', error); console.error('Error handling shop interaction:', error);
if (!interaction.replied && !interaction.deferred) { if (!interaction.replied && !interaction.deferred) {
@@ -341,8 +332,7 @@ async function buildShopPanel(
async function handleButtonInteraction( async function handleButtonInteraction(
interaction: ButtonInteraction, interaction: ButtonInteraction,
offers: any[], offers: any[],
currentPage: number, sessionState: { currentPage: number; selectedOfferId: string | null },
selectedOfferId: string | null,
userId: string, userId: string,
guildId: string, guildId: string,
shopMessage: Message, shopMessage: Message,
@@ -354,15 +344,17 @@ async function handleButtonInteraction(
if (customId.startsWith('shop_view_')) { if (customId.startsWith('shop_view_')) {
const offerId = customId.replace('shop_view_', ''); const offerId = customId.replace('shop_view_', '');
const wallet = await getOrCreateWallet(userId, guildId); const wallet = await getOrCreateWallet(userId, guildId);
sessionState.selectedOfferId = offerId;
await interaction.update({ await interaction.update({
components: await buildShopPanel(offers, currentPage, wallet.coins, offerId) components: await buildShopPanel(offers, sessionState.currentPage, wallet.coins, sessionState.selectedOfferId)
}); });
return; return;
} }
// Comprar // Comprar
if (customId === 'shop_buy_1' || customId === 'shop_buy_5') { if (customId === 'shop_buy_1' || customId === 'shop_buy_5') {
const selectedOfferId = sessionState.selectedOfferId;
if (!selectedOfferId) { if (!selectedOfferId) {
await interaction.reply({ await interaction.reply({
content: '❌ Primero selecciona un item.', content: '❌ Primero selecciona un item.',
@@ -385,7 +377,7 @@ async function handleButtonInteraction(
// Actualizar tienda // Actualizar tienda
await shopMessage.edit({ await shopMessage.edit({
components: await buildShopPanel(offers, currentPage, wallet.coins, selectedOfferId) components: await buildShopPanel(offers, sessionState.currentPage, wallet.coins, sessionState.selectedOfferId)
}); });
} catch (error: any) { } catch (error: any) {
await interaction.followUp({ await interaction.followUp({
@@ -400,7 +392,7 @@ async function handleButtonInteraction(
if (customId === 'shop_refresh') { if (customId === 'shop_refresh') {
const wallet = await getOrCreateWallet(userId, guildId); const wallet = await getOrCreateWallet(userId, guildId);
await interaction.update({ await interaction.update({
components: await buildShopPanel(offers, currentPage, wallet.coins, selectedOfferId) components: await buildShopPanel(offers, sessionState.currentPage, wallet.coins, sessionState.selectedOfferId)
}); });
return; return;
} }
@@ -417,17 +409,19 @@ async function handleButtonInteraction(
// Navegación de páginas (ya manejado en el collect) // Navegación de páginas (ya manejado en el collect)
if (customId === 'shop_prev_page' || customId === 'shop_next_page') { if (customId === 'shop_prev_page' || customId === 'shop_next_page') {
const wallet = await getOrCreateWallet(userId, guildId); const wallet = await getOrCreateWallet(userId, guildId);
let newPage = currentPage; let newPage = sessionState.currentPage;
if (customId === 'shop_prev_page') { if (customId === 'shop_prev_page') {
newPage = Math.max(1, currentPage - 1); newPage = Math.max(1, sessionState.currentPage - 1);
} else { } else {
const totalPages = Math.ceil(offers.length / ITEMS_PER_PAGE); 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({ await interaction.update({
components: await buildShopPanel(offers, newPage, wallet.coins, selectedOfferId) components: await buildShopPanel(offers, sessionState.currentPage, wallet.coins, sessionState.selectedOfferId)
}); });
return; return;
} }

View File

@@ -33,7 +33,7 @@ export async function getOrCreateWallet(userId: string, guildId: string) {
return prisma.economyWallet.upsert({ return prisma.economyWallet.upsert({
where: { userId_guildId: { userId, guildId } }, where: { userId_guildId: { userId, guildId } },
update: {}, update: {},
create: { userId, guildId }, create: { userId, guildId, coins: 25 },
}); });
} }