From a97a24f72ad444e67515cc5d78408c70a07aadc1 Mon Sep 17 00:00:00 2001 From: syntaxbullet Date: Thu, 18 Dec 2025 19:41:50 +0100 Subject: [PATCH] chore: updated listing command with autocomplete from items table --- src/commands/admin/listing.ts | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/commands/admin/listing.ts b/src/commands/admin/listing.ts index 9ded877..361f5b9 100644 --- a/src/commands/admin/listing.ts +++ b/src/commands/admin/listing.ts @@ -11,15 +11,19 @@ import { } from "discord.js"; import { inventoryService } from "@/modules/inventory/inventory.service"; import { createErrorEmbed, createWarningEmbed } from "@lib/embeds"; +import { items } from "@/db/schema"; +import { ilike, isNotNull, and } from "drizzle-orm"; +import { DrizzleClient } from "@/lib/DrizzleClient"; export const listing = createCommand({ data: new SlashCommandBuilder() .setName("listing") .setDescription("Post an item listing in the channel for users to buy") .addNumberOption(option => - option.setName("itemid") - .setDescription("The ID of the item to list") + option.setName("item") + .setDescription("The item to list") .setRequired(true) + .setAutocomplete(true) ) .addChannelOption(option => option.setName("channel") @@ -30,7 +34,7 @@ export const listing = createCommand({ execute: async (interaction) => { await interaction.deferReply({ flags: MessageFlags.Ephemeral }); - const itemId = interaction.options.getNumber("itemid", true); + const itemId = interaction.options.getNumber("item", true); const targetChannel = (interaction.options.getChannel("channel") as BaseGuildTextChannel) || interaction.channel as BaseGuildTextChannel; if (!targetChannel || !targetChannel.isSendable()) { @@ -73,5 +77,29 @@ export const listing = createCommand({ console.error("Failed to send listing message:", error); await interaction.editReply({ content: "", embeds: [createErrorEmbed("Failed to post the listing.")] }); } + }, + autocomplete: async (interaction) => { + const focusedValue = interaction.options.getFocused(); + + const results = await DrizzleClient.select({ + id: items.id, + name: items.name, + price: items.price + }) + .from(items) + .where( + and( + ilike(items.name, `%${focusedValue}%`), + isNotNull(items.price) + ) + ) + .limit(20); + + await interaction.respond( + results.map(item => ({ + name: `${item.name} (Price: ${item.price})`, + value: item.id + })) + ); } });