WIP: Update the search query to only query at the execution of the command
All checks were successful
Test / build (push) Successful in 10s
All checks were successful
Test / build (push) Successful in 10s
This commit is contained in:
parent
79a4d18df3
commit
767616b617
3 changed files with 68 additions and 13 deletions
|
@ -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");
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -11,16 +11,26 @@ interface ReturnedPage {
|
|||
embed: EmbedBuilder,
|
||||
row: ActionRowBuilder<ButtonBuilder>,
|
||||
attachment: AttachmentBuilder,
|
||||
results: string[],
|
||||
}
|
||||
|
||||
export default class CardSearchHelper {
|
||||
public static async GenerateSearchPage(query: string, userid: string, page: number): Promise<ReturnedPage | undefined> {
|
||||
public static async GenerateSearchQuery(query: string, userid: string, pages: number): Promise<ReturnedPage | undefined> {
|
||||
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<ButtonBuilder>()
|
||||
.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<ReturnedPage | undefined> {
|
||||
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<ButtonBuilder>()
|
||||
.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 };
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue