tg-bot-ts/src/subcommands/char/add.ts
2026-06-03 01:51:26 +01:00

32 lines
1.6 KiB
TypeScript

import { ChatInputCommandInteraction } from "discord.js";
import { cfg } from "../../systems/config";
import { resolveUser, hasOfficerRole } from "../../systems/users";
import { addCharacter } from "../../systems/characters";
import { replyAndDelete } from "../../utils";
import { ClassKey, Nation } from "../../types";
export async function handleCharAdd(interaction: ChatInputCommandInteraction): Promise<void> {
const member = await interaction.guild!.members.fetch(interaction.user.id);
const isOfficer = hasOfficerRole(member, cfg("officerRoles"));
const nameArg = interaction.options.getString("name");
const charName = interaction.options.getString("char_name", true);
const cls = interaction.options.getString("class", true) as ClassKey;
const level = interaction.options.getInteger("level", true);
const nation = interaction.options.getString("nation", true) as Nation;
let userKey: string | null;
if (nameArg) {
if (!isOfficer) return replyAndDelete(interaction, "❌ Only officers can manage other players' characters.");
userKey = nameArg;
} else {
const user = await resolveUser(member);
userKey = user.userKey;
}
if (!userKey) return replyAndDelete(interaction, "❌ You are not registered in the system.");
const added = addCharacter(userKey, { name: charName, class: cls, level, nation });
if (!added) return replyAndDelete(interaction, `❌ A character named **${charName}** already exists.`);
return replyAndDelete(interaction, `✅ Character **«${charName}»** (${cls} · Lv${level} · ${nation}) added.`);
}