chore: updated listing command with autocomplete from items table

This commit is contained in:
syntaxbullet
2025-12-18 19:41:50 +01:00
parent 7bd4d811cd
commit a97a24f72a

View File

@@ -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
}))
);
}
});