Last active 3 weeks ago

gistfile1.txt Raw
1import fs from "fs";
2import path from "path";
3
4const IMPERSONATE_RESET_ON_POLL = process.env.IMPERSONATE_RESET_ON_POLL !== "false";
5const IMPERSONATE_INDICATOR = process.env.IMPERSONATE_INDICATOR !== "false";
6
7// realDiscordId → userKey being impersonated
8const impersonations = new Map<string, string>();
9
10export function setImpersonation(realDiscordId: string, userKey: string): void {
11 impersonations.set(realDiscordId, userKey);
12}
13
14export function clearImpersonation(realDiscordId: string): void {
15 impersonations.delete(realDiscordId);
16}
17
18export function getImpersonation(realDiscordId: string): string | null {
19 return impersonations.get(realDiscordId) ?? null;
20}
21
22export function clearAllImpersonations(): void {
23 if (IMPERSONATE_RESET_ON_POLL) impersonations.clear();
24}
25
26export function shouldShowIndicator(): boolean {
27 return IMPERSONATE_INDICATOR;
28}
29
30// Returns all registered userKeys from usermap.json
31export function getRegisteredUsers(): { userKey: string; aliases: string[] }[] {
32 try {
33 const usermap = JSON.parse(
34 fs.readFileSync(path.join(__dirname, "../../data/usermap.json"), "utf8")
35 );
36 return Object.entries(usermap).map(([, entry]: [string, any]) => {
37 const fileKey = typeof entry === "string" ? entry : entry.file;
38 const aliases = typeof entry === "object" ? (entry.aliases ?? []) : [];
39 return { userKey: fileKey, aliases };
40 });
41 } catch {
42 return [];
43 }
44}