156 lines
No EOL
6.5 KiB
TypeScript
156 lines
No EOL
6.5 KiB
TypeScript
import { AutocompleteInteraction } from "discord.js";
|
|
import { resolveUser } from "@systems/users";
|
|
import { getCharacters } from "@systems/characters";
|
|
import { Config } from "@systems/config";
|
|
import { Emoji } from "@systems/emojis";
|
|
import { CharacterRegistry } from "@registry/character-registry";
|
|
import { UserRegistry } from "@registry/user-registry";
|
|
import { Paths } from "@helpers/paths";
|
|
import { Nation } from "@types";
|
|
import { NATION_UNICODE } from "@systems/nations";
|
|
import { autocompleteLayout } from "@subcommands/tg-config/set-layout";
|
|
import { UpdatesCommands } from "@subcommands/admin/updates";
|
|
import { ResultCommands } from "@subcommands/admin/result-post";
|
|
import { SetResultLayoutCommands } from "@subcommands/tg-config/set-result-layout";
|
|
import { SetLeaderboardLayoutCommands } from "@subcommands/tg-config/set-leaderboard-layout";
|
|
import fs from "fs";
|
|
import { AnnouncementCommands } from "../subcommands/admin/announcement";
|
|
|
|
// ─── Usermap cache ────────────────────────────────────────────────────────────
|
|
let _usermapCache: Record<string, any> | null = null;
|
|
|
|
function getUsermapCache(): Record<string, any> {
|
|
if (!_usermapCache) {
|
|
try { _usermapCache = JSON.parse(fs.readFileSync(Paths.data("usermap.json"), "utf8")); }
|
|
catch { _usermapCache = {}; }
|
|
}
|
|
return _usermapCache!;
|
|
}
|
|
|
|
export function invalidateUsermapCache(): void { _usermapCache = null; }
|
|
|
|
// ─── Autocomplete subsets ─────────────────────────────────────────────────────
|
|
|
|
async function autocompleteCharNames(
|
|
interaction: AutocompleteInteraction,
|
|
focused: string,
|
|
nation?: Nation | null // optional — if provided, filter by nation
|
|
): Promise<void> {
|
|
const member = await interaction.guild!.members.fetch(interaction.user.id);
|
|
const user = await resolveUser(member);
|
|
|
|
// For bringer set — scan all chars by nation, no user filter needed
|
|
if (nation !== undefined) {
|
|
const all = CharacterRegistry.all();
|
|
const results = all
|
|
.filter((c) => !nation || c.nation === nation)
|
|
.filter((c) => c.name.toLowerCase().includes(focused.toLowerCase()))
|
|
.map((c) => {
|
|
const classKey = typeof c.class === "object" ? c.class?.key : c.class;
|
|
return {
|
|
name: `${classKey} ${c.level} ${c.name} [${c.nation}]`.trim(),
|
|
value: c.name
|
|
};
|
|
})
|
|
.slice(0, 25);
|
|
return interaction.respond(results);
|
|
}
|
|
|
|
if (!user.userKey) return interaction.respond([]);
|
|
|
|
// Own chars
|
|
const ownChars = getCharacters(user.userKey).map((c) => {
|
|
const classKey = typeof c.class === "object" ? c.class?.key : c.class;
|
|
return {
|
|
name: `${classKey} ${c.level} ${c.name} [${c.nation}]`.trim(),
|
|
value: c.name,
|
|
};
|
|
});
|
|
|
|
// Shared chars
|
|
const sharedChars = CharacterRegistry.sharedWith(user.userKey).map(({ char }) => {
|
|
const classKey = typeof char.class === "object" ? char.class?.key : char.class;
|
|
return {
|
|
name: `${classKey} ${char.level} ${char.name} [${char.nation}] 🔗`.trim(),
|
|
value: char.name,
|
|
};
|
|
});
|
|
|
|
const all = [...ownChars, ...sharedChars]
|
|
.filter((c) => c.name.toLowerCase().includes(focused.toLowerCase()))
|
|
.slice(0, 25);
|
|
|
|
await interaction.respond(all);
|
|
}
|
|
|
|
async function autocompleteUserKeys(
|
|
interaction: AutocompleteInteraction,
|
|
focused: string
|
|
): Promise<void> {
|
|
try {
|
|
const usermap = getUsermapCache();
|
|
const choices = Object.entries(usermap)
|
|
.map(([, entry]: [string, any]) => {
|
|
const fileKey = typeof entry === "string" ? entry : entry.file;
|
|
const alias = typeof entry === "object" ? (entry.aliases?.[0] ?? fileKey) : fileKey;
|
|
return { name: `${alias} (${fileKey})`, value: fileKey };
|
|
})
|
|
.filter((c) => c.name.toLowerCase().includes(focused.toLowerCase()))
|
|
.slice(0, 25);
|
|
await interaction.respond(choices);
|
|
} catch {
|
|
await interaction.respond([]);
|
|
}
|
|
}
|
|
|
|
async function autocompleteSlots(
|
|
interaction: AutocompleteInteraction,
|
|
focused: string
|
|
): Promise<void> {
|
|
const slots = Config.get({ section: "poll", key: "slots" })
|
|
.filter((s) => s.active)
|
|
.map((s) => ({ name: `${s.tgHour}:00`, value: String(s.tgHour) }))
|
|
.filter((s) => s.name.includes(focused));
|
|
await interaction.respond(slots);
|
|
}
|
|
|
|
// ─── Router ───────────────────────────────────────────────────────────────────
|
|
|
|
export async function handleAutocomplete(interaction: AutocompleteInteraction): Promise<void> {
|
|
try {
|
|
const focused = interaction.options.getFocused(true);
|
|
const optionName = focused.name;
|
|
const focusedValue = focused.value as string;
|
|
const sub = interaction.options.getSubcommand(false);
|
|
const subGroup = interaction.options.getSubcommandGroup(false);
|
|
|
|
if (optionName === "char_name") {
|
|
// Bringer set — filter by selected nation
|
|
if (sub === "set" && subGroup === "bringer") {
|
|
const nation = interaction.options.getString("nation") as Nation | null;
|
|
return await autocompleteCharNames(interaction, focusedValue, nation);
|
|
}
|
|
return await autocompleteCharNames(interaction, focusedValue);
|
|
}
|
|
|
|
if (optionName === "name") return await autocompleteUserKeys(interaction, focusedValue);
|
|
if (optionName === "slot") return await autocompleteSlots(interaction, focusedValue);
|
|
if (optionName === "owner") return await autocompleteUserKeys(interaction, focusedValue);
|
|
if (optionName === "version") return UpdatesCommands.autocomplete(interaction);
|
|
if (optionName === "history_key") return await ResultCommands.autocompleteHistory(interaction);
|
|
if (optionName === "week_key") return await ResultCommands.autocompleteWeekKey(interaction);
|
|
|
|
if (optionName === "layout") {
|
|
if (sub === "set-result-layout") return await SetResultLayoutCommands.autocomplete(interaction);
|
|
if (sub === "set-leaderboard-layout") return await SetLeaderboardLayoutCommands.autocomplete(interaction);
|
|
return await autocompleteLayout(interaction); // poll default
|
|
}
|
|
|
|
if (optionName === "id") return await AnnouncementCommands.autocomplete(interaction);
|
|
|
|
await interaction.respond([]);
|
|
} catch (err) {
|
|
console.error("[autocomplete] error:", err);
|
|
try { await interaction.respond([]); } catch {}
|
|
}
|
|
} |