Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multi-embeds in MessageAction #1652

Merged
merged 12 commits into from
Jun 15, 2021
10 changes: 5 additions & 5 deletions src/main/java/net/dv8tion/jda/api/EmbedBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class EmbedBuilder

/**
* Constructs a new EmbedBuilder instance, which can be used to create {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbeds}.
* These can then be sent to a channel using {@link net.dv8tion.jda.api.entities.MessageChannel#sendMessage(MessageEmbed)}.
* These can then be sent to a channel using {@link net.dv8tion.jda.api.entities.MessageChannel#sendMessageEmbeds(MessageEmbed...)}.
* <br>Every part of an embed can be removed or cleared by providing {@code null} to the setter method.
*/
public EmbedBuilder() { }
Expand Down Expand Up @@ -480,7 +480,7 @@ public EmbedBuilder setColor(int color)
* 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.sendFile(file, "cat.png").embed(embed.build()).queue();
* channel.sendFile(file, "cat.png").setEmbeds(embed.build()).queue();
* </code></pre>
*
* @param url
Expand Down Expand Up @@ -526,7 +526,7 @@ public EmbedBuilder setThumbnail(@Nullable String url)
* 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.sendFile(file, "cat.png").embed(embed.build()).queue();
* channel.sendFile(file, "cat.png").setEmbeds(embed.build()).queue();
* </code></pre>
*
* @param url
Expand Down Expand Up @@ -623,7 +623,7 @@ public EmbedBuilder setAuthor(@Nullable String name, @Nullable String url)
* 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.sendFile(file, "cat.png").embed(embed.build()).queue();
* channel.sendFile(file, "cat.png").setEmbeds(embed.build()).queue();
* </code></pre>
*
* @param name
Expand Down Expand Up @@ -699,7 +699,7 @@ public EmbedBuilder setFooter(@Nullable String text)
* InputStream file = new URL("https://http.cat/500").openStream();
* embed.setFooter("Cool footer!", "attachment://cat.png") // we specify this in sendFile as "cat.png"
* .setDescription("This is a cute cat :3");
* channel.sendFile(file, "cat.png").embed(embed.build()).queue();
* channel.sendFile(file, "cat.png").setEmbeds(embed.build()).queue();
* </code></pre>
*
* @param text
Expand Down
88 changes: 73 additions & 15 deletions src/main/java/net/dv8tion/jda/api/MessageBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public class MessageBuilder implements Appendable
{
protected final StringBuilder builder = new StringBuilder();

protected final List<MessageEmbed> embeds = new ArrayList<>();
protected boolean isTTS = false;
protected String nonce;
protected MessageEmbed embed;
protected List<ComponentLayout> components = new ArrayList<>();
protected EnumSet<Message.MentionType> allowedMentions = null;
protected Set<String> mentionedUsers = new HashSet<>();
Expand All @@ -72,8 +72,8 @@ public MessageBuilder(@Nullable Message message)
isTTS = message.isTTS();
builder.append(message.getContentRaw());
List<MessageEmbed> embeds = message.getEmbeds();
if (embeds != null && !embeds.isEmpty() && embeds.get(0).getType() == EmbedType.RICH)
embed = embeds.get(0);
if (embeds != null)
embeds.stream().filter(it -> it.getType() == EmbedType.RICH).forEach(this.embeds::add);
components.addAll(message.getActionRows());
if (message instanceof DataMessage)
{
Expand All @@ -93,7 +93,7 @@ public MessageBuilder(@Nullable MessageBuilder builder)
this.isTTS = builder.isTTS;
this.builder.append(builder.builder);
this.nonce = builder.nonce;
this.embed = builder.embed;
this.embeds.addAll(builder.embeds);
this.components.addAll(builder.components);
if (builder.allowedMentions != null)
this.allowedMentions = Helpers.copyEnumSet(Message.MentionType.class, builder.allowedMentions);
Expand All @@ -105,12 +105,12 @@ public MessageBuilder(@Nullable MessageBuilder builder)
public MessageBuilder(@Nullable EmbedBuilder builder)
{
if (builder != null)
this.embed = builder.build();
this.embeds.add(builder.build());
}

public MessageBuilder(@Nullable MessageEmbed embed)
{
this.embed = embed;
this.embeds.add(embed);
}

/**
Expand All @@ -129,7 +129,7 @@ public MessageBuilder setTTS(boolean tts)
this.isTTS = tts;
return this;
}

/**
* Adds a {@link net.dv8tion.jda.api.entities.MessageEmbed} to the Message. Embeds can be built using
* the {@link net.dv8tion.jda.api.EmbedBuilder} and offer specialized formatting.
Expand All @@ -138,11 +138,69 @@ public MessageBuilder setTTS(boolean tts)
* the embed to add, or null to remove
*
* @return The MessageBuilder instance. Useful for chaining.
*
* @deprecated Use {@link #setEmbeds(MessageEmbed...)} instead
*/
@Nonnull
@Deprecated
@ForRemoval(deadline = "5.0.0")
@ReplaceWith("setEmbeds(embed)")
@DeprecatedSince("4.4.0")
public MessageBuilder setEmbed(@Nullable MessageEmbed embed)
{
this.embed = embed;
return setEmbeds(embed);
}

/**
* Adds up to 10 {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbeds} to the Message. Embeds can be built using
* the {@link net.dv8tion.jda.api.EmbedBuilder} and offer specialized formatting.
*
* @param embeds
* the embeds to add, or null to remove
*
* @throws java.lang.IllegalArgumentException
* If any of the provided MessageEmbeds is not sendable according to {@link net.dv8tion.jda.api.entities.MessageEmbed#isSendable() MessageEmbed.isSendable()}!
* The sum of all {@link MessageEmbed#getLength()} must not be greater than {@link MessageEmbed#EMBED_MAX_LENGTH_BOT}!
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder setEmbeds(@Nullable MessageEmbed... embeds)
{
Checks.noneNull(embeds, "MessageEmbeds");
return setEmbeds(Arrays.asList(embeds));
}

/**
* Adds up to 10 {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbeds} to the Message. Embeds can be built using
* the {@link net.dv8tion.jda.api.EmbedBuilder} and offer specialized formatting.
*
* @param embeds
* the embeds to add, or null to remove
*
* @throws java.lang.IllegalArgumentException
* If any of the provided MessageEmbeds is not sendable according to {@link net.dv8tion.jda.api.entities.MessageEmbed#isSendable() MessageEmbed.isSendable()}!
* The sum of all {@link MessageEmbed#getLength()} must not be greater than {@link MessageEmbed#EMBED_MAX_LENGTH_BOT}!
*
* @return The MessageBuilder instance. Useful for chaining.
*/
@Nonnull
public MessageBuilder setEmbeds(@Nullable Collection<? extends MessageEmbed> embeds)
{

this.embeds.clear();
if (embeds != null)
{
Checks.noneNull(embeds, "MessageEmbeds");
embeds.forEach(embed ->
Checks.check(embed.isSendable(),
"Provided Message contains an empty embed or an embed with a length greater than %d characters, which is the max for bot accounts!",
MessageEmbed.EMBED_MAX_LENGTH_BOT)
);
Checks.check(embeds.size() <= 10, "Cannot have more than 10 embeds in a message!");
Checks.check(embeds.stream().mapToInt(MessageEmbed::getLength).sum() <= MessageEmbed.EMBED_MAX_LENGTH_BOT, "The sum of all MessageEmbeds may not exceed %d!", MessageEmbed.EMBED_MAX_LENGTH_BOT);
this.embeds.addAll(embeds);
}
return this;
}

Expand Down Expand Up @@ -460,7 +518,7 @@ public int length()
* @return whether the message contains content
*/
public boolean isEmpty() {
return builder.length() == 0 && embed == null;
return builder.length() == 0 && embeds.isEmpty();
}

/**
Expand Down Expand Up @@ -1023,7 +1081,7 @@ public StringBuilder getStringBuilder()
@Nonnull
public MessageBuilder clear() {
this.builder.setLength(0);
this.embed = null;
this.embeds.clear();
this.isTTS = false;
return this;
}
Expand Down Expand Up @@ -1211,8 +1269,8 @@ public MessageAction sendTo(@Nonnull MessageChannel channel)
throw new UnsupportedOperationException("Cannot send a private message between bots.");
}
final Route.CompiledRoute route = Route.Messages.SEND_MESSAGE.compile(channel.getId());
final MessageActionImpl action = new MessageActionImpl(channel.getJDA(), route, channel, builder);
return action.tts(isTTS).embed(embed).nonce(nonce);
final MessageActionImpl action = new MessageActionImpl(channel.getJDA(), null, channel, builder);
return action.tts(isTTS).setEmbeds(embeds).nonce(nonce);
}

/**
Expand Down Expand Up @@ -1241,7 +1299,7 @@ public Message build()
throw new IllegalStateException("Cannot build a Message with more than 2000 characters. Please limit your input.");

String[] ids = new String[0];
return new DataMessage(isTTS, message, nonce, embed,
return new DataMessage(isTTS, message, nonce, embeds,
allowedMentions, mentionedUsers.toArray(ids), mentionedRoles.toArray(ids), components.toArray(new ComponentLayout[0]));
}

Expand Down Expand Up @@ -1302,9 +1360,9 @@ public Queue<Message> buildAll(@Nullable SplitPolicy... policy)
messages.add(build(currentBeginIndex, builder.length() - 1));
}

if (this.embed != null)
if (this.embeds != null)
{
((DataMessage) messages.get(messages.size() - 1)).setEmbed(embed);
((DataMessage) messages.get(messages.size() - 1)).setEmbeds(embeds);
}

return messages;
Expand Down
Loading