33 lines
1.6 KiB
TypeScript
33 lines
1.6 KiB
TypeScript
import { ChatInputCommandInteraction } from "discord.js";
|
|
import { Config } from "../../systems/config";
|
|
import { resolveUser, hasOfficerRole } from "../../systems/users";
|
|
import { setCharacterNation, getActiveCharacter } from "../../systems/characters";
|
|
import { replyAndDelete } from "../../utils";
|
|
import { Nation } from "../../types";
|
|
|
|
export async function handleCharSetNation(interaction: ChatInputCommandInteraction): Promise<void> {
|
|
const member = await interaction.guild!.members.fetch(interaction.user.id);
|
|
const isOfficer = hasOfficerRole(member, Config.get({ section: "roles", key: "officer" }));
|
|
const nameArg = interaction.options.getString("name");
|
|
const nation = interaction.options.getString("nation", true) as Nation;
|
|
const charName = interaction.options.getString("char_name"); // optional, defaults to active
|
|
|
|
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 targetName = charName ?? getActiveCharacter(userKey)?.name;
|
|
if (!targetName) return replyAndDelete(interaction, "❌ No active character found. Specify a character name.");
|
|
|
|
const set = setCharacterNation(userKey, targetName, nation);
|
|
if (!set) return replyAndDelete(interaction, `❌ No character named **${targetName}** found.`);
|
|
|
|
return replyAndDelete(interaction, `✅ **«${targetName}»** nation set to **${nation}**.`);
|
|
}
|