Skip to content

Commit

Permalink
Add check for max number of fields in EmbedBuilder (#2592)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre601 committed Dec 22, 2023
1 parent abc4c36 commit c0c12e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/net/dv8tion/jda/api/EmbedBuilder.java
Expand Up @@ -155,7 +155,15 @@ public static EmbedBuilder fromData(@Nonnull DataObject data)
* that has been checked as being valid for sending.
*
* @throws java.lang.IllegalStateException
* If the embed is empty. Can be checked with {@link #isEmpty()}.
* <ul>
* <li>If the embed is empty. Can be checked with {@link #isEmpty()}.</li>
* <li>If the character limit for {@code description}, defined by {@link net.dv8tion.jda.api.entities.MessageEmbed#DESCRIPTION_MAX_LENGTH} as {@value net.dv8tion.jda.api.entities.MessageEmbed#DESCRIPTION_MAX_LENGTH},
* is exceeded.</li>
* <li>If the embed's total length, defined by {@link net.dv8tion.jda.api.entities.MessageEmbed#EMBED_MAX_LENGTH_BOT} as {@value net.dv8tion.jda.api.entities.MessageEmbed#EMBED_MAX_LENGTH_BOT},
* is exceeded.</li>
* <li>If the embed's number of embed fields, defined by {@link net.dv8tion.jda.api.entities.MessageEmbed#MAX_FIELD_AMOUNT} as {@value net.dv8tion.jda.api.entities.MessageEmbed#MAX_FIELD_AMOUNT},
* is exceeded.</li>
* </ul>
*
* @return the built, sendable {@link net.dv8tion.jda.api.entities.MessageEmbed}
*/
Expand All @@ -167,7 +175,9 @@ public MessageEmbed build()
if (description.length() > MessageEmbed.DESCRIPTION_MAX_LENGTH)
throw new IllegalStateException(Helpers.format("Description is longer than %d! Please limit your input!", MessageEmbed.DESCRIPTION_MAX_LENGTH));
if (length() > MessageEmbed.EMBED_MAX_LENGTH_BOT)
throw new IllegalStateException("Cannot build an embed with more than " + MessageEmbed.EMBED_MAX_LENGTH_BOT + " characters!");
throw new IllegalStateException(Helpers.format("Cannot build an embed with more than %d characters!", MessageEmbed.EMBED_MAX_LENGTH_BOT));
if (fields.size() > MessageEmbed.MAX_FIELD_AMOUNT)
throw new IllegalStateException(Helpers.format("Cannot build an embed with more than %d embed fields set!", MessageEmbed.MAX_FIELD_AMOUNT));
final String description = this.description.length() < 1 ? null : this.description.toString();

return EntityBuilder.createMessageEmbed(url, title, description, EmbedType.RICH, timestamp,
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/MessageEmbed.java
Expand Up @@ -115,6 +115,13 @@ public class MessageEmbed implements SerializableData
@ForRemoval
public static final int EMBED_MAX_LENGTH_CLIENT = 2000;

/**
* The maximum amount of total embed fields the embed can hold
*
* @see net.dv8tion.jda.api.EmbedBuilder#addField(String, String, boolean)
*/
public static final int MAX_FIELD_AMOUNT = 25;

protected final Object mutex = new Object();

protected final String url;
Expand Down

0 comments on commit c0c12e5

Please sign in to comment.