chore: updated listing command with autocomplete from items table
This commit is contained in:
@@ -11,15 +11,19 @@ import {
|
|||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
import { inventoryService } from "@/modules/inventory/inventory.service";
|
import { inventoryService } from "@/modules/inventory/inventory.service";
|
||||||
import { createErrorEmbed, createWarningEmbed } from "@lib/embeds";
|
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({
|
export const listing = createCommand({
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName("listing")
|
.setName("listing")
|
||||||
.setDescription("Post an item listing in the channel for users to buy")
|
.setDescription("Post an item listing in the channel for users to buy")
|
||||||
.addNumberOption(option =>
|
.addNumberOption(option =>
|
||||||
option.setName("itemid")
|
option.setName("item")
|
||||||
.setDescription("The ID of the item to list")
|
.setDescription("The item to list")
|
||||||
.setRequired(true)
|
.setRequired(true)
|
||||||
|
.setAutocomplete(true)
|
||||||
)
|
)
|
||||||
.addChannelOption(option =>
|
.addChannelOption(option =>
|
||||||
option.setName("channel")
|
option.setName("channel")
|
||||||
@@ -30,7 +34,7 @@ export const listing = createCommand({
|
|||||||
execute: async (interaction) => {
|
execute: async (interaction) => {
|
||||||
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
|
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;
|
const targetChannel = (interaction.options.getChannel("channel") as BaseGuildTextChannel) || interaction.channel as BaseGuildTextChannel;
|
||||||
|
|
||||||
if (!targetChannel || !targetChannel.isSendable()) {
|
if (!targetChannel || !targetChannel.isSendable()) {
|
||||||
@@ -73,5 +77,29 @@ export const listing = createCommand({
|
|||||||
console.error("Failed to send listing message:", error);
|
console.error("Failed to send listing message:", error);
|
||||||
await interaction.editReply({ content: "", embeds: [createErrorEmbed("Failed to post the listing.")] });
|
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
|
||||||
|
}))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user