tg-bot-ts/src/systems/impersonate.ts

46 lines
No EOL
1.4 KiB
TypeScript

import { UserRegistry } from "@registry/user-registry";
import { UsermapEntry } from "@types";
import { Config } from "@systems/config";
const IMPERSONATE_RESET_ON_POLL = Config.get("impersonateResetOnPoll");
const IMPERSONATE_INDICATOR = Config.get("impersonateIndicator");
// realDiscordId → userKey being impersonated
const impersonations = new Map<string, string>();
export function setImpersonation(realDiscordId: string, userKey: string): void {
impersonations.set(realDiscordId, userKey);
}
export function clearImpersonation(realDiscordId: string): void {
impersonations.delete(realDiscordId);
}
export function getImpersonation(realDiscordId: string): string | null {
return impersonations.get(realDiscordId) ?? null;
}
export function clearAllImpersonations(): void {
if (IMPERSONATE_RESET_ON_POLL) impersonations.clear();
}
export function shouldShowIndicator(): boolean {
return IMPERSONATE_INDICATOR;
}
// Returns all registered userKeys from usermap.json
export function getRegisteredUsers(): { userKey: string; aliases: string[] }[] {
try {
const entries = UserRegistry.all();
const seen = new Set<string>();
return entries.map(({ entry }: { entry: UsermapEntry }) => ({ userKey: entry.file, aliases: entry.aliases ?? [] }))
.filter(({ userKey }) => {
if (seen.has(userKey)) return false;
seen.add(userKey);
return true;
});
} catch {
return [];
}
}