all repos — stealth-developers @ b67a9627edd74694e4a3fa47bc78c20d8c534299

apps/bot/src/feats/roblox/search.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
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"] });
	},
});