Embed API¶
EmbedDataSource¶
In JDA, you define Embeds by using the EmbedBuilder
,
which eventually gets built into a MessageEmbed
.
MessageEmbeds
can be serialized into (and deserialized from) JSON
.
Tip
For details on the JSON
format of Embeds, see the Discord Documentation
or use one the various Online Embed Builders.
JDA-Commands takes advantage of this and provides the EmbedDataSource
interface to load Embeds from a JSON
object. It supports the following sources by default:
- a raw
JSON
String - a
*.json
file either located externally or inside theresources
folder - an
InputStream
- JDAs
DataObject
You can also provide your own sources by implementing the EmbedDataSource
interface.
The JSON
object can contain an arbitrary amount of Embeds as child objects. The name of these child objects must be unique
and will be later used to load the respective Embed.
Example
When configuring JDA-Commands, you can then use the EmbedConfig
to add one or multiple EmbedDataSources
to later load Embeds from at various places.
Example
Loading Embeds¶
You can either use the JDACommands
or the ReplyableEvent
class to load embeds into an object.
Embeds don't get loaded directly into a JDA MessageEmbed
or EmbedBuilder
object. Instead, JDA-Commands provides
a subclass of EmbedBuilder
, simply called Embed
.
It provides some additional utility, e.g. when dealing with fields, and is also required for the localization and
placeholder feature.
Example
Tip
The #embed(String)
method will throw an IllegalArgumentException
if no Embed with the given name was found. Use the
#findEmbed(String)
method, which
returns an Optional, if you cannot ensure that the Embed exists.
See the Reply Section of this wiki to learn more about on how to use Embeds for your interaction replies.
Localization¶
Just like any other feature of JDA-Commands, Embeds can be localized as well. For the default Localizer implementation, which uses Fluava, this could look like this:
Example
Use Embed#locale(Locale)
to set the locale the Embed should be localized with. When replying to interactions, this is done automatically by using
GenericInteractionCreateData#getUserLocale()
to retrieve the user locale. The Embed will be localized once #build()
is called.
See the Localization Section of this wiki for details.
Placeholders¶
You can add placeholders to your Embeds by piggybacking the localization feature. JDA-Commands treats hardcoded values (where a localization key would normally be used) as the content of a localization message, thus supporting all functionality of the used localization system, such as placeholders.
You can then use the Embed#placeholders(...) method to insert the placeholder values.
You can of course also use your placeholders regularly inside the localization files.
Global Placeholders¶
A special feature of the Embed API is that you can define global placeholders for your Embeds using the EmbedConfig
.
Example
Global placeholders can be used anywhere inside your Embeds.