-
Notifications
You must be signed in to change notification settings - Fork 9
/
Embed.ts
135 lines (117 loc) · 3.22 KB
/
Embed.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import type {
APIEmbed,
APIEmbedAuthor,
APIEmbedField,
APIEmbedFooter,
APIEmbedImage,
APIEmbedProvider,
} from "discord-api-types/v9";
import { Child, isEmptyChild } from "../helpers";
import { $field } from "./Field";
export const $embed = /* @__PURE__ */ Symbol("$embed");
export interface EmbedFooterProps {
text: string;
iconUrl?: string;
proxyIconUrl?: string;
}
export interface EmbedMediaProps {
url?: string;
proxyUrl?: string;
width?: number;
height?: number;
}
export interface EmbedProviderProps {
name?: string;
url?: string;
}
export interface EmbedAuthorProps {
name: string;
url?: string;
iconUrl?: string;
proxyIconUrl?: string;
}
export interface EmbedProps {
title?: string;
url?: string;
timestamp?: string | Date;
color?: number;
image?: string | EmbedMediaProps; // Just URL or full image
thumbnail?: string | EmbedMediaProps; // Just URL or full thumbnail
video?: string | EmbedMediaProps; // Just URL or full video
footer?: string | EmbedFooterProps; // Just text or full footer
provider?: string | EmbedProviderProps; // Just name or full provider
author?: string | EmbedAuthorProps; // Just name or full author
children?: (Child | (APIEmbedField & { [$field]: true }))[]; // For description and embeds
}
function normaliseMedia(
props?: string | EmbedMediaProps
): APIEmbedImage | undefined {
if (!props) return;
if (typeof props === "string") return { url: props };
return {
url: props.url as any,
proxy_url: props.proxyUrl,
width: props.width,
height: props.height,
};
}
function normaliseFooter(
props?: string | EmbedFooterProps
): APIEmbedFooter | undefined {
if (!props) return;
if (typeof props === "string") return { text: props };
return {
text: props.text,
icon_url: props.iconUrl,
proxy_icon_url: props.proxyIconUrl,
};
}
function normaliseProvider(
props?: string | EmbedProviderProps
): APIEmbedProvider | undefined {
return typeof props === "string" ? { name: props } : props;
}
function normaliseAuthor(
props?: string | EmbedAuthorProps
): APIEmbedAuthor | undefined {
if (!props) return;
if (typeof props === "string") return { name: props };
return {
name: props.name,
url: props.url,
icon_url: props.iconUrl,
proxy_icon_url: props.proxyIconUrl,
};
}
export function Embed(props: EmbedProps): APIEmbed & { [$embed]: true } {
let description = undefined;
const fields: APIEmbedField[] = [];
for (const child of props.children?.flat(Infinity) ?? []) {
if (isEmptyChild(child)) continue;
if ((child as any)[$field]) {
fields.push(child as any);
} else {
description ??= "";
description += child;
}
}
return {
[$embed]: true,
title: props.title,
description,
url: props.url,
timestamp: props.timestamp
? typeof props.timestamp === "string"
? props.timestamp
: props.timestamp.toISOString()
: undefined,
color: props.color,
image: normaliseMedia(props.image),
thumbnail: normaliseMedia(props.thumbnail),
video: normaliseMedia(props.video),
footer: normaliseFooter(props.footer),
provider: normaliseProvider(props.provider),
author: normaliseAuthor(props.author),
fields,
};
}