-
Notifications
You must be signed in to change notification settings - Fork 15
/
embed.js
282 lines (248 loc) · 6.71 KB
/
embed.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Represents an Embed in a Discord webhook message.
*/
class Embed {
/**
* @constructor
* @param {Author} [author]
* @param {Body} [body]
* @param {Field[]} [fields=[]] An array of fields.
* @param {string} [imageUrl] An image to show on the Embed.
* @param {string} [thumbnailUrl] A URL pointing to a thumbnail image.
* @param {Footer} [footer]
*/
constructor(author, body, fields, imageUrl, thumbnailUrl, footer) {
this._author = author;
this._body = body;
this._fields = fields || [];
this._imageUrl = imageUrl;
this._thumbnailUrl = thumbnailUrl;
this._footer = footer;
}
/**
* @param {Author} author
*/
set author(author) {this._author = author;}
/**
* @param {string} body
*/
set body(body) {this._body = body;}
/**
* @param {Fields[]} fields
*/
set fields(fields) {this._fields = fields;}
/**
* @param {string} imageUrl
*/
set imageUrl(imageUrl) {this._imageUrl = imageUrl;}
/**
* @param {string} thumbnailUrl
*/
set thumbnailUrl(thumbnailUrl) {this._thumbnailUrl = thumbnailUrl;}
/**
* @param {Footer} footer
*/
set footer(footer) {this._footer = footer;}
/**
* Adds a Field to the Embed.
* @param {Field} field The field to add.
*/
addField(field) {
this._fields.push(field);
}
/**
* Returns a JSON.stringify friendly object in the format used by Discord.
* @returns {object} Embed as a JSON object.
*/
toJSON() {
let obj = this._body ? this._body.toJSON() : {};
obj.author = this._author;
if (this._fields.length > 0) obj.fields = this._fields;
if (this._imageUrl) obj.image = {url: this._imageUrl}; //What on earth Discord?
if (this._thumbnailUrl) obj.thumbnail = {url: this._thumbnailUrl};
obj.footer = this._footer;
return obj;
}
}
/**
* Represents the Body of an Embed in a Discord webhook message.
*/
class Body {
/**
* @constructor
* @param {string} [title] The title of the embed.
* @param {string} [description] A description shown below the title.
* @param {string} [url] URL of the embed title.
* @param {string} [color=FFFFFF] The colour of the embed as a hexadecimal colour value (omit the _).
* @param {string} [timestamp] A timestamp to show on the footer.
*/
constructor(title, description, url, color, timestamp) {
this._title = title;
this._description = description;
this._url = url;
this._color = color || "FFFFFF";
this._timestamp = timestamp;
}
/**
* @param {string} title
*/
set title(title) {this._title = title;}
/**
* @param {string} description
*/
set description(description) {this._description = description;}
/**
* @param {string} url
*/
set url(url) {this._url = url;}
/**
* @param {string} color The colour of the embed as a hexadecimal colour value (omit the _).
*/
set color(color) {this._color = color;}
/**
* @returns {number} The embed colour in decimal.
*/
get color() {
return parseInt(this._color, 16);
}
/**
* @param {string} timestamp
*/
set timestamp(timestamp) {this._timestamp = timestamp;}
/**
* Sets the timestamp of the embed to the time this method is called.
*/
setDateToNow() {
this._timestamp = new Date().toISOString();
}
/**
* Returns a JSON.stringify friendly object in the format used by Discord.
* @returns {object} Body as a JSON object.
*/
toJSON() {
return {
title: this._title,
description: this._description,
url: this._url,
color: this.color,
timestamp: this._timestamp
};
}
}
/**
* Represents the Author of an Embed in a Discord webhook message.
*/
class Author {
/**
* @constructor
* @param {string} name The name of the Author.
* @param {string} [url] URL of the Author's name.
* @param {string} [iconUrl] The icon URL of the Author.
*/
constructor(name, url, iconUrl) {
this._name = name;
this._url = url;
this._iconUrl = iconUrl;
}
/**
* @param {string} name
*/
set name(name) {this._name = name;}
/**
* @param {string} url
*/
set url(url) {this._url = url;}
/**
* @param {string} iconUrl
*/
set iconUrl(iconUrl) {this._iconUrl = iconUrl;}
/**
* Returns a JSON.stringify friendly object in the format used by Discord.
* @returns {object} Author as a JSON string.
*/
toJSON() {
return {
name: this._name,
url: this._url,
icon_url: this._iconUrl
};
}
}
/**
* Represents a Field of an Embed in a Discord webhook message.
*/
class Field {
/**
* @constructor
* @param {string} name The name of the field.
* @param {string} description The description below the field title.
* @param {string} [isInline] Should the field be displayed inline.
*/
constructor(name, description, isInline) {
this._name = name;
this._description = description;
this._isInline = isInline;
}
/**
* @param {string} name
*/
set name(name) {this._name = name;}
/**
* @param {string} description
*/
set description(description) {this._description = description;}
/**
* @param {string} isInline
*/
set isInline(isInline) {this._isInline = isInline;}
/**
* Returns a JSON.stringify friendly object in the format used by Discord.
* @returns {object} Field as a JSON object.
*/
toJSON() {
return {
name: this._name,
value: this._description,
inline: this._isInline
};
}
}
/**
* Represents the Footer of an Embed in a Discord webhook message.
*/
class Footer {
/**
* @constructor
* @param {string} [text] The text shown on the footer.
* @param {string} [iconUrl] An icon to show on the footer.
*/
constructor(text, iconUrl) {
this._text = text;
this._iconUrl = iconUrl;
}
/**
* @param {string} text
*/
set text(text) {this._text = text;}
/**
* @param {string} iconUrl
*/
set iconUrl(iconUrl) {this._iconUrl = iconUrl;}
/**
* Returns a JSON.stringify friendly object in the format used by Discord.
* @returns {object} Footer as a JSON object.
*/
toJSON() {
return {
text: this._text,
icon_url: this._iconUrl,
};
}
}
module.exports = {
Embed,
Body,
Author,
Field,
Footer
};