autocomplete.ts
· 6.4 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";
// ─── 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 nationEmoji = c.nation ? (NATION_UNICODE[c.nation] || c.nation) : "";
return { name: `${c.class} ${c.level} ${c.name} ${nationEmoji}`.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 nationEmoji = c.nation ? (Emoji.nation(c.nation) || c.nation) : "";
return {
name: `${c.class} ${c.level} ${c.name} ${nationEmoji}`.trim(),
value: c.name,
};
});
// Shared chars
const sharedChars = CharacterRegistry.sharedWith(user.userKey).map(({ char }) => {
const nationEmoji = char.nation ? (Emoji.nation(char.nation) || char.nation) : "";
return {
name: `${char.class} ${char.level} ${char.name} 🔗 ${nationEmoji}`.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 === "layout") return await autocompleteLayout(interaction);
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
}
await interaction.respond([]);
} catch (err) {
console.error("[autocomplete] error:", err);
try { await interaction.respond([]); } catch {}
}
}
| 1 | import { AutocompleteInteraction } from "discord.js"; |
| 2 | import { resolveUser } from "@systems/users"; |
| 3 | import { getCharacters } from "@systems/characters"; |
| 4 | import { Config } from "@systems/config"; |
| 5 | import { Emoji } from "@systems/emojis"; |
| 6 | import { CharacterRegistry } from "@registry/character-registry"; |
| 7 | import { UserRegistry } from "@registry/user-registry"; |
| 8 | import { Paths } from "@helpers/paths"; |
| 9 | import { Nation } from "@types"; |
| 10 | import { NATION_UNICODE } from "@systems/nations"; |
| 11 | import { autocompleteLayout } from "@subcommands/tg-config/set-layout"; |
| 12 | import { UpdatesCommands } from "@subcommands/admin/updates"; |
| 13 | import { ResultCommands } from "@subcommands/admin/result-post"; |
| 14 | import { SetResultLayoutCommands } from "@subcommands/tg-config/set-result-layout"; |
| 15 | import { SetLeaderboardLayoutCommands } from "@subcommands/tg-config/set-leaderboard-layout"; |
| 16 | import fs from "fs"; |
| 17 | |
| 18 | // ─── Usermap cache ──────────────────────────────────────────────────────────── |
| 19 | let _usermapCache: Record<string, any> | null = null; |
| 20 | |
| 21 | function getUsermapCache(): Record<string, any> { |
| 22 | if (!_usermapCache) { |
| 23 | try { _usermapCache = JSON.parse(fs.readFileSync(Paths.data("usermap.json"), "utf8")); } |
| 24 | catch { _usermapCache = {}; } |
| 25 | } |
| 26 | return _usermapCache!; |
| 27 | } |
| 28 | |
| 29 | export function invalidateUsermapCache(): void { _usermapCache = null; } |
| 30 | |
| 31 | // ─── Autocomplete subsets ───────────────────────────────────────────────────── |
| 32 | |
| 33 | async function autocompleteCharNames( |
| 34 | interaction: AutocompleteInteraction, |
| 35 | focused: string, |
| 36 | nation?: Nation | null // optional — if provided, filter by nation |
| 37 | ): Promise<void> { |
| 38 | const member = await interaction.guild!.members.fetch(interaction.user.id); |
| 39 | const user = await resolveUser(member); |
| 40 | |
| 41 | // For bringer set — scan all chars by nation, no user filter needed |
| 42 | if (nation !== undefined) { |
| 43 | const all = CharacterRegistry.all(); |
| 44 | const results = all |
| 45 | .filter((c) => !nation || c.nation === nation) |
| 46 | .filter((c) => c.name.toLowerCase().includes(focused.toLowerCase())) |
| 47 | .map((c) => { |
| 48 | const nationEmoji = c.nation ? (NATION_UNICODE[c.nation] || c.nation) : ""; |
| 49 | return { name: `${c.class} ${c.level} ${c.name} ${nationEmoji}`.trim(), value: c.name }; |
| 50 | }) |
| 51 | .slice(0, 25); |
| 52 | return interaction.respond(results); |
| 53 | } |
| 54 | |
| 55 | if (!user.userKey) return interaction.respond([]); |
| 56 | |
| 57 | // Own chars |
| 58 | const ownChars = getCharacters(user.userKey).map((c) => { |
| 59 | const nationEmoji = c.nation ? (Emoji.nation(c.nation) || c.nation) : ""; |
| 60 | return { |
| 61 | name: `${c.class} ${c.level} ${c.name} ${nationEmoji}`.trim(), |
| 62 | value: c.name, |
| 63 | }; |
| 64 | }); |
| 65 | |
| 66 | // Shared chars |
| 67 | const sharedChars = CharacterRegistry.sharedWith(user.userKey).map(({ char }) => { |
| 68 | const nationEmoji = char.nation ? (Emoji.nation(char.nation) || char.nation) : ""; |
| 69 | return { |
| 70 | name: `${char.class} ${char.level} ${char.name} 🔗 ${nationEmoji}`.trim(), |
| 71 | value: char.name, |
| 72 | }; |
| 73 | }); |
| 74 | |
| 75 | const all = [...ownChars, ...sharedChars] |
| 76 | .filter((c) => c.name.toLowerCase().includes(focused.toLowerCase())) |
| 77 | .slice(0, 25); |
| 78 | |
| 79 | await interaction.respond(all); |
| 80 | } |
| 81 | |
| 82 | async function autocompleteUserKeys( |
| 83 | interaction: AutocompleteInteraction, |
| 84 | focused: string |
| 85 | ): Promise<void> { |
| 86 | try { |
| 87 | const usermap = getUsermapCache(); |
| 88 | const choices = Object.entries(usermap) |
| 89 | .map(([, entry]: [string, any]) => { |
| 90 | const fileKey = typeof entry === "string" ? entry : entry.file; |
| 91 | const alias = typeof entry === "object" ? (entry.aliases?.[0] ?? fileKey) : fileKey; |
| 92 | return { name: `${alias} (${fileKey})`, value: fileKey }; |
| 93 | }) |
| 94 | .filter((c) => c.name.toLowerCase().includes(focused.toLowerCase())) |
| 95 | .slice(0, 25); |
| 96 | await interaction.respond(choices); |
| 97 | } catch { |
| 98 | await interaction.respond([]); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | async function autocompleteSlots( |
| 103 | interaction: AutocompleteInteraction, |
| 104 | focused: string |
| 105 | ): Promise<void> { |
| 106 | const slots = Config.get({ section: "poll", key: "slots" }) |
| 107 | .filter((s) => s.active) |
| 108 | .map((s) => ({ name: `${s.tgHour}:00`, value: String(s.tgHour) })) |
| 109 | .filter((s) => s.name.includes(focused)); |
| 110 | await interaction.respond(slots); |
| 111 | } |
| 112 | |
| 113 | // ─── Router ─────────────────────────────────────────────────────────────────── |
| 114 | |
| 115 | export async function handleAutocomplete(interaction: AutocompleteInteraction): Promise<void> { |
| 116 | try { |
| 117 | const focused = interaction.options.getFocused(true); |
| 118 | const optionName = focused.name; |
| 119 | const focusedValue = focused.value as string; |
| 120 | const sub = interaction.options.getSubcommand(false); |
| 121 | const subGroup = interaction.options.getSubcommandGroup(false); |
| 122 | |
| 123 | if (optionName === "char_name") { |
| 124 | // Bringer set — filter by selected nation |
| 125 | if (sub === "set" && subGroup === "bringer") { |
| 126 | const nation = interaction.options.getString("nation") as Nation | null; |
| 127 | return await autocompleteCharNames(interaction, focusedValue, nation); |
| 128 | } |
| 129 | return await autocompleteCharNames(interaction, focusedValue); |
| 130 | } |
| 131 | |
| 132 | if (optionName === "name") return await autocompleteUserKeys(interaction, focusedValue); |
| 133 | if (optionName === "slot") return await autocompleteSlots(interaction, focusedValue); |
| 134 | if (optionName === "owner") return await autocompleteUserKeys(interaction, focusedValue); |
| 135 | if (optionName === "layout") return await autocompleteLayout(interaction); |
| 136 | if (optionName === "version") return UpdatesCommands.autocomplete(interaction); |
| 137 | if (optionName === "history_key") return await ResultCommands.autocompleteHistory(interaction); |
| 138 | if (optionName === "week_key") return await ResultCommands.autocompleteWeekKey(interaction); |
| 139 | |
| 140 | if (optionName === "layout") { |
| 141 | if (sub === "set-result-layout") return await SetResultLayoutCommands.autocomplete(interaction); |
| 142 | if (sub === "set-leaderboard-layout") return await SetLeaderboardLayoutCommands.autocomplete(interaction); |
| 143 | return await autocompleteLayout(interaction); // poll default |
| 144 | } |
| 145 | |
| 146 | await interaction.respond([]); |
| 147 | } catch (err) { |
| 148 | console.error("[autocomplete] error:", err); |
| 149 | try { await interaction.respond([]); } catch {} |
| 150 | } |
| 151 | } |