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

Allow ephemeral deferred interaction responses #838

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -47,6 +47,18 @@ public interface InteractionBase extends DiscordEntity {
*/
CompletableFuture<InteractionOriginalResponseUpdater> respondLater();

/**
* Sends an acknowledgement of the interaction to Discord and displays an (ephemeral) loading state to the user,
* indicating that you'll respond with a delay.
* Please note that you can only actually update this loading state message within 15 minutes after receiving the
* interaction.
*
* @param ephemeral wether or not the response should be ephemeral
* @return A CompletableFuture that completes as soon as the acknowledgement has been sent; it yields an updater
* that should be used to update the message later on.
*/
CompletableFuture<InteractionOriginalResponseUpdater> respondLater(boolean ephemeral);
felldo marked this conversation as resolved.
Show resolved Hide resolved

/**
* Create a message builder to send follow up messages for this interaction.
* You can send, edit and delete follow up messages up to 15 minutes after you received the interaction.
Expand Down
Expand Up @@ -6,6 +6,7 @@
import org.javacord.api.entity.channel.Channel;
import org.javacord.api.entity.channel.ServerChannel;
import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.entity.message.MessageFlag;
import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User;
import org.javacord.api.interaction.Interaction;
Expand Down Expand Up @@ -33,6 +34,10 @@ public abstract class InteractionImpl implements Interaction {
private static final String RESPOND_LATER_BODY =
"{\"type\": " + InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE.getId() + "}";

private static final String RESPOND_LATER_EPHEMERAL_BODY =
"{\"type\": " + InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE.getId()
+ ", \"data\": {\"flags\": " + MessageFlag.EPHEMERAL.getId() + "}}";

private final DiscordApiImpl api;
private final TextChannel channel;

Expand Down Expand Up @@ -98,10 +103,15 @@ public InteractionImmediateResponseBuilder createImmediateResponder() {

@Override
public CompletableFuture<InteractionOriginalResponseUpdater> respondLater() {
return respondLater(false);
}

@Override
public CompletableFuture<InteractionOriginalResponseUpdater> respondLater(boolean ephemeral) {
return new RestRequest<InteractionOriginalResponseUpdater>(this.api,
RestMethod.POST, RestEndpoint.INTERACTION_RESPONSE)
.setUrlParameters(getIdAsString(), token)
.setBody(RESPOND_LATER_BODY)
.setBody(ephemeral ? RESPOND_LATER_EPHEMERAL_BODY : RESPOND_LATER_BODY)
.execute(result -> new InteractionOriginalResponseUpdaterImpl(this));
}

Expand Down