-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemoji.ts
63 lines (56 loc) · 1.65 KB
/
emoji.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { Env } from "../env";
const parseEmoji = (env?: string): Record<string, string> => {
try {
const raw = JSON.parse(env || "{}");
if (
typeof raw !== "object" ||
raw === null ||
Object.entries(raw).some(
([key, value]) =>
typeof key !== "string" || typeof value !== "string",
)
)
throw new Error("Invalid JSON");
return raw;
} catch (e) {
return {};
}
};
const hash = (str: string) => {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = (hash << 5) - hash + str.charCodeAt(i);
hash |= 0;
}
return hash;
};
const hearts = [
":heart:",
":orange_heart:",
":yellow_heart:",
":green_heart:",
":blue_heart:",
":purple_heart:",
];
let emojiCausesCache: Record<string, string> | null = null;
export const emojiCauses = (env: Env, cause: string) => {
const obj =
emojiCausesCache ||
(emojiCausesCache = parseEmoji(env.DISCORD_CAUSES_EMOJI));
return obj[cause] || hearts[Math.abs(hash(cause)) % hearts.length];
};
const regular = {
mascot: ":snowflake:",
happy: ":smile:",
hype: ":partying_face:",
love: ":heart_eyes:",
sad: ":sob:",
};
let emojiRegularCache: Record<string, string> | null = null;
export const emojiRegular = (env: Env, type: keyof typeof regular) => {
if (!(type in regular)) throw new Error(`Invalid emoji type: ${type}`);
const obj =
emojiRegularCache ||
(emojiRegularCache = parseEmoji(env.DISCORD_REGULAR_EMOJI));
return obj[type] || regular[type as keyof typeof regular];
};