@@ -77,27 +77,28 @@ public static void ensureWebhookExists(@NotNull IWebhookContainer channel, @NotN
7777 * the message
7878 */
7979 public static CompletableFuture <ReadonlyMessage > mirrorMessageToWebhook (@ NotNull Webhook webhook , @ NotNull Message originalMessage , String newMessageContent , long threadId , @ Nullable List <LayoutComponent > components , @ Nullable List <MessageEmbed > embeds ) {
80- JDAWebhookClient client = new WebhookClientBuilder (webhook .getIdLong (), webhook .getToken ())
81- .setThreadId (threadId ).buildJDA ();
82- WebhookMessageBuilder message = new WebhookMessageBuilder ().setContent (newMessageContent )
83- .setAllowedMentions (AllowedMentions .none ())
84- .setAvatarUrl (originalMessage .getMember ().getEffectiveAvatarUrl ())
85- .setUsername (originalMessage .getMember ().getEffectiveName ());
86- if (components != null && !components .isEmpty ()) {
87- message .addComponents (components );
88- }
89- if (embeds != null && !embeds .isEmpty ()) {
90- message .addEmbeds (embeds .stream ().map (e -> WebhookEmbedBuilder .fromJDA (e ).build ()).toList ());
91- }
92- List <Attachment > attachments = originalMessage .getAttachments ();
93- @ SuppressWarnings ("unchecked" )
94- CompletableFuture <?>[] futures = new CompletableFuture <?>[attachments .size ()];
95- for (int i = 0 ; i < attachments .size (); i ++) {
96- Attachment attachment = attachments .get (i );
97- futures [i ] = attachment .getProxy ().download ().thenAccept (
98- is -> message .addFile ((attachment .isSpoiler () ? "SPOILER_" : "" ) + attachment .getFileName (), is ));
80+ try (JDAWebhookClient client = new WebhookClientBuilder (webhook .getIdLong (), webhook .getToken ())
81+ .setThreadId (threadId ).buildJDA ()) {
82+ WebhookMessageBuilder message = new WebhookMessageBuilder ().setContent (newMessageContent )
83+ .setAllowedMentions (AllowedMentions .none ())
84+ .setAvatarUrl (originalMessage .getMember ().getEffectiveAvatarUrl ())
85+ .setUsername (originalMessage .getMember ().getEffectiveName ());
86+ if (components != null && !components .isEmpty ()) {
87+ message .addComponents (components );
88+ }
89+ if (embeds != null && !embeds .isEmpty ()) {
90+ message .addEmbeds (embeds .stream ().map (e -> WebhookEmbedBuilder .fromJDA (e ).build ()).toList ());
91+ }
92+ List <Attachment > attachments = originalMessage .getAttachments ();
93+ @ SuppressWarnings ("unchecked" )
94+ CompletableFuture <?>[] futures = new CompletableFuture <?>[attachments .size ()];
95+ for (int i = 0 ; i < attachments .size (); i ++) {
96+ Attachment attachment = attachments .get (i );
97+ futures [i ] = attachment .getProxy ().download ().thenAccept (
98+ is -> message .addFile ((attachment .isSpoiler () ? "SPOILER_" : "" ) + attachment .getFileName (), is ));
99+ }
100+ return CompletableFuture .allOf (futures ).thenCompose (unused -> client .send (message .build ()))
101+ .whenComplete ((result , err ) -> client .close ());
99102 }
100- return CompletableFuture .allOf (futures ).thenCompose (unused -> client .send (message .build ()))
101- .whenComplete ((result , err ) -> client .close ());
102103 }
103104}
0 commit comments