Skip to content

Commit

Permalink
Add support for ephemeral middlewares.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShindouMihou committed Mar 9, 2022
1 parent e6398eb commit bdb00bd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,54 @@ public interface NexusMiddlewareEvent extends NexusCommandEvent {
* A middleware-only function that tells Discord that the response will be
* taking more than three-seconds because the middleware has to process tasks
* that can take more than three-second limit.
* @return The future to determine whether the response was accepted or not.
*/
default CompletableFuture<Void> askDelayedResponse() {
return CompletableFuture.allOf(
getNexus().getResponderRepository().peek(getBaseEvent().getInteraction())
);
}

/**
* A middleware-only function that tells Discord that the response will be
* taking more than three-seconds because the middleware has to process tasks
* that can take more than three-second limit.
* @return The future to determine whether the response was accepted or not.
*/
default CompletableFuture<Void> askDelayedResponseAsEphemeral() {
return CompletableFuture.allOf(
getNexus().getResponderRepository().peekEphemeral(getBaseEvent().getInteraction())
);
}

/**
* A middleware-only function that tells Discord that the response will be
* taking more than three-seconds because the middleware has to process tasks
* that can take more than three-second limit.
*
* @param predicate The predicate to determine whether the response should be ephemeral or not.
* @return The future to determine whether the response was accepted or not.
*/
default CompletableFuture<Void> askDelayedResponseAsEphemeralIf(boolean predicate) {
if (predicate) {
return askDelayedResponseAsEphemeral();
}

return askDelayedResponse();
}

/**
* A middleware-only function that tells Discord that the response will be
* taking more than three-seconds because the middleware has to process tasks
* that can take more than three-second limit.
*
* @param predicate The predicate to determine whether the response should be ephemeral or not.
* @return The future to determine whether the response was accepted or not.
*/
default CompletableFuture<Void> askDelayedResponseAsEphemeralIf(Predicate<Void> predicate) {
return askDelayedResponseAsEphemeralIf(predicate.test(null));
}

/**
* Tells the command interceptor handler to move forward with the next
* middleware if there is any, otherwise executes the command code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ public CompletableFuture<InteractionOriginalResponseUpdater> peek(Interaction in
});
}

/**
* Gets the current {@link InteractionOriginalResponseUpdater} for the specific interaction
* if available from another middleware otherwise requests for a new {@link InteractionOriginalResponseUpdater}
* that can be used instead.
* <br><br>
* Not to be confused with {@link NexusResponderRepository#get(Interaction)} which deliberately
* destroys the interaction that is being stored after being requested. This is intended for middlewares to
* prevent Discord's Interaction has failed while processing a heavy task.
*
* @param interaction The interaction to reference.
* @return The {@link InteractionOriginalResponseUpdater} if present otherwise requests for one.
*/
public CompletableFuture<InteractionOriginalResponseUpdater> peekEphemeral(Interaction interaction) {
if (responders.containsKey(interaction.getId())) {
return CompletableFuture.completedFuture(responders.get(interaction.getId()));
}

return interaction.respondLater(true)
.thenApply(responseUpdater -> {
responders.put(interaction.getId(), responseUpdater);
return responseUpdater;
});
}

/**
* Gets the current {@link InteractionOriginalResponseUpdater} for the specific interaction if
* available from another middleware otherwise requests for a new {@link InteractionOriginalResponseUpdater}
Expand Down

0 comments on commit bdb00bd

Please sign in to comment.