all repos — stealth-developers @ fee480c17f290e9703225a6747c6f80fd4bfb8a2

src/interactions/commands/roblox/getUser.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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
import {
	type ChatInputCommandInteraction,
	type Client,
	type GuildMember,
	SlashCommandBuilder,
} from "discord.js";

import { roblox } from "@/roblox/client";
import { processRobloxUserInteraction } from "@/roblox/robloxUser";
import type { RobloxUserId } from "@/roblox/types";
import { hasManagerPermissions } from "@/utils/discord/permissions";

const commandData = new SlashCommandBuilder()
	.setName("user")
	.setDescription("Get information about a Roblox user")
	// .addSubcommand((subcommand) =>
	// 	subcommand
	// 		.setName("discord")
	// 		.setDescription("Use Bloxlink to get Roblox info from a Discord user")
	// 		.addUserOption((option) =>
	// 			option
	// 				.setName("user")
	// 				.setDescription("The Discord user to look up")
	// 				.setRequired(true),
	// 		),
	// )
	.addSubcommand((subcommand) =>
		subcommand
			.setName("roblox")
			.setDescription("Get Roblox info from a user ID or username")
			.addStringOption((option) =>
				option
					.setName("input")
					.setDescription(
						"The Roblox username to lookup - prefix with id: for IDs",
					)
					.setRequired(true),
			),
	);

async function execute(
	_client: Client,
	interaction: ChatInputCommandInteraction,
) {
	const subcommand = interaction.options.getSubcommand();
	const isModerator = await hasManagerPermissions(
		interaction.member as GuildMember,
	);
	await interaction.deferReply();

	try {
		let id: RobloxUserId;

		if (subcommand === "discord") {
			const target = interaction.options.getUser("user", true);
			const [linkedRes, linkedErr] = await roblox.getLinkedAccount(target.id);
			if (linkedErr) {
				const message = linkedErr.error;
				if (message === "User not found") {
					await interaction.editReply(
						"No linked Roblox account found for that user.",
					);
					return;
				}
				await interaction.editReply(
					`There was an error fetching the linked account: ${linkedErr.error}`,
				);
				return;
			}
			id = linkedRes.robloxID;
		} else {
			const input = interaction.options.getString("input", true);
			const isId = input.startsWith("id:");

			if (isId) {
				id = input.slice(3);
				if (!id) {
					await interaction.editReply("Invalid ID provided.");
					return;
				}
			} else {
				const [usersRes] = await roblox.getUsersByUsernames([input]);
				if (!usersRes || !usersRes.data || usersRes.data.length === 0) {
					await interaction.editReply("User not found");
					return;
				}
				id = usersRes.data[0].id;
			}
		}

		await processRobloxUserInteraction(interaction, id, isModerator);
	} catch (err) {
		console.error(err);
		await interaction.editReply(
			"An unexpected error occurred while fetching the user.",
		);
	}
}

export default {
	data: commandData,
	execute,
};