forked from syntaxbullet/AuroraBot-discord
refactor: Optimize item autocomplete by moving name filtering to the database query and increasing the limit.
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user