refactor: Optimize item autocomplete by moving name filtering to the database query and increasing the limit.

This commit is contained in:
syntaxbullet
2025-12-18 16:51:22 +01:00
parent 34cbea2753
commit 83cd33e439

View File

@@ -60,20 +60,24 @@ export const use = createCommand({
const focusedValue = interaction.options.getFocused(); const focusedValue = interaction.options.getFocused();
const userId = interaction.user.id; const userId = interaction.user.id;
// Fetch owned items that are usable // Fetch owned items that match the search query
const userInventory = await DrizzleClient.query.inventory.findMany({ // We join with items table to filter by name directly in the database
where: eq(inventory.userId, BigInt(userId)), const entries = await DrizzleClient.select({
with: { quantity: inventory.quantity,
item: true item: items
}, })
limit: 10 .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 filtered = entries.filter(entry => {
const matchName = entry.item.name.toLowerCase().includes(focusedValue.toLowerCase());
const usageData = entry.item.usageData as ItemUsageData | null; const usageData = entry.item.usageData as ItemUsageData | null;
const isUsable = usageData && usageData.effects && usageData.effects.length > 0; const isUsable = usageData && usageData.effects && usageData.effects.length > 0;
return matchName && isUsable; return isUsable;
}); });
await interaction.respond( await interaction.respond(