@@ -264,21 +264,37 @@ private CompletableFuture<?> handleResponse(JdaCommandAdapter.MessageSender hook
264264 }
265265
266266 @ Override
267+ @ SuppressWarnings ("unchecked" )
267268 public void handleResponse (CommandUsage cmd , @ NotNull Object response , Object ... args ) {
268- final var e = of (args ).flatMap (cast (SlashCommandInteractionEvent .class )).findAny ().orElseThrow ();
269- final var user = of (args ).flatMap (cast (User .class )).findAny ().orElseThrow ();
270- var ephemeral = cmd .getStackTrace ().peek ().getAttribute ().privacy () != CommandPrivacyLevel .PUBLIC ;
269+ final var e = of (args ).flatMap (cast (SlashCommandInteractionEvent .class )).findAny ().orElseThrow ();
270+ final var user = of (args ).flatMap (cast (User .class )).findAny ().orElseThrow ();
271271
272- if (response instanceof CompletableFuture ) e .deferReply ()
272+ var ephemeral = cmd .getStackTrace ().peek ().getAttribute ().privacy () != CommandPrivacyLevel .PUBLIC ;
273+ var body = response ;
274+
275+ final Function <Message , RestAction <?>>[] callback = new Function [1 ];
276+
277+ if (body instanceof ResponseCallback (Object wrapperBody , Function <Message , RestAction <?>> wrapperCallback )) {
278+ callback [0 ] = wrapperCallback ;
279+ body = wrapperBody ;
280+ }
281+ if (body instanceof CompletableFuture ) e .deferReply ()
273282 .setEphemeral (ephemeral )
274283 .submit ()
275- .thenCombine (((CompletableFuture <?>) response ),
276- (hook , resp ) -> handleResponse (msg -> hook .sendMessage (msg ).submit (), user , resp ))
284+ .thenCombine (((CompletableFuture <?>) body ), (hook , resp ) -> handleResponse (msg -> {
285+ RestAction <Message > action = hook .sendMessage (msg );
286+ if (callback [0 ] != null ) action = action .flatMap (it -> callback [0 ].apply (it ).map ($ -> it ));
287+ return action .submit ();
288+ }, user , resp ))
277289 .thenCompose (identity ())
278290 .exceptionally (Debug .exceptionLogger ("Could not defer reply to command" ));
279- else handleResponse (msg -> e .reply (msg ).setEphemeral (ephemeral ).submit (),
280- user ,
281- response ).exceptionally (Debug .exceptionLogger ("Could not reply to command" ));
291+ else handleResponse (msg -> {
292+ RestAction <Message > action = e .reply (msg )
293+ .setEphemeral (ephemeral )
294+ .map (hook -> hook .getCallbackResponse ().getMessage ());
295+ if (callback [0 ] != null ) action = action .flatMap (it -> callback [0 ].apply (it ).map ($ -> it ));
296+ return action .submit ();
297+ }, user , body ).exceptionally (Debug .exceptionLogger ("Could not reply to command" ));
282298 }
283299
284300 @ Override
@@ -446,6 +462,8 @@ default CompletableFuture<?> send(String content) {
446462 }
447463 }
448464
465+ public record ResponseCallback (Object body , Function <Message , RestAction <?>> callback ) {}
466+
449467 @ Value
450468 @ NonFinal
451469 public static class PaginatedList <T > extends ListenerAdapter implements UncheckedCloseable {
0 commit comments