48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
import type { APIMessageComponentEmoji } from "discord.js";
|
|
|
|
const CUSTOM_EMOJI_RE = /^<(?<animated>a?):(?<name>[A-Za-z0-9_]+):(?<id>\d+)>$/;
|
|
|
|
export function parseColor(input: string): number | undefined {
|
|
const trimmed = input.trim();
|
|
if (!trimmed) return undefined;
|
|
const normalized = trimmed.startsWith("#") ? trimmed.slice(1) : trimmed;
|
|
if (!/^[0-9a-fA-F]{6}$/.test(normalized)) return undefined;
|
|
return Number.parseInt(normalized, 16);
|
|
}
|
|
|
|
export function formatColor(color?: number): string {
|
|
if (color === undefined) return "Not set";
|
|
return `#${color.toString(16).padStart(6, "0").toUpperCase()}`;
|
|
}
|
|
|
|
export function customEmojiId(input?: string): string | undefined {
|
|
if (!input) return undefined;
|
|
return input.trim().match(CUSTOM_EMOJI_RE)?.groups?.id;
|
|
}
|
|
|
|
export function emojiForButton(input?: string): APIMessageComponentEmoji | string | undefined {
|
|
if (!input?.trim()) return undefined;
|
|
const emoji = input.trim();
|
|
const match = emoji.match(CUSTOM_EMOJI_RE);
|
|
if (!match?.groups) return emoji;
|
|
return {
|
|
id: match.groups.id,
|
|
name: match.groups.name,
|
|
animated: match.groups.animated === "a",
|
|
};
|
|
}
|
|
|
|
export function reactionEmojiKey(name: string | null, id: string | null, animated = false): string {
|
|
if (id && name) return `<${animated ? "a" : ""}:${name}:${id}>`;
|
|
return name ?? "";
|
|
}
|
|
|
|
export function emojiMatches(storedEmoji: string | undefined, reactionName: string | null, reactionId: string | null): boolean {
|
|
if (!storedEmoji) return false;
|
|
const stored = storedEmoji.trim();
|
|
const reactionKey = reactionEmojiKey(reactionName, reactionId);
|
|
if (stored === reactionKey) return true;
|
|
if (reactionId && customEmojiId(stored) === reactionId) return true;
|
|
return stored === (reactionName ?? "");
|
|
}
|