From 767616b61790971005443b813d50fe8f25b299a3 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 21 Aug 2024 19:02:36 +0100 Subject: [PATCH 1/3] WIP: Update the search query to only query at the execution of the command --- src/buttonEvents/View.ts | 4 +- src/commands/view.ts | 2 +- src/helpers/CardSearchHelper.ts | 75 ++++++++++++++++++++++++++++----- 3 files changed, 68 insertions(+), 13 deletions(-) diff --git a/src/buttonEvents/View.ts b/src/buttonEvents/View.ts index 8c133ba..b59f426 100644 --- a/src/buttonEvents/View.ts +++ b/src/buttonEvents/View.ts @@ -5,11 +5,11 @@ import CardSearchHelper from "../helpers/CardSearchHelper.js"; export default class View extends ButtonEvent { public override async execute(interaction: ButtonInteraction) { const page = interaction.customId.split(" ")[1]; - const query = interaction.customId.split(" ").splice(1).join(" "); + const results = interaction.customId.split(" ").splice(1); await interaction.deferUpdate(); - const searchResult = await CardSearchHelper.GenerateSearchPage(query, interaction.user.id, Number(page)); + const searchResult = await CardSearchHelper.GenerateSearchPageFromQuery(results, interaction.user.id, Number(page)); if (!searchResult) { await interaction.followUp("No results found"); diff --git a/src/commands/view.ts b/src/commands/view.ts index aff1ef5..86b90aa 100644 --- a/src/commands/view.ts +++ b/src/commands/view.ts @@ -24,7 +24,7 @@ export default class View extends Command { await interaction.deferReply(); - const searchResult = await CardSearchHelper.GenerateSearchPage(name.value!.toString(), interaction.user.id, 0); + const searchResult = await CardSearchHelper.GenerateSearchQuery(name.value!.toString(), interaction.user.id, 0); if (!searchResult) { await interaction.editReply("No results found"); diff --git a/src/helpers/CardSearchHelper.ts b/src/helpers/CardSearchHelper.ts index c0f6bd5..1c68c44 100644 --- a/src/helpers/CardSearchHelper.ts +++ b/src/helpers/CardSearchHelper.ts @@ -11,16 +11,26 @@ interface ReturnedPage { embed: EmbedBuilder, row: ActionRowBuilder, attachment: AttachmentBuilder, + results: string[], } export default class CardSearchHelper { - public static async GenerateSearchPage(query: string, userid: string, page: number): Promise { + public static async GenerateSearchQuery(query: string, userid: string, pages: number): Promise { + AppLogger.LogSilly("CardSearchHelper/GenerateSearchQuery", `Parameters: query=${query}, userid=${userid}, pages=${pages}`); + const fzf = new Fuse(CoreClient.Cards.flatMap(x => x.cards), { keys: ["name"] }); - const entries = fzf.search(query); + const entries = fzf.search(query) + .splice(0, pages); - const entry = entries[page]; + const entry = entries[0]; + const results = entries + .flatMap(x => x.item.id); - if (!entry) return undefined; + if (!entry) { + AppLogger.LogVerbose("CardSearchHelper/GenerateSearchQuery", `Unable to find entry: ${query}`); + + return undefined; + } const card = CardDropHelperMetadata.GetCardByCardNumber(entry.item.id); @@ -32,7 +42,7 @@ export default class CardSearchHelper { try { image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path)); } catch { - AppLogger.LogError("Commands/View", `Unable to fetch image for card ${card.card.id}.`); + AppLogger.LogError("CardSearchHelper/GenerateSearchQuery", `Unable to fetch image for card ${card.card.id}.`); return undefined; } @@ -47,16 +57,61 @@ export default class CardSearchHelper { const row = new ActionRowBuilder() .addComponents( new ButtonBuilder() - .setCustomId(`view ${page - 1} ${query}`) + .setCustomId(`view 0 ${results.join(" ")}`) .setLabel("Previous") .setStyle(ButtonStyle.Primary) - .setDisabled(page == 0), + .setDisabled(true), new ButtonBuilder() - .setCustomId(`view ${page + 1} ${query}`) + .setCustomId(`view 2 ${results.join(" ")}`) .setLabel("Next") .setStyle(ButtonStyle.Primary) - .setDisabled(page + 1 == entries.length)); + .setDisabled(pages == 1)); - return { embed, row, attachment }; + return { embed, row, attachment, results }; + } + + public static async GenerateSearchPageFromQuery(results: string[], userid: string, page: number): Promise { + const currentPageId = results[page - 1]; + + const card = CardDropHelperMetadata.GetCardByCardNumber(currentPageId); + + if (!card) { + AppLogger.LogError("CardSearchHelper/GenerateSearchPageFromQuery", `Unable to find card by id: ${currentPageId}.`); + + return undefined; + } + + let image: Buffer; + const imageFileName = card.card.path.split("/").pop()!; + + try { + image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path)); + } catch { + AppLogger.LogError("CardSearchHelper/GenerateSearchPageFromQuery", `Unable to fetch image for card ${card.card.id}.`); + + return undefined; + } + + const attachment = new AttachmentBuilder(image, { name: imageFileName }); + + const inventory = await Inventory.FetchOneByCardNumberAndUserId(userid, card.card.id); + const quantityClaimed = inventory?.Quantity ?? 0; + + const embed = CardDropHelperMetadata.GenerateDropEmbed(card, quantityClaimed, imageFileName); + + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(`view ${page - 1} ${results.join(" ")}`) + .setLabel("Previous") + .setStyle(ButtonStyle.Primary) + .setDisabled(page - 1 == 0), + new ButtonBuilder() + .setCustomId(`view ${page + 1} ${results.join(" ")}`) + .setLabel("Next") + .setStyle(ButtonStyle.Primary) + .setDisabled(page + 1 == results.length)); + + return { embed, row, attachment, results }; } } -- 2.45.2 From 4c5704531e50def655d51f5854ddd508e2dbd07a Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 23 Aug 2024 18:00:15 +0100 Subject: [PATCH 2/3] Fix unable to find cards issue --- src/buttonEvents/View.ts | 2 +- src/commands/view.ts | 2 +- src/helpers/CardSearchHelper.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/buttonEvents/View.ts b/src/buttonEvents/View.ts index b59f426..f79a1af 100644 --- a/src/buttonEvents/View.ts +++ b/src/buttonEvents/View.ts @@ -5,7 +5,7 @@ import CardSearchHelper from "../helpers/CardSearchHelper.js"; export default class View extends ButtonEvent { public override async execute(interaction: ButtonInteraction) { const page = interaction.customId.split(" ")[1]; - const results = interaction.customId.split(" ").splice(1); + const results = interaction.customId.split(" ").splice(2); await interaction.deferUpdate(); diff --git a/src/commands/view.ts b/src/commands/view.ts index 86b90aa..6268577 100644 --- a/src/commands/view.ts +++ b/src/commands/view.ts @@ -24,7 +24,7 @@ export default class View extends Command { await interaction.deferReply(); - const searchResult = await CardSearchHelper.GenerateSearchQuery(name.value!.toString(), interaction.user.id, 0); + const searchResult = await CardSearchHelper.GenerateSearchQuery(name.value!.toString(), interaction.user.id, 5); if (!searchResult) { await interaction.editReply("No results found"); diff --git a/src/helpers/CardSearchHelper.ts b/src/helpers/CardSearchHelper.ts index 1c68c44..29014f7 100644 --- a/src/helpers/CardSearchHelper.ts +++ b/src/helpers/CardSearchHelper.ts @@ -110,7 +110,7 @@ export default class CardSearchHelper { .setCustomId(`view ${page + 1} ${results.join(" ")}`) .setLabel("Next") .setStyle(ButtonStyle.Primary) - .setDisabled(page + 1 == results.length)); + .setDisabled(page == results.length)); return { embed, row, attachment, results }; } -- 2.45.2 From f0999a042a5834adf6c621dc4185295b2e5dd6a7 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 24 Aug 2024 16:09:24 +0100 Subject: [PATCH 3/3] Bring back the old /view logic as /id --- src/commands/id.ts | 82 ++++++++++++++++++++++++++++++++++++++++++++ src/commands/view.ts | 4 +-- src/registry.ts | 2 ++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/commands/id.ts diff --git a/src/commands/id.ts b/src/commands/id.ts new file mode 100644 index 0000000..ae924a6 --- /dev/null +++ b/src/commands/id.ts @@ -0,0 +1,82 @@ +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"; +import AppLogger from "../client/appLogger"; + +export default class Id extends Command { + constructor() { + super(); + + this.CommandBuilder = new SlashCommandBuilder() + .setName("id") + .setDescription("View a specific command by its id") + .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"); + + AppLogger.LogSilly("Commands/View", `Parameters: cardNumber=${cardNumber?.value}`); + + 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 { + AppLogger.LogError("Commands/View", `Unable to fetch image for card ${card.id}.`); + + 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) { + AppLogger.LogError("Commands/View", `Error sending view for card ${card.id}: ${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."); + } + } + } +} diff --git a/src/commands/view.ts b/src/commands/view.ts index 6268577..3ba5621 100644 --- a/src/commands/view.ts +++ b/src/commands/view.ts @@ -9,7 +9,7 @@ export default class View extends Command { this.CommandBuilder = new SlashCommandBuilder() .setName("view") - .setDescription("View a specific command") + .setDescription("Search for a card by its name") .addStringOption(x => x .setName("name") @@ -24,7 +24,7 @@ export default class View extends Command { await interaction.deferReply(); - const searchResult = await CardSearchHelper.GenerateSearchQuery(name.value!.toString(), interaction.user.id, 5); + const searchResult = await CardSearchHelper.GenerateSearchQuery(name.value!.toString(), interaction.user.id, 7); if (!searchResult) { await interaction.editReply("No results found"); diff --git a/src/registry.ts b/src/registry.ts index 20787af..561d370 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -9,6 +9,7 @@ import Daily from "./commands/daily"; import Drop from "./commands/drop"; import Gdrivesync from "./commands/gdrivesync"; import Give from "./commands/give"; +import Id from "./commands/id"; import Inventory from "./commands/inventory"; import Resync from "./commands/resync"; import Sacrifice from "./commands/sacrifice"; @@ -39,6 +40,7 @@ export default class Registry { CoreClient.RegisterCommand("drop", new Drop()); CoreClient.RegisterCommand("gdrivesync", new Gdrivesync()); CoreClient.RegisterCommand("give", new Give()); + CoreClient.RegisterCommand("id", new Id()); CoreClient.RegisterCommand("inventory", new Inventory()); CoreClient.RegisterCommand("resync", new Resync()); CoreClient.RegisterCommand("sacrifice", new Sacrifice()); -- 2.45.2