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
2 changes: 1 addition & 1 deletion src/main/java/net/dv8tion/jda/api/MessageBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ 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);
final MessageActionImpl action = new MessageActionImpl(channel.getJDA(), null, channel, builder);
return action.tts(isTTS).setEmbeds(embeds).nonce(nonce);
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/dv8tion/jda/api/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,8 @@ default MessageAction reply(@Nonnull MessageEmbed content)
@CheckReturnValue
default MessageAction replyEmbeds(@Nonnull MessageEmbed... embeds)
{
return getChannel().sendMessageEmbeds(embeds).reference(this);
Checks.noneNull(embeds, "MessageEmbeds");
return replyEmbeds(Arrays.asList(embeds));
}

/**
Expand Down
40 changes: 12 additions & 28 deletions src/main/java/net/dv8tion/jda/api/entities/MessageChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,10 @@ default MessageAction sendMessage(@Nonnull CharSequence text)
Checks.notEmpty(text, "Provided text for message");
Checks.check(text.length() <= 2000, "Provided text for message must be less than 2000 characters in length");

Route.CompiledRoute route = Route.Messages.SEND_MESSAGE.compile(getId());
if (text instanceof StringBuilder)
return new MessageActionImpl(getJDA(), route, this, (StringBuilder) text);
return new MessageActionImpl(getJDA(), null, this, (StringBuilder) text);
else
return new MessageActionImpl(getJDA(), route, this).append(text);
return new MessageActionImpl(getJDA(), null, this).append(text);
}

/**
Expand Down Expand Up @@ -416,8 +415,7 @@ default MessageAction sendMessage(@Nonnull MessageEmbed embed)
{
Checks.notNull(embed, "Provided embed");

Route.CompiledRoute route = Route.Messages.SEND_MESSAGE.compile(getId());
return new MessageActionImpl(getJDA(), route, this).setEmbeds(embed);
return new MessageActionImpl(getJDA(), null, this).setEmbeds(embed);
}

/**
Expand Down Expand Up @@ -459,8 +457,7 @@ default MessageAction sendMessage(@Nonnull MessageEmbed embed)
@CheckReturnValue
default MessageAction sendMessageEmbeds(@Nonnull MessageEmbed... embeds)
{
Route.CompiledRoute route = Route.Messages.SEND_MESSAGE.compile(getId());
return new MessageActionImpl(getJDA(), route, this).setEmbeds(embeds);
return new MessageActionImpl(getJDA(), null, this).setEmbeds(embeds);
}

/**
Expand Down Expand Up @@ -502,8 +499,7 @@ default MessageAction sendMessageEmbeds(@Nonnull MessageEmbed... embeds)
@CheckReturnValue
default MessageAction sendMessageEmbeds(@Nonnull Collection<? extends MessageEmbed> embeds)
{
Route.CompiledRoute route = Route.Messages.SEND_MESSAGE.compile(getId());
return new MessageActionImpl(getJDA(), route, this).setEmbeds(embeds);
return new MessageActionImpl(getJDA(), null, this).setEmbeds(embeds);
}


Expand Down Expand Up @@ -563,9 +559,7 @@ default MessageAction sendMessageEmbeds(@Nonnull Collection<? extends MessageEmb
default MessageAction sendMessage(@Nonnull Message msg)
{
Checks.notNull(msg, "Message");

Route.CompiledRoute route = Route.Messages.SEND_MESSAGE.compile(getId());
return new MessageActionImpl(getJDA(), route, this).apply(msg);
return new MessageActionImpl(getJDA(), null, this).apply(msg);
}

/**
Expand Down Expand Up @@ -623,7 +617,6 @@ default MessageAction sendMessage(@Nonnull Message msg)
default MessageAction sendFile(@Nonnull File file, @Nonnull AttachmentOption... options)
{
Checks.notNull(file, "file");

return sendFile(file, file.getName(), options);
}

Expand Down Expand Up @@ -777,9 +770,7 @@ default MessageAction sendFile(@Nonnull InputStream data, @Nonnull String fileNa
{
Checks.notNull(data, "data InputStream");
Checks.notNull(fileName, "fileName");

Route.CompiledRoute route = Route.Messages.SEND_MESSAGE.compile(getId());
return new MessageActionImpl(getJDA(), route, this).addFile(data, fileName, options);
return new MessageActionImpl(getJDA(), null, this).addFile(data, fileName, options);
}

/**
Expand Down Expand Up @@ -2731,12 +2722,10 @@ default MessageAction editMessageById(@Nonnull String messageId, @Nonnull CharSe
Checks.isSnowflake(messageId, "Message ID");
Checks.notEmpty(newContent, "Provided message content");
Checks.check(newContent.length() <= 2000, "Provided newContent length must be 2000 or less characters.");

Route.CompiledRoute route = Route.Messages.EDIT_MESSAGE.compile(getId(), messageId);
if (newContent instanceof StringBuilder)
return new MessageActionImpl(getJDA(), route, this, (StringBuilder) newContent);
return new MessageActionImpl(getJDA(), messageId, this, (StringBuilder) newContent);
else
return new MessageActionImpl(getJDA(), route, this).append(newContent);
return new MessageActionImpl(getJDA(), messageId, this).append(newContent);
}

/**
Expand Down Expand Up @@ -2835,9 +2824,7 @@ default MessageAction editMessageById(@Nonnull String messageId, @Nonnull Messag
{
Checks.isSnowflake(messageId, "Message ID");
Checks.notNull(newContent, "message");

Route.CompiledRoute route = Route.Messages.EDIT_MESSAGE.compile(getId(), messageId);
return new MessageActionImpl(getJDA(), route, this).apply(newContent);
return new MessageActionImpl(getJDA(), messageId, this).apply(newContent);
}

/**
Expand Down Expand Up @@ -3060,9 +3047,7 @@ default MessageAction editMessageById(@Nonnull String messageId, @Nonnull Messag
{
Checks.isSnowflake(messageId, "Message ID");
Checks.notNull(newEmbed, "MessageEmbed");

Route.CompiledRoute route = Route.Messages.EDIT_MESSAGE.compile(getId(), messageId);
return new MessageActionImpl(getJDA(), route, this).setEmbeds(newEmbed);
return new MessageActionImpl(getJDA(), messageId, this).setEmbeds(newEmbed);
}

/**
Expand Down Expand Up @@ -3262,8 +3247,7 @@ default MessageAction editMessageEmbedsById(long messageId, @Nonnull MessageEmbe
default MessageAction editMessageEmbedsById(@Nonnull String messageId, @Nonnull Collection<? extends MessageEmbed> newEmbeds)
{
Checks.isSnowflake(messageId, "Message ID");
Route.CompiledRoute route = Route.Messages.EDIT_MESSAGE.compile(getId(), messageId);
return new MessageActionImpl(getJDA(), route, this).setEmbeds(newEmbeds);
return new MessageActionImpl(getJDA(), messageId, this).setEmbeds(newEmbeds);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import javax.annotation.Nonnull;
import java.io.File;
import java.io.InputStream;
import java.time.temporal.ChronoUnit;
import java.util.Collection;

/**
Expand Down Expand Up @@ -64,6 +65,16 @@ public interface InteractionHook extends WebhookClient<Message>
@Nonnull
Interaction getInteraction();

default long getExpirationTimestamp()
{
return getInteraction().getTimeCreated().plus(15, ChronoUnit.MINUTES).toEpochSecond() * 1000;
}

default boolean isExpired()
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
{
return System.currentTimeMillis() > getExpirationTimestamp();
}

/**
* Whether messages sent from this interaction hook should be ephemeral by default.
* <br>This does not affect message updates, including deferred replies sent with {@link #sendMessage(String) sendMessage(...)} methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import net.dv8tion.jda.api.audit.AuditLogChange;
import net.dv8tion.jda.api.audit.AuditLogEntry;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.Guild.VerificationLevel;
import net.dv8tion.jda.api.entities.Guild.NotificationLevel;
import net.dv8tion.jda.api.entities.Guild.ExplicitContentLevel;
import net.dv8tion.jda.api.entities.Guild.NotificationLevel;
import net.dv8tion.jda.api.entities.Guild.Timeout;
import net.dv8tion.jda.api.entities.Guild.VerificationLevel;
import net.dv8tion.jda.api.entities.MessageEmbed.*;
import net.dv8tion.jda.api.entities.templates.Template;
import net.dv8tion.jda.api.entities.templates.TemplateChannel;
Expand Down Expand Up @@ -1098,7 +1098,7 @@ public Message createMessage(DataObject jsonObject, boolean modifyCache)

return createMessage(jsonObject, chan, modifyCache);
}
public Message createMessage(DataObject jsonObject, @Nullable MessageChannel channel, boolean modifyCache)
public ReceivedMessage createMessage(DataObject jsonObject, @Nullable MessageChannel channel, boolean modifyCache)
{
long channelId = jsonObject.getUnsignedLong("channel_id");
if (channel != null && channelId != channel.getIdLong())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.exceptions.MissingAccessException;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
Expand All @@ -34,6 +35,7 @@
import net.dv8tion.jda.internal.requests.CompletedRestAction;
import net.dv8tion.jda.internal.requests.Route;
import net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl;
import net.dv8tion.jda.internal.requests.restaction.MessageActionImpl;
import net.dv8tion.jda.internal.utils.Checks;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -72,6 +74,8 @@ public class ReceivedMessage extends AbstractMessage
protected final TLongSet mentionedRoles;
protected final int flags;

protected InteractionHook interactionHook = null; // late-init

// LAZY EVALUATED
protected String altContent = null;
protected String strippedContent = null;
Expand Down Expand Up @@ -112,6 +116,12 @@ public ReceivedMessage(
this.flags = flags;
}

public ReceivedMessage withHook(InteractionHook hook)
{
this.interactionHook = hook;
return this;
}

@Nonnull
@Override
public JDA getJDA()
Expand Down Expand Up @@ -798,31 +808,31 @@ public MessageActivity getActivity()
public MessageAction editMessage(@Nonnull CharSequence newContent)
{
checkUser();
return channel.editMessageById(getId(), newContent);
return ((MessageActionImpl) channel.editMessageById(getId(), newContent)).withHook(interactionHook);
}

@Nonnull
@Override
public MessageAction editMessageEmbeds(@Nonnull Collection<? extends MessageEmbed> embeds)
{
checkUser();
return channel.editMessageEmbedsById(getId(), embeds);
return ((MessageActionImpl) channel.editMessageEmbedsById(getId(), embeds)).withHook(interactionHook);
}

@Nonnull
@Override
public MessageAction editMessageFormat(@Nonnull String format, @Nonnull Object... args)
{
checkUser();
return channel.editMessageFormatById(getId(), format, args);
return ((MessageActionImpl) channel.editMessageFormatById(getId(), format, args)).withHook(interactionHook);
}

@Nonnull
@Override
public MessageAction editMessage(@Nonnull Message newContent)
{
checkUser();
return channel.editMessageById(getIdLong(), newContent);
return ((MessageActionImpl) channel.editMessageById(getId(), newContent)).withHook(interactionHook);
}

private void checkUser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public WebhookMessageActionImpl<Message> sendRequest()
{
Route.CompiledRoute route = Route.Interactions.CREATE_FOLLOWUP.compile(getJDA().getSelfUser().getApplicationId(), interaction.getToken());
route = route.withQueryParams("wait", "true");
Function<DataObject, Message> transform = (json) -> ((JDAImpl) api).getEntityBuilder().createMessage(json, getInteraction().getMessageChannel(), false);
Function<DataObject, Message> transform = (json) -> ((JDAImpl) api).getEntityBuilder().createMessage(json, getInteraction().getMessageChannel(), false).withHook(this);
return onReady(new WebhookMessageActionImpl<>(getJDA(), interaction.getMessageChannel(), route, transform)).setEphemeral(ephemeral);
}

Expand All @@ -164,7 +164,7 @@ public WebhookMessageUpdateActionImpl<Message> editRequest(String messageId)
Checks.isSnowflake(messageId);
Route.CompiledRoute route = Route.Interactions.EDIT_FOLLOWUP.compile(getJDA().getSelfUser().getApplicationId(), interaction.getToken(), messageId);
route = route.withQueryParams("wait", "true");
Function<DataObject, Message> transform = (json) -> ((JDAImpl) api).getEntityBuilder().createMessage(json, getInteraction().getMessageChannel(), false);
Function<DataObject, Message> transform = (json) -> ((JDAImpl) api).getEntityBuilder().createMessage(json, getInteraction().getMessageChannel(), false).withHook(this);
return onReady(new WebhookMessageUpdateActionImpl<>(getJDA(), route, transform));
}

Expand Down
Loading