-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathEmbed.ts
More file actions
107 lines (94 loc) · 2.67 KB
/
Embed.ts
File metadata and controls
107 lines (94 loc) · 2.67 KB
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
import type { APIEmbed } from 'discord-api-types/v10';
import { Structure } from '../../Structure.js';
import { dateToDiscordISOTimestamp } from '../../utils/optimization.js';
import { kCreatedTimestamp, kData } from '../../utils/symbols.js';
import type { Partialize } from '../../utils/types.js';
/**
* Represents an embed on a message.
*
* @typeParam Omitted - Specify the properties that will not be stored in the raw data field as a union, implement via `DataTemplate`
* @remarks has substructures `EmbedAuthor`, `EmbedFooter`, `EmbedField`, `EmbedImage`, `EmbedThumbnail`, `EmbedProvider`, `EmbedVideo` which need to be instantiated and stored by an extending class using it
*/
export class Embed<Omitted extends keyof APIEmbed | '' = ''> extends Structure<APIEmbed, Omitted> {
/**
* The template used for removing data from the raw data stored for each Embed.
*
* @remarks This template has defaults, if you want to remove additional data and keep the defaults,
* use `Object.defineProperties`. To override the defaults, set this value directly.
*/
public static override readonly DataTemplate: Partial<APIEmbed> = {
set timestamp(_: string) {},
};
protected [kCreatedTimestamp]: number | null = null;
/**
* @param data - The raw data received from the API for the connection
*/
public constructor(data: Partialize<APIEmbed, Omitted>) {
super(data);
this.optimizeData(data);
}
/**
* {@inheritDoc Structure.optimizeData}
*
* @internal
*/
protected override optimizeData(data: Partial<APIEmbed>) {
if (data.timestamp) {
this[kCreatedTimestamp] = Date.parse(data.timestamp);
}
}
/**
* The color code of the embed
*/
public get color() {
return this[kData].color;
}
/**
* The hexadecimal version of the embed color, with a leading hash
*/
public get hexColor() {
const color = this.color;
if (typeof color !== 'number') return color;
return `#${color.toString(16).padStart(6, '0')}`;
}
/**
* The description of the embed
*/
public get description() {
return this[kData].description;
}
/**
* THe title of the embed
*/
public get title() {
return this[kData].title;
}
/**
* The timestamp of the embed content
*/
public get timestamp() {
return this[kCreatedTimestamp];
}
/**
* The type of embed (always "rich" for webhook embeds)
*/
public get type() {
return this[kData].type;
}
/**
* The URL of the embed
*/
public get url() {
return this[kData].url;
}
/**
* {@inheritDoc Structure.toJSON}
*/
public override toJSON() {
const clone = super.toJSON();
if (this[kCreatedTimestamp]) {
clone.timestamp = dateToDiscordISOTimestamp(new Date(this[kCreatedTimestamp]));
}
return clone;
}
}