import { option } from "@purrkit/router"; import { Result } from "@sapphire/result"; import { ContainerBuilder, TextDisplayBuilder } from "discord.js"; import { kitten } from "@/client"; import { errorMessage, logger } from "@/lib"; import { client } from "./client"; export const searchCommand = kitten.command("search", { description: "Search for users", options: { query: option.string("The user to search for", { required: true }), }, run: async (interaction, args) => { const { query } = args; const result = await Result.fromAsync(() => client.searchUsers(query)); if (result.isErr()) { logger.error(result.unwrapErr(), "error searching for users"); return errorMessage(interaction, "There was an error fetching the search results"); } const users = result.unwrap().data; if (!users || users.length === 0) { const container = new ContainerBuilder().addTextDisplayComponents( new TextDisplayBuilder().setContent(`No results found for **"${query}"**.`), ); return interaction.reply({ components: [container], flags: ["IsComponentsV2"] }); } const normalizedQuery = query.toLowerCase(); const exactMatches = users.filter((u) => u.name.toLowerCase() === normalizedQuery); const otherMatches = users.filter((u) => u.name.toLowerCase() !== normalizedQuery); const lines: string[] = []; if (exactMatches.length > 0) { lines.push("### Exact Matches"); for (const u of exactMatches) { lines.push( `* **${u.displayName}** \`@${u.name}\` - [View Profile](https://roblox.com/users/${u.id}/profile)`, ); } if (otherMatches.length > 0) { lines.push(""); } } if (otherMatches.length > 0) { if (exactMatches.length > 0) { lines.push("### Other Matches"); } const displayLimit = 25; const displayOthers = otherMatches.slice(0, displayLimit); for (const u of displayOthers) { lines.push( `* ${u.displayName} \`@${u.name}\` - [View Profile](https://roblox.com/users/${u.id}/profile)`, ); } if (otherMatches.length > displayLimit) { lines.push(`*...and ${otherMatches.length - displayLimit} more results.*`); } } const container = new ContainerBuilder().addTextDisplayComponents( new TextDisplayBuilder().setContent(`**Search results for "${query}"**`), new TextDisplayBuilder().setContent(lines.join("\n")), ); return interaction.reply({ components: [container], flags: ["IsComponentsV2"] }); }, });