gistfile1.txt
· 2.3 KiB · Text
Исходник
import { ChatInputCommandInteraction } from "discord.js";
import { cfg } from "../../systems/config";
import { resolveUser, hasOfficerRole } from "../../systems/users";
import { setActiveCharacter } from "../../systems/characters";
import { setSessionBorrow } from "../../systems/borrow";
import { replyAndDelete } from "../../utils";
import fs from "fs";
import path from "path";
const CHARS_PATH = path.join(__dirname, "../../../data/characters.json");
function findSharedChar(usermapKey: string, charName: string): { ownerKey: string; charName: string } | null {
try {
const chars = JSON.parse(fs.readFileSync(CHARS_PATH, "utf8"));
for (const [ownerKey, data] of Object.entries(chars) as [string, any][]) {
if (ownerKey === usermapKey) continue;
const char = data.characters?.find(
(c: any) => c.name.toLowerCase() === charName.toLowerCase() && c.sharedWith?.includes(usermapKey)
);
if (char) return { ownerKey, charName: char.name };
}
} catch {}
return null;
}
export async function handleCharSetActive(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);
let usermapKey: string | null;
if (nameArg) {
if (!isOfficer) return void replyAndDelete(interaction, "❌ Only officers can manage other players' characters.");
usermapKey = nameArg;
} else {
const user = await resolveUser(member);
usermapKey = user.usermapKey;
}
if (!usermapKey) return void replyAndDelete(interaction, "❌ You are not registered in the system.");
// Try own characters first
const set = setActiveCharacter(usermapKey, charName);
if (set) return void replyAndDelete(interaction, `✅ **${charName}** is now your active character.`);
// Fall back to shared characters
const shared = findSharedChar(usermapKey, charName);
if (shared) {
setSessionBorrow(usermapKey, shared.ownerKey, shared.charName);
return void replyAndDelete(interaction, `✅ **${charName}** (shared by **${shared.ownerKey}**) set as active for this session.`);
}
return void replyAndDelete(interaction, `❌ No character named **${charName}** found.`);
}
| 1 | import { ChatInputCommandInteraction } from "discord.js"; |
| 2 | import { cfg } from "../../systems/config"; |
| 3 | import { resolveUser, hasOfficerRole } from "../../systems/users"; |
| 4 | import { setActiveCharacter } from "../../systems/characters"; |
| 5 | import { setSessionBorrow } from "../../systems/borrow"; |
| 6 | import { replyAndDelete } from "../../utils"; |
| 7 | import fs from "fs"; |
| 8 | import path from "path"; |
| 9 | |
| 10 | const CHARS_PATH = path.join(__dirname, "../../../data/characters.json"); |
| 11 | |
| 12 | function findSharedChar(usermapKey: string, charName: string): { ownerKey: string; charName: string } | null { |
| 13 | try { |
| 14 | const chars = JSON.parse(fs.readFileSync(CHARS_PATH, "utf8")); |
| 15 | for (const [ownerKey, data] of Object.entries(chars) as [string, any][]) { |
| 16 | if (ownerKey === usermapKey) continue; |
| 17 | const char = data.characters?.find( |
| 18 | (c: any) => c.name.toLowerCase() === charName.toLowerCase() && c.sharedWith?.includes(usermapKey) |
| 19 | ); |
| 20 | if (char) return { ownerKey, charName: char.name }; |
| 21 | } |
| 22 | } catch {} |
| 23 | return null; |
| 24 | } |
| 25 | |
| 26 | export async function handleCharSetActive(interaction: ChatInputCommandInteraction): Promise<void> { |
| 27 | const member = await interaction.guild!.members.fetch(interaction.user.id); |
| 28 | const isOfficer = hasOfficerRole(member, cfg("officerRoles")); |
| 29 | const nameArg = interaction.options.getString("name"); |
| 30 | const charName = interaction.options.getString("char_name", true); |
| 31 | |
| 32 | let usermapKey: string | null; |
| 33 | if (nameArg) { |
| 34 | if (!isOfficer) return void replyAndDelete(interaction, "❌ Only officers can manage other players' characters."); |
| 35 | usermapKey = nameArg; |
| 36 | } else { |
| 37 | const user = await resolveUser(member); |
| 38 | usermapKey = user.usermapKey; |
| 39 | } |
| 40 | |
| 41 | if (!usermapKey) return void replyAndDelete(interaction, "❌ You are not registered in the system."); |
| 42 | |
| 43 | // Try own characters first |
| 44 | const set = setActiveCharacter(usermapKey, charName); |
| 45 | if (set) return void replyAndDelete(interaction, `✅ **${charName}** is now your active character.`); |
| 46 | |
| 47 | // Fall back to shared characters |
| 48 | const shared = findSharedChar(usermapKey, charName); |
| 49 | if (shared) { |
| 50 | setSessionBorrow(usermapKey, shared.ownerKey, shared.charName); |
| 51 | return void replyAndDelete(interaction, `✅ **${charName}** (shared by **${shared.ownerKey}**) set as active for this session.`); |
| 52 | } |
| 53 | |
| 54 | return void replyAndDelete(interaction, `❌ No character named **${charName}** found.`); |
| 55 | } |