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