Class EmbedBuilder
MessageEmbeds
.
A visual breakdown of an Embed and how it relates to this class is available at Embed Overview.
- Since:
- 3.0
- Author:
- John A. Grosh
-
Field Summary
-
Constructor Summary
ConstructorDescriptionConstructs a new EmbedBuilder instance, which can be used to createMessageEmbeds
.EmbedBuilder
(EmbedBuilder builder) Creates an EmbedBuilder using fields from an existing builderEmbedBuilder
(MessageEmbed embed) Creates an EmbedBuilder using fields in an existing embed. -
Method Summary
Modifier and TypeMethodDescriptionaddBlankField
(boolean inline) Adds a blank (empty) Field to the embed.Adds a Field to the embed.addField
(MessageEmbed.Field field) Copies the provided Field into a new Field for this builder.appendDescription
(CharSequence description) Appends to the description of the embed.build()
Returns aMessageEmbed
that has been checked as being valid for sending.clear()
Resets this builder to default state.Clears all fields from the embed, such as those created with theEmbedBuilder(MessageEmbed)
constructor or via theaddField
methods.void
copyFrom
(EmbedBuilder builder) Copies the data from the given builder into this builder.void
copyFrom
(MessageEmbed embed) Copies the data from the given embed into this builder.static EmbedBuilder
fromData
(DataObject data) Creates an instance of this builder from the providedDataObject
.TheStringBuilder
used to build the description for the embed.Modifiable list ofMessageEmbed
Fields that the builder will use forbuild()
.boolean
isEmpty()
Checks if the given embed is empty.boolean
Checks whether the constructedMessageEmbed
is within the limits for a bot account.int
length()
The overall length of the current EmbedBuilder in displayed characters.Sets the Author of the embed.Sets the Author of the embed.Sets the Author of the embed.setColor
(int color) Sets the raw RGB color value for the embed.Sets the Color of the embed.final EmbedBuilder
setDescription
(CharSequence description) Sets the Description of the embed.Sets the Footer of the embed without icon.Sets the Footer of the embed.Sets the Image of the embed.setThumbnail
(String url) Sets the Thumbnail of the embed.setTimestamp
(TemporalAccessor temporal) Sets the Timestamp of the embed.Sets the Title of the embed.Sets the Title of the embed.Sets the URL of the embed.
-
Field Details
-
ZERO_WIDTH_SPACE
- See Also:
-
URL_PATTERN
-
-
Constructor Details
-
EmbedBuilder
public EmbedBuilder()Constructs a new EmbedBuilder instance, which can be used to createMessageEmbeds
. These can then be sent to a channel usingMessageChannel.sendMessageEmbeds(MessageEmbed, MessageEmbed...)
.
Every part of an embed can be removed or cleared by providingnull
to the setter method. -
EmbedBuilder
Creates an EmbedBuilder using fields from an existing builder- Parameters:
builder
- the existing builder
-
EmbedBuilder
Creates an EmbedBuilder using fields in an existing embed.- Parameters:
embed
- the existing embed
-
-
Method Details
-
fromData
Creates an instance of this builder from the providedDataObject
.This is the inverse of
MessageEmbed.toData()
.- Parameters:
data
- The serialized embed object- Returns:
- The new builder instance
- Throws:
IllegalArgumentException
- If the provided data isnull
or invalidParsingException
- If the provided data is malformed
-
build
Returns aMessageEmbed
that has been checked as being valid for sending.- Returns:
- the built, sendable
MessageEmbed
- Throws:
IllegalStateException
-- If the embed is empty. Can be checked with
isEmpty()
. - If the character limit for
description
, defined byMessageEmbed.DESCRIPTION_MAX_LENGTH
as 4096, is exceeded. - If the embed's total length, defined by
MessageEmbed.EMBED_MAX_LENGTH_BOT
as 6000, is exceeded. - If the embed's number of embed fields, defined by
MessageEmbed.MAX_FIELD_AMOUNT
as 25, is exceeded.
- If the embed is empty. Can be checked with
-
clear
Resets this builder to default state.
All parts will be either empty or null after this method has returned.- Returns:
- The current EmbedBuilder with default values
-
copyFrom
Copies the data from the given builder into this builder.
All the parts of the given builder will be applied to this one.- Parameters:
builder
- the existing builder
-
copyFrom
Copies the data from the given embed into this builder.
All the parts of the given embed will be applied to this builder.- Parameters:
embed
- the existing embed
-
isEmpty
public boolean isEmpty()Checks if the given embed is empty. Empty embeds will throw an exception if built.- Returns:
- true if the embed is empty and cannot be built
-
length
public int length()The overall length of the current EmbedBuilder in displayed characters.
Represents theMessageEmbed.getLength()
value.- Returns:
- length of the current builder state
-
isValidLength
public boolean isValidLength()Checks whether the constructedMessageEmbed
is within the limits for a bot account. -
setTitle
- Parameters:
title
- the title of the embed- Returns:
- the builder after the title has been set
- Throws:
IllegalArgumentException
-- If the provided
title
is an empty String. - If the character limit for
title
, defined byMessageEmbed.TITLE_MAX_LENGTH
as 256, is exceeded.
- If the provided
-
setTitle
Sets the Title of the embed.
You can providenull
as url if no url should be used.
If you want to set a URL without a title, usesetUrl(String)
instead.- Parameters:
title
- the title of the embedurl
- Makes the title into a hyperlink pointed at this url.- Returns:
- the builder after the title has been set
- Throws:
IllegalArgumentException
-- If the provided
title
is an empty String. - If the character limit for
title
, defined byMessageEmbed.TITLE_MAX_LENGTH
as 256, is exceeded. - If the character limit for
url
, defined byMessageEmbed.URL_MAX_LENGTH
as 2000, is exceeded. - If the provided
url
is not a properly formatted http or https url.
- If the provided
-
setUrl
Sets the URL of the embed.
The Discord client mostly only uses this property in combination with thetitle
for a clickable Hyperlink.If multiple embeds in a message use the same URL, the Discord client will merge them into a single embed and aggregate images into a gallery view.
- Returns:
- the builder after the URL has been set
- Throws:
IllegalArgumentException
-- If the character limit for
url
, defined byMessageEmbed.URL_MAX_LENGTH
as 2000, is exceeded. - If the provided
url
is not a properly formatted http or https url.
- If the character limit for
- See Also:
-
getDescriptionBuilder
TheStringBuilder
used to build the description for the embed.
Note: To reset the description usesetDescription(null)
- Returns:
- StringBuilder with current description context
-
setDescription
Sets the Description of the embed. This is where the main chunk of text for an embed is typically placed.- Parameters:
description
- the description of the embed,null
to reset- Returns:
- the builder after the description has been set
- Throws:
IllegalArgumentException
- Ifdescription
is longer than 4096 characters, as defined byMessageEmbed.DESCRIPTION_MAX_LENGTH
-
appendDescription
Appends to the description of the embed. This is where the main chunk of text for an embed is typically placed.- Parameters:
description
- the string to append to the description of the embed- Returns:
- the builder after the description has been set
- Throws:
IllegalArgumentException
-- If the provided
description
String is null. - If the character limit for
description
, defined byMessageEmbed.DESCRIPTION_MAX_LENGTH
as 4096, is exceeded.
- If the provided
-
setTimestamp
Sets the Timestamp of the embed.Hint: You can get the current time using
Instant.now()
or convert time from a millisecond representation by usingInstant.ofEpochMilli(long)
;- Parameters:
temporal
- the temporal accessor of the timestamp- Returns:
- the builder after the timestamp has been set
-
setColor
Sets the Color of the embed.- Parameters:
color
- TheColor
of the embed ornull
to use no color- Returns:
- the builder after the color has been set
- See Also:
-
setColor
Sets the raw RGB color value for the embed.- Parameters:
color
- The raw rgb value, orRole.DEFAULT_COLOR_RAW
to use no color- Returns:
- the builder after the color has been set
- See Also:
-
setThumbnail
Sets the Thumbnail of the embed.Uploading images with Embeds
When uploading an image (usingMessageChannel.sendFiles(...)
) you can reference said image using the specified filename as URIattachment://filename.ext
.Example
MessageChannel channel; // = reference of a MessageChannel EmbedBuilder embed = new EmbedBuilder(); InputStream file = new URL("https://http.cat/500").openStream(); embed.setThumbnail("attachment://cat.png") // we specify this in sendFile as "cat.png" .setDescription("This is a cute cat :3"); channel.sendFiles(FileUpload.fromData(file, "cat.png")).setEmbeds(embed.build()).queue();
- Parameters:
url
- the url of the thumbnail of the embed- Returns:
- the builder after the thumbnail has been set
- Throws:
IllegalArgumentException
-- If the character limit for
url
, defined byMessageEmbed.URL_MAX_LENGTH
as 2000, is exceeded. - If the provided
url
is not a properly formatted http or https url.
- If the character limit for
-
setImage
Sets the Image of the embed.Uploading images with Embeds
When uploading an image (usingMessageChannel.sendFiles(...)
) you can reference said image using the specified filename as URIattachment://filename.ext
.Example
MessageChannel channel; // = reference of a MessageChannel EmbedBuilder embed = new EmbedBuilder(); InputStream file = new URL("https://http.cat/500").openStream(); embed.setImage("attachment://cat.png") // we specify this in sendFile as "cat.png" .setDescription("This is a cute cat :3"); channel.sendFiles(FileUpload.fromData(file, "cat.png")).setEmbeds(embed.build()).queue();
- Parameters:
url
- the url of the image of the embed- Returns:
- the builder after the image has been set
- Throws:
IllegalArgumentException
-- If the character limit for
url
, defined byMessageEmbed.URL_MAX_LENGTH
as 2000, is exceeded. - If the provided
url
is not a properly formatted http or https url.
- If the character limit for
- See Also:
-
setAuthor
Sets the Author of the embed. The author appears in the top left of the embed and can have a small image beside it along with the author's name being made clickable by way of providing a url. This convenience method just sets the name.- Parameters:
name
- the name of the author of the embed. If this is not set, the author will not appear in the embed- Returns:
- the builder after the author has been set
- Throws:
IllegalArgumentException
- Ifname
is longer than 256 characters, as defined byMessageEmbed.AUTHOR_MAX_LENGTH
-
setAuthor
Sets the Author of the embed. The author appears in the top left of the embed and can have a small image beside it along with the author's name being made clickable by way of providing a url. This convenience method just sets the name and the url.- Parameters:
name
- the name of the author of the embed. If this is not set, the author will not appear in the embedurl
- the url of the author of the embed- Returns:
- the builder after the author has been set
- Throws:
IllegalArgumentException
-- If the character limit for
name
, defined byMessageEmbed.AUTHOR_MAX_LENGTH
as 256, is exceeded. - If the character limit for
url
, defined byMessageEmbed.URL_MAX_LENGTH
as 2000, is exceeded. - If the provided
url
is not a properly formatted http or https url.
- If the character limit for
-
setAuthor
@Nonnull public EmbedBuilder setAuthor(@Nullable String name, @Nullable String url, @Nullable String iconUrl) Sets the Author of the embed. The author appears in the top left of the embed and can have a small image beside it along with the author's name being made clickable by way of providing a url.Uploading images with Embeds
When uploading an image (usingMessageChannel.sendFiles(...)
) you can reference said image using the specified filename as URIattachment://filename.ext
.Example
MessageChannel channel; // = reference of a MessageChannel EmbedBuilder embed = new EmbedBuilder(); InputStream file = new URL("https://http.cat/500").openStream(); embed.setAuthor("Minn", null, "attachment://cat.png") // we specify this in sendFile as "cat.png" .setDescription("This is a cute cat :3"); channel.sendFiles(FileUpload.fromData(file, "cat.png")).setEmbeds(embed.build()).queue();
- Parameters:
name
- the name of the author of the embed. If this is not set, the author will not appear in the embedurl
- the url of the author of the embediconUrl
- the url of the icon for the author- Returns:
- the builder after the author has been set
- Throws:
IllegalArgumentException
-- If the character limit for
name
, defined byMessageEmbed.AUTHOR_MAX_LENGTH
as 256, is exceeded. - If the character limit for
url
, defined byMessageEmbed.URL_MAX_LENGTH
as 2000, is exceeded. - If the provided
url
is not a properly formatted http or https url. - If the character limit for
iconUrl
, defined byMessageEmbed.URL_MAX_LENGTH
as 2000, is exceeded. - If the provided
iconUrl
is not a properly formatted http or https url.
- If the character limit for
-
addField
Copies the provided Field into a new Field for this builder.
For additional documentation, seeaddField(String, String, boolean)
- Parameters:
field
- the field object to add- Returns:
- the builder after the field has been added
-
addField
Adds a Field to the embed.Note: If a blank string is provided to either
name
orvalue
, the blank string is replaced withZERO_WIDTH_SPACE
.- Parameters:
name
- the name of the Field, displayed in bold above thevalue
.value
- the contents of the field.inline
- whether or not this field should display inline.- Returns:
- the builder after the field has been added
- Throws:
IllegalArgumentException
-- If
null
is provided - If the character limit for
name
, defined byMessageEmbed.TITLE_MAX_LENGTH
as 256, is exceeded. - If the character limit for
value
, defined byMessageEmbed.VALUE_MAX_LENGTH
as 1024, is exceeded.
- If
-
addBlankField
Adds a blank (empty) Field to the embed.- Parameters:
inline
- whether or not this field should display inline- Returns:
- the builder after the field has been added
-
clearFields
Clears all fields from the embed, such as those created with theEmbedBuilder(MessageEmbed)
constructor or via theaddField
methods.- Returns:
- the builder after the field has been added
-
getFields
Modifiable list ofMessageEmbed
Fields that the builder will use forbuild()
.
You can add/remove Fields and restructure thisList
and it will then be applied in the built MessageEmbed. These fields will be available again throughMessageEmbed.getFields()
.- Returns:
- Mutable List of
Fields
-