Update view command to use fuzzy search instead of direct card number (#340)
- Install `fuse.js` package to allow for fuzzy finding - Update the `/view` command to use fuzzy search by name instead of the card number - Add pagination for the command via the `View` button event #154 Reviewed-on: #340 Reviewed-by: VylpesTester <tester@vylpes.com> Co-authored-by: Ethan Lane <ethan@vylpes.com> Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
parent
8bd5f44524
commit
5defb682c1
7 changed files with 112 additions and 58 deletions
62
src/helpers/CardSearchHelper.ts
Normal file
62
src/helpers/CardSearchHelper.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
import {ActionRowBuilder, AttachmentBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder} from "discord.js";
|
||||
import Fuse from "fuse.js";
|
||||
import {CoreClient} from "../client/client.js";
|
||||
import CardDropHelperMetadata from "./CardDropHelperMetadata.js";
|
||||
import Inventory from "../database/entities/app/Inventory.js";
|
||||
import {readFileSync} from "fs";
|
||||
import path from "path";
|
||||
import AppLogger from "../client/appLogger.js";
|
||||
|
||||
interface ReturnedPage {
|
||||
embed: EmbedBuilder,
|
||||
row: ActionRowBuilder<ButtonBuilder>,
|
||||
attachment: AttachmentBuilder,
|
||||
}
|
||||
|
||||
export default class CardSearchHelper {
|
||||
public static async GenerateSearchPage(query: string, userid: string, page: number): Promise<ReturnedPage | undefined> {
|
||||
const fzf = new Fuse(CoreClient.Cards.flatMap(x => x.cards), { keys: ["name"] });
|
||||
const entries = fzf.search(query);
|
||||
|
||||
const entry = entries[page];
|
||||
|
||||
if (!entry) return undefined;
|
||||
|
||||
const card = CardDropHelperMetadata.GetCardByCardNumber(entry.item.id);
|
||||
|
||||
if (!card) 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("Commands/View", `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<ButtonBuilder>()
|
||||
.addComponents(
|
||||
new ButtonBuilder()
|
||||
.setCustomId(`view ${page - 1} ${query}`)
|
||||
.setLabel("Previous")
|
||||
.setStyle(ButtonStyle.Primary)
|
||||
.setDisabled(page == 0),
|
||||
new ButtonBuilder()
|
||||
.setCustomId(`view ${page + 1} ${query}`)
|
||||
.setLabel("Next")
|
||||
.setStyle(ButtonStyle.Primary)
|
||||
.setDisabled(page + 1 == entries.length));
|
||||
|
||||
return { embed, row, attachment };
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue