-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
EmbedBuilder.java
315 lines (296 loc) · 10.1 KB
/
EmbedBuilder.java
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright 2015-2016 Austin Keener & Michael Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core;
import java.awt.Color;
import java.time.OffsetDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import net.dv8tion.jda.core.entities.MessageEmbed;
import net.dv8tion.jda.core.entities.impl.MessageEmbedImpl;
public class EmbedBuilder
{
public final static int TITLE_MAX_LENGTH = 256;
public final static int VALUE_MAX_LENGTH = 1024;
public final static int TEXT_MAX_LENGTH = 2048;
public final static int URL_MAX_LENGTH = 2000;
public final static Pattern URL_PATTERN = Pattern.compile("\\s*https?:\\/\\/.+\\..{2,}\\s*", Pattern.CASE_INSENSITIVE);
public final static Pattern HTTPS_URL_PATTERN = Pattern.compile("\\s*https:\\/\\/.+\\..{2,}\\s*", Pattern.CASE_INSENSITIVE);
private String url;
private String title;
private String description;
private OffsetDateTime timestamp;
private Color color;
private MessageEmbed.Thumbnail thumbnail;
private MessageEmbed.Provider siteProvider;
private MessageEmbed.AuthorInfo author;
private MessageEmbed.VideoInfo videoInfo;
private MessageEmbed.Footer footer;
private MessageEmbed.ImageInfo image;
private final List<MessageEmbed.Field> fields;
/**
* Creates an EmbedBuilder to be used to creates an embed to send
*/
public EmbedBuilder()
{
fields = new LinkedList<>();
}
/**
* Returns a {@link net.dv8tion.jda.core.entities.MessageEmbed MessageEmbed}
* that has been checked as being valid for sending.
*
* @return the built, sendable MessageEmbed
*/
public MessageEmbed build()
{
if (title == null
&& url == null
&& description == null
&& timestamp == null
&& color == null
&& thumbnail == null
&& siteProvider == null
&& author == null
&& videoInfo == null
&& footer == null
&& image == null
&& fields.isEmpty())
{
throw new IllegalStateException("Cannot build an empty embed!");
}
return new MessageEmbedImpl().setTitle(title)
.setUrl(url)
.setDescription(description)
.setTimestamp(timestamp)
.setColor(color)
.setThumbnail(thumbnail)
.setSiteProvider(siteProvider)
.setAuthor(author)
.setVideoInfo(videoInfo)
.setFooter(footer)
.setImage(image)
.setFields(fields);
}
/**
* Sets the URL of the embed
* @param url the url of the embed
* @return the builder after the url has been set
*/
public EmbedBuilder setUrl(String url)
{
urlCheck(url);
this.url = url;
return this;
}
/**
* Sets the Title of the embed
* @param title the title of the embed
* @return the builder after the title has been set
*/
public EmbedBuilder setTitle(String title)
{
if (title != null && title.length() > TITLE_MAX_LENGTH)
throw new IllegalArgumentException("Title cannot be longer than " + TITLE_MAX_LENGTH + " characters.");
this.title = title;
return this;
}
/**
* Sets the Description of the embed.
* @param description the description of the embed
* @return the builder after the description has been set
*/
public EmbedBuilder setDescription(String description)
{
if (description != null && description.length() > TEXT_MAX_LENGTH)
throw new IllegalArgumentException("Description cannot be longer than " + TEXT_MAX_LENGTH + " characters.");
this.description = description;
return this;
}
/**
* Sets the Timestamp of the embed.
* @param timestamp the timestamp of the embed
* @return the builder after the timestamp has been set
*/
public EmbedBuilder setTimestamp(OffsetDateTime timestamp)
{
this.timestamp = timestamp;
return this;
}
/**
* Sets the Color of the embed.
* @param color the color of the embed
* @return the builder after the color has been set
*/
public EmbedBuilder setColor(Color color)
{
this.color = color;
return this;
}
/**
* Sets the Thumbnail of the embed.
* @param url the url of the thumbnail of the embed
* @return the builder after the thumbnail has been set
*/
public EmbedBuilder setThumbnail(String url)
{
if (url == null)
{
this.thumbnail = null;
}
else
{
httpsCheck(url);
this.thumbnail = new MessageEmbed.Thumbnail(url, null, 0, 0);
}
return this;
}
/**
* Sets the Video of the embed.
* @param url the url of the video of the embed
* @return the builder after the video has been set
*/
public EmbedBuilder setVideo(String url)
{
if (url == null)
{
this.videoInfo = null;
}
else
{
urlCheck(url);
this.videoInfo = new MessageEmbed.VideoInfo(url, 0, 0);
}
return this;
}
/**
* Sets the Image of the embed.
* @param url the url of the image of the embed
* @return the builder after the image has been set
*/
public EmbedBuilder setImage(String url)
{
if (url == null)
{
this.image = null;
}
else
{
httpsCheck(url);
this.image = new MessageEmbed.ImageInfo(url, null, 0, 0);
}
return this;
}
/**
* Sets the Provider of the embed.
* @param name the name of the provider of the embed
* @param url the url of the provider of the embed
* @return the builder after the provider has been set
*/
public EmbedBuilder setProvider(String name, String url)
{
if (name == null && url == null)
{
this.siteProvider = null;
}
else
{
urlCheck(url);
this.siteProvider = new MessageEmbed.Provider(name, url);
}
return this;
}
/**
* Sets the Author of the embed.
* @param name the name of the author of the embed. If this is not set, the
* author will not appear in the embed
* @param url the url of the author of the embed
* @param iconUrl the url of the icon for the author
* @return the builder after the author has been set
*/
public EmbedBuilder setAuthor(String name, String url, String iconUrl)
{
if (name == null && url == null && iconUrl == null)
{
this.author = null;
}
else
{
urlCheck(url);
this.author = new MessageEmbed.AuthorInfo(name, url, iconUrl, null);
}
return this;
}
/**
* Sets the Footer of the embed.
* @param text the text of the footer of the embed. If this is not set, the
* footer will not appear in the embed
* @param iconUrl the url of the icon for the footer
* @return the builder after the footer has been set
*/
public EmbedBuilder setFooter(String text, String iconUrl)
{
if (text == null && iconUrl == null)
{
this.footer = null;
}
else
{
if (text != null && text.length() > TEXT_MAX_LENGTH)
throw new IllegalArgumentException("Text cannot be longer than " + TEXT_MAX_LENGTH + " characters.");
httpsCheck(iconUrl);
this.footer = new MessageEmbed.Footer(text, iconUrl, null);
}
return this;
}
/**
* Adds a Field to the embed.
* @param name the name of the footer of the embed.
* @param value the contents of the field
* @param inline whether or not this field should display inline
* @return the builder after the field has been added
*/
public EmbedBuilder addField(String name, String value, boolean inline)
{
if (name == null && value == null)
return this;
else if (name == null || value == null)
throw new IllegalArgumentException("Both Name and Value must be set!");
else if (name.length() > TITLE_MAX_LENGTH)
throw new IllegalArgumentException("Name cannot be longer than " + TITLE_MAX_LENGTH + " characters.");
else if (value.length() > VALUE_MAX_LENGTH)
throw new IllegalArgumentException("Value cannot be longer than " + VALUE_MAX_LENGTH + " characters.");
this.fields.add(new MessageEmbed.Field(name, value, inline));
return this;
}
private void urlCheck(String url)
{
if (url == null)
return;
else if (url.length() > URL_MAX_LENGTH)
throw new IllegalArgumentException("URL cannot be longer than " + URL_MAX_LENGTH + " characters.");
else if (!URL_PATTERN.matcher(url).matches())
throw new IllegalArgumentException("URL must be a valid http or https url.");
}
private void httpsCheck(String url)
{
if (url == null)
return;
else if (url.length() > URL_MAX_LENGTH)
throw new IllegalArgumentException("URL cannot be longer than " + URL_MAX_LENGTH + " characters.");
else if (!HTTPS_URL_PATTERN.matcher(url).matches())
throw new IllegalArgumentException("URL must be a valid https url.");
}
}