From c7b01c932e71f6101155f851377888128d7f5d22 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 31 Mar 2024 15:51:10 +0100 Subject: [PATCH] Update claim event to increment the claimed number when the user claims a drop (#191) # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Update the claim event to increment the claim number when the user claims a drop - Due to discord api limitations I can't reply to an interaction and edit the embed, so I've had to change it so the claimed by is displayed in the same embed - This also has the nice side effect of letting me disable the claim button as well #89 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. # Checklist - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://gitea.vylpes.xyz/External/card-drop/pulls/191 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- src/buttonEvents/Claim.ts | 31 +++++++++++++++++++++------ src/commands/give.ts | 6 +++--- src/helpers/CardDropHelperMetadata.ts | 30 ++++++++++++++++++++------ 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/buttonEvents/Claim.ts b/src/buttonEvents/Claim.ts index 53538f4..6c45900 100644 --- a/src/buttonEvents/Claim.ts +++ b/src/buttonEvents/Claim.ts @@ -1,9 +1,12 @@ -import { ButtonInteraction } from "discord.js"; +import { AttachmentBuilder, ButtonInteraction } from "discord.js"; import { ButtonEvent } from "../type/buttonEvent"; import Inventory from "../database/entities/app/Inventory"; import { CoreClient } from "../client/client"; import { default as eClaim } from "../database/entities/app/Claim"; import AppLogger from "../client/appLogger"; +import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata"; +import { readFileSync } from "fs"; +import path from "path"; export default class Claim extends ButtonEvent { public override async execute(interaction: ButtonInteraction) { @@ -16,17 +19,15 @@ export default class Claim extends ButtonEvent { AppLogger.LogSilly("Button/Claim", `Parameters: cardNumber=${cardNumber}, claimId=${claimId}, droppedBy=${droppedBy}, userId=${userId}`); - await interaction.deferReply(); - const claimed = await eClaim.FetchOneByClaimId(claimId); if (claimed) { - await interaction.editReply("This card has already been claimed"); + await interaction.reply("This card has already been claimed"); return; } if (claimId == CoreClient.ClaimId && userId != droppedBy) { - await interaction.editReply("The latest dropped card can only be claimed by the user who dropped it"); + await interaction.reply("The latest dropped card can only be claimed by the user who dropped it"); return; } @@ -45,6 +46,24 @@ export default class Claim extends ButtonEvent { await claim.Save(eClaim, claim); - await interaction.editReply(`Card claimed by ${interaction.user}`); + const card = CardDropHelperMetadata.GetCardByCardNumber(cardNumber); + + if (!card) { + return; + } + + const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path)); + const imageFileName = card.card.path.split("/").pop()!; + + const attachment = new AttachmentBuilder(image, { name: imageFileName }); + + const embed = CardDropHelperMetadata.GenerateDropEmbed(card, inventory.Quantity, imageFileName, interaction.user.username); + const row = CardDropHelperMetadata.GenerateDropButtons(card, claimId, interaction.user.id, true); + + await interaction.update({ + embeds: [ embed ], + files: [ attachment ], + components: [ row ], + }); } } \ No newline at end of file diff --git a/src/commands/give.ts b/src/commands/give.ts index 5121dda..3656dc5 100644 --- a/src/commands/give.ts +++ b/src/commands/give.ts @@ -56,16 +56,16 @@ export default class Give extends Command { return; } - let inventory = await Inventory.FetchOneByCardNumberAndUserId(user.id, card.id); + let inventory = await Inventory.FetchOneByCardNumberAndUserId(user.id, card.card.id); if (!inventory) { - inventory = new Inventory(user.id, card.id, 1); + inventory = new Inventory(user.id, card.card.id, 1); } else { inventory.SetQuantity(inventory.Quantity + 1); } await inventory.Save(Inventory, inventory); - await interaction.reply(`${card.name} given to ${user.username}, they now have ${inventory.Quantity}`); + await interaction.reply(`${card.card.name} given to ${user.username}, they now have ${inventory.Quantity}`); } } \ No newline at end of file diff --git a/src/helpers/CardDropHelperMetadata.ts b/src/helpers/CardDropHelperMetadata.ts index 27a0d04..26a7731 100644 --- a/src/helpers/CardDropHelperMetadata.ts +++ b/src/helpers/CardDropHelperMetadata.ts @@ -1,7 +1,7 @@ import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js"; import { CardRarity, CardRarityToColour, CardRarityToString } from "../constants/CardRarity"; import CardRarityChances from "../constants/CardRarityChances"; -import { CardMetadata, DropResult } from "../contracts/SeriesMetadata"; +import { DropResult } from "../contracts/SeriesMetadata"; import { CoreClient } from "../client/client"; import AppLogger from "../client/appLogger"; @@ -56,25 +56,40 @@ export default class CardDropHelperMetadata { }; } - public static GetCardByCardNumber(cardNumber: string): CardMetadata | undefined { + public static GetCardByCardNumber(cardNumber: string): DropResult | undefined { AppLogger.LogSilly("CardDropHelperMetadata/GetCardByCardNumber", `Parameters: cardNumber=${cardNumber}`); const card = CoreClient.Cards .flatMap(x => x.cards) .find(x => x.id == cardNumber); - AppLogger.LogSilly("CardDropHelperMetadata/GetCardByCardNumber", `Card: ${card?.id} ${card?.name}`); + const series = CoreClient.Cards + .find(x => x.cards.find(y => y.id == card?.id)); - return card; + AppLogger.LogSilly("CardDropHelperMetadata/GetCardByCardNumber", `Card: ${card?.id} ${card?.name}`); + AppLogger.LogSilly("CardDropHelperMetadata/GetCardByCardNumber", `Series: ${series?.id} ${series?.name}`); + + if (!card || !series) { + AppLogger.LogVerbose("CardDropHelperMetadata/GetCardByCardNumber", `Unable to find card metadata: ${cardNumber}`); + return undefined; + } + + return { card, series }; } - public static GenerateDropEmbed(drop: DropResult, quantityClaimed: number, imageFileName: string): EmbedBuilder { + public static GenerateDropEmbed(drop: DropResult, quantityClaimed: number, imageFileName: string, claimedBy?: string): EmbedBuilder { AppLogger.LogSilly("CardDropHelperMetadata/GenerateDropEmbed", `Parameters: drop=${drop.card.id}, quantityClaimed=${quantityClaimed}, imageFileName=${imageFileName}`); let description = ""; description += `Series: ${drop.series.name}\n`; description += `Claimed: ${quantityClaimed}\n`; + if (claimedBy != null) { + description += `Claimed by: ${claimedBy}\n`; + } else { + description += "Claimed by: (UNCLAIMED)\n"; + } + return new EmbedBuilder() .setTitle(drop.card.name) .setDescription(description) @@ -83,7 +98,7 @@ export default class CardDropHelperMetadata { .setImage(`attachment://${imageFileName}`); } - public static GenerateDropButtons(drop: DropResult, claimId: string, userId: string): ActionRowBuilder { + public static GenerateDropButtons(drop: DropResult, claimId: string, userId: string, disabled: boolean = false): ActionRowBuilder { AppLogger.LogSilly("CardDropHelperMetadata/GenerateDropButtons", `Parameters: drop=${drop.card.id}, claimId=${claimId}, userId=${userId}`); return new ActionRowBuilder() @@ -91,7 +106,8 @@ export default class CardDropHelperMetadata { new ButtonBuilder() .setCustomId(`claim ${drop.card.id} ${claimId} ${userId}`) .setLabel("Claim") - .setStyle(ButtonStyle.Primary), + .setStyle(ButtonStyle.Primary) + .setDisabled(disabled), new ButtonBuilder() .setCustomId("reroll") .setLabel("Reroll")