diff --git a/src/commands/inventory/use.ts b/src/commands/inventory/use.ts index 1c5bff0..3283782 100644 --- a/src/commands/inventory/use.ts +++ b/src/commands/inventory/use.ts @@ -60,20 +60,24 @@ export const use = createCommand({ const focusedValue = interaction.options.getFocused(); const userId = interaction.user.id; - // Fetch owned items that are usable - const userInventory = await DrizzleClient.query.inventory.findMany({ - where: eq(inventory.userId, BigInt(userId)), - with: { - item: true - }, - limit: 10 - }); + // Fetch owned items that match the search query + // We join with items table to filter by name directly in the database + const entries = await DrizzleClient.select({ + quantity: inventory.quantity, + item: items + }) + .from(inventory) + .innerJoin(items, eq(inventory.itemId, items.id)) + .where(and( + eq(inventory.userId, BigInt(userId)), + like(items.name, `%${focusedValue}%`) + )) + .limit(20); // Fetch up to 20 matching items - const filtered = userInventory.filter(entry => { - const matchName = entry.item.name.toLowerCase().includes(focusedValue.toLowerCase()); + const filtered = entries.filter(entry => { const usageData = entry.item.usageData as ItemUsageData | null; const isUsable = usageData && usageData.effects && usageData.effects.length > 0; - return matchName && isUsable; + return isUsable; }); await interaction.respond(