From ecc879cf13616bd2d280547130fa178e2e733f59 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 24 May 2025 10:05:49 +0100 Subject: [PATCH] Fix sacrificing a dropped card taking from your inventory first (#448) - Fix sacrificing a dropped card trying to take it from your inventory instead of just directly from the drop - Did it by creating a separate subaction for the sacrifice command, which does the same logic except without checking your inventory, since it wont be coming from there. - Fix the sacrificed card image showing outside of the embed #435, #436 Reviewed-on: https://git.vylpes.xyz/External/card-drop/pulls/448 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- src/buttonEvents/Sacrifice.ts | 76 ++++++++++++++++++++++ src/helpers/DropHelpers/DropEmbedHelper.ts | 2 +- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/buttonEvents/Sacrifice.ts b/src/buttonEvents/Sacrifice.ts index de0bb77..1626cf6 100644 --- a/src/buttonEvents/Sacrifice.ts +++ b/src/buttonEvents/Sacrifice.ts @@ -5,6 +5,7 @@ import { CardRarityToString, GetSacrificeAmount } from "../constants/CardRarity" import EmbedColours from "../constants/EmbedColours"; import User from "../database/entities/app/User"; import GetCardsHelper from "../helpers/DropHelpers/GetCardsHelper"; +import CardConstants from "../constants/CardConstants"; export default class Sacrifice extends ButtonEvent { public override async execute(interaction: ButtonInteraction) { @@ -17,6 +18,8 @@ export default class Sacrifice extends ButtonEvent { case "cancel": await this.cancel(interaction); break; + case "give": + await this.give(interaction); } } @@ -168,4 +171,77 @@ export default class Sacrifice extends ButtonEvent { components: [ row ], }); } + + private async give(interaction: ButtonInteraction) { + const userId = interaction.customId.split(" ")[2]; + const cardNumber = interaction.customId.split(" ")[3]; + const quantity = Number(interaction.customId.split(" ")[4]) || 1; + + if (userId != interaction.user.id) { + await interaction.reply("Only the user who created this sacrifice can confirm it."); + return; + } + + const cardData = GetCardsHelper.GetCardByCardNumber(cardNumber); + + if (!cardData) { + await interaction.reply("Unable to find card in the database."); + return; + } + + let user = await User.FetchOneById(User, userId); + + if (!user) { + user = new User(userId, CardConstants.StartingCurrency); + } + + const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber); + let cardQuantity = 0; + + if (cardInInventory) { + cardQuantity = cardInInventory.Quantity; + } + + const cardValue = GetSacrificeAmount(cardData.card.type) * quantity; + const cardRarityString = CardRarityToString(cardData.card.type); + + user.AddCurrency(cardValue); + + await user.Save(User, user); + + const description = [ + `Card: ${cardData.card.name}`, + `Series: ${cardData.series.name}`, + `Rarity: ${cardRarityString}`, + `Quantity Owned: ${cardQuantity}`, + `Quantity To Sacrifice: ${quantity}`, + `Sacrifice Amount: ${cardValue}`, + ]; + + const embed = new EmbedBuilder() + .setTitle("Card Sacrificed") + .setDescription(description.join("\n")) + .setColor(EmbedColours.Green) + .setFooter({ text: `${interaction.user.username}` }); + + const row = new ActionRowBuilder() + .addComponents([ + new ButtonBuilder() + .setCustomId(`sacrifice confirm ${interaction.user.id} ${cardNumber}`) + .setLabel("Confirm") + .setStyle(ButtonStyle.Success) + .setDisabled(true), + new ButtonBuilder() + .setCustomId("sacrifice cancel") + .setLabel("Cancel") + .setStyle(ButtonStyle.Secondary) + .setDisabled(true), + ]); + + await interaction.update({ + embeds: [ embed ], + components: [ row ], + attachments: [], + }); + } } \ No newline at end of file diff --git a/src/helpers/DropHelpers/DropEmbedHelper.ts b/src/helpers/DropHelpers/DropEmbedHelper.ts index 08b5813..5956a27 100644 --- a/src/helpers/DropHelpers/DropEmbedHelper.ts +++ b/src/helpers/DropHelpers/DropEmbedHelper.ts @@ -77,7 +77,7 @@ export default class DropEmbedHelper { .setStyle(ButtonStyle.Success) .setDisabled(disabled), new ButtonBuilder() - .setCustomId(`sacrifice confirm ${userId} ${drop.card.id} 1`) + .setCustomId(`sacrifice give ${userId} ${drop.card.id} 1`) .setLabel(`Sacrifice`) .setStyle(ButtonStyle.Danger), new ButtonBuilder()