58 lines
3.0 KiB
TypeScript
58 lines
3.0 KiB
TypeScript
import { createCommand } from "@/lib/utils";
|
|
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
|
|
import { userService } from "@/modules/user/user.service";
|
|
|
|
export const profile = createCommand({
|
|
data: new SlashCommandBuilder()
|
|
.setName("profile")
|
|
.setDescription("View your or another user's profile")
|
|
.addUserOption(option =>
|
|
option.setName("user")
|
|
.setDescription("The user to view")
|
|
.setRequired(false)
|
|
),
|
|
execute: async (interaction) => {
|
|
await interaction.deferReply();
|
|
|
|
const targetUser = interaction.options.getUser("user") || interaction.user;
|
|
const targetMember = await interaction.guild?.members.fetch(targetUser.id).catch(() => null);
|
|
|
|
let user = await userService.getUserById(targetUser.id);
|
|
|
|
if (!user) {
|
|
// Auto-create user if they don't exist
|
|
// Assuming no class assigned initially (null)
|
|
user = await userService.createUser(targetUser.id, targetUser.username, undefined);
|
|
}
|
|
|
|
// Refetch to get class relation if needed (though createUser returns user, it might not have relations loaded if we add them later)
|
|
// For now, let's assume we might need to join class manually or update userService to return it.
|
|
// Actually, let's just use what we have. If we need class name, we might need a separate query or update userService to include relation.
|
|
// Let's check if 'class' is in the returned user object from userService.getUserById.
|
|
// Looking at userService.ts, it uses findFirst. If we want relations, we need to add `with`.
|
|
|
|
// Let's quickly re-fetch with relations to be safe and get Class Name
|
|
// Or we can update userService given we are in "Implement Commands" but changing service might be out of scope?
|
|
// No, I should make sure the command works.
|
|
// Let's rely on standard Drizzle query here for the "view" part or update service.
|
|
// Updating service is cleaner.
|
|
|
|
const embed = new EmbedBuilder()
|
|
.setTitle(`${targetUser.username}'s Profile`)
|
|
.setThumbnail(targetUser.displayAvatarURL({ size: 512 }))
|
|
.setColor(targetMember?.displayHexColor || "Random")
|
|
.addFields(
|
|
{ name: "Level", value: `${user!.level || 1}`, inline: true },
|
|
{ name: "XP", value: `${user!.xp || 0}`, inline: true },
|
|
{ name: "Balance", value: `${user!.balance || 0}`, inline: true },
|
|
{ name: "Class", value: user!.class?.name || "None", inline: true },
|
|
{ name: "Joined Discord", value: `<t:${Math.floor(targetUser.createdTimestamp / 1000)}:R>`, inline: true },
|
|
{ name: "Joined Server", value: targetMember ? `<t:${Math.floor(targetMember.joinedTimestamp! / 1000)}:R>` : "Unknown", inline: true }
|
|
)
|
|
.setFooter({ text: `ID: ${targetUser.id}` })
|
|
.setTimestamp();
|
|
|
|
await interaction.editReply({ embeds: [embed] });
|
|
}
|
|
});
|