Skip to content
This repository was archived by the owner on Jun 11, 2026. It is now read-only.

Commit 7a45079

Browse files
author
Kaleidox
committed
listen for deferReply() to be complete before further handling
1 parent 0389ec9 commit 7a45079

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

src/main/java/org/comroid/interaction/adapter/jda/DiscordResponseChain.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.comroid.util.JdaUtil;
1818
import org.jspecify.annotations.Nullable;
1919

20+
import java.util.concurrent.CompletableFuture;
21+
2022
@Log
2123
enum DiscordResponseChain implements ResponseChain<MessageCreateData> {
2224
@Instance INSTANCE;
@@ -27,16 +29,16 @@ public Class<MessageCreateData> getResponseType() {
2729
}
2830

2931
@Override
30-
public void deferResponse(InteractionContext context) {
32+
public CompletableFuture<?> deferResponse(InteractionContext context) {
3133
var privacy = privacy(context);
32-
if (privacy == Interaction.PrivacyLevel.PRIVATE) return;
34+
if (privacy == Interaction.PrivacyLevel.PRIVATE) return CompletableFuture.completedFuture(null);
3335

34-
context.component(IReplyCallback.class)
36+
return context.component(IReplyCallback.class)
3537
.assertion()
3638
.deferReply()
3739
.setEphemeral(privacy == Interaction.PrivacyLevel.EPHEMERAL)
3840
.map(context::addChild)
39-
.queue();
41+
.submit();
4042
}
4143

4244
@Override
@@ -84,6 +86,7 @@ public void sendResponse(InteractionContext context, MessageCreateData response)
8486
if (object instanceof Response response) return convertResponse(response);
8587
if (object instanceof EmbedBuilder builder) object = builder.build();
8688
if (object instanceof MessageEmbed embed) object = new MessageCreateBuilder().addEmbeds(embed);
89+
if (object instanceof CharSequence chars) object = new MessageCreateBuilder().setContent(chars.toString());
8790
if (object instanceof MessageCreateBuilder builder) object = builder.build();
8891
if (object instanceof MessageCreateData data) return data;
8992
return null;
@@ -102,10 +105,10 @@ public MessageCreateData convertResponse(Response response) {
102105
}
103106

104107
static Interaction.PrivacyLevel privacy(InteractionContext context) {
105-
return context.component(Interaction.class).map(Interaction::privacy).orElse(Interaction.PrivacyLevel.EPHEMERAL);
108+
return context.getNode().getInteraction().privacy();
106109
}
107110

108111
static boolean async(InteractionContext context) {
109-
return context.component(Interaction.class).filter(Interaction::async).isNonNull();
112+
return context.getNode().getInteraction().async();
110113
}
111114
}

src/main/java/org/comroid/interaction/component/response/ResponseHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import org.comroid.interaction.model.InteractionContext;
44

5+
import java.util.concurrent.CompletableFuture;
6+
57
public interface ResponseHandler<T> {
68
Class<T> getResponseType();
79

810
/// an implementation may choose to override this stub to provide additional context data for a deferred response on async interactions
9-
default void deferResponse(InteractionContext context) {
11+
default CompletableFuture<?> deferResponse(InteractionContext context) {
12+
return CompletableFuture.completedFuture(null);
1013
}
1114

1215
void sendResponse(InteractionContext context, T response);

src/main/java/org/comroid/interaction/model/InteractionContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ public void invoke() {
101101
if (components(PermissionAdapter.class).noneMatch(adp -> adp.verifyPermission(this))) throw Response.of("Insufficient permissions");
102102

103103
if (node.getInteraction().async()) {
104-
component(ResponseHandler.class).ifPresent(it -> it.deferResponse(this));
104+
component(ResponseHandler.class).ifPresent(it -> it.deferResponse(this).join());
105105

106106
CompletableFuture.supplyAsync(() -> node.invoke(this)).thenAccept(this::handle).exceptionally(t -> {
107+
log.log(Level.FINE, "An internal error occurred during async interaction handling", t);
107108
handle(t);
108109
return null;
109110
});

0 commit comments

Comments
 (0)