From f181c5dbfe869b747e869db496c735a73587eb65 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 2 Feb 2024 21:53:42 +0000 Subject: [PATCH 1/2] Add view command (#153) # 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. - Add view command to let users see specific cards but not claim them #80 ## 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/153 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- src/commands/view.ts | 77 ++++++++++++++++++++++++++++++++++++++++++++ src/registry.ts | 2 ++ 2 files changed, 79 insertions(+) create mode 100644 src/commands/view.ts diff --git a/src/commands/view.ts b/src/commands/view.ts new file mode 100644 index 0000000..8f89eb4 --- /dev/null +++ b/src/commands/view.ts @@ -0,0 +1,77 @@ +import { AttachmentBuilder, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js"; +import { Command } from "../type/command"; +import { CoreClient } from "../client/client"; +import { readFileSync } from "fs"; +import path from "path"; +import Inventory from "../database/entities/app/Inventory"; +import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata"; + +export default class View extends Command { + constructor() { + super(); + + this.CommandBuilder = new SlashCommandBuilder() + .setName("view") + .setDescription("View a specific command") + .addStringOption(x => + x + .setName("cardnumber") + .setDescription("The card number to view") + .setRequired(true)); + } + + public override async execute(interaction: CommandInteraction) { + const cardNumber = interaction.options.get("cardnumber"); + + if (!cardNumber || !cardNumber.value) { + await interaction.reply("Card number is required."); + return; + } + + const card = CoreClient.Cards + .flatMap(x => x.cards) + .find(x => x.id == cardNumber.value); + + if (!card) { + await interaction.reply("Card not found."); + return; + } + + const series = CoreClient.Cards + .find(x => x.cards.includes(card))!; + + let image: Buffer; + const imageFileName = card.path.split("/").pop()!; + + try { + image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.path)); + } catch { + await interaction.reply(`Unable to fetch image for card ${card.id}.`); + return; + } + + await interaction.deferReply(); + + const attachment = new AttachmentBuilder(image, { name: imageFileName }); + + const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.id); + const quantityClaimed = inventory ? inventory.Quantity : 0; + + const embed = CardDropHelperMetadata.GenerateDropEmbed({ card, series }, quantityClaimed, imageFileName); + + try { + await interaction.editReply({ + embeds: [ embed ], + files: [ attachment ], + }); + } catch (e) { + console.error(e); + + if (e instanceof DiscordAPIError) { + await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. Code: ${e.code}.`); + } else { + await interaction.editReply("Unable to send next drop. Please try again, and report this if it keeps happening. Code: UNKNOWN."); + } + } + } +} \ No newline at end of file diff --git a/src/registry.ts b/src/registry.ts index a7cf494..2fc657d 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -8,6 +8,7 @@ import Gdrivesync from "./commands/gdrivesync"; import Give from "./commands/give"; import Inventory from "./commands/inventory"; import Resync from "./commands/resync"; +import View from "./commands/view"; // Test Command Imports import Dropnumber from "./commands/stage/dropnumber"; @@ -27,6 +28,7 @@ export default class Registry { CoreClient.RegisterCommand("give", new Give()); CoreClient.RegisterCommand("inventory", new Inventory()); CoreClient.RegisterCommand("resync", new Resync()); + CoreClient.RegisterCommand("view", new View()); // Test Commands CoreClient.RegisterCommand("dropnumber", new Dropnumber(), Environment.Test); From a0ba8d7994457f34706db8fe9bc5dec88fed1245 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 12 Feb 2024 17:49:51 +0000 Subject: [PATCH 2/2] Update footer text in CardDropHelperMetadata to include card number (#159) # 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 footer text in CardDropHelperMetadata to include card number #127 ## 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. - This has been tested by running the `/drop`, `/dropnumber` commands, and `Reroll` button event. All display the card number on the footer now # 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/159 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- src/helpers/CardDropHelperMetadata.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/CardDropHelperMetadata.ts b/src/helpers/CardDropHelperMetadata.ts index 27eebb8..762648d 100644 --- a/src/helpers/CardDropHelperMetadata.ts +++ b/src/helpers/CardDropHelperMetadata.ts @@ -63,7 +63,7 @@ export default class CardDropHelperMetadata { return new EmbedBuilder() .setTitle(drop.card.name) .setDescription(description) - .setFooter({ text: CardRarityToString(drop.card.type) }) + .setFooter({ text: `${CardRarityToString(drop.card.type)} ยท ${drop.card.id}` }) .setColor(CardRarityToColour(drop.card.type)) .setImage(`attachment://${imageFileName}`); }