@@ -137,6 +137,59 @@ private static CompletableFuture<ReadonlyMessage> mirrorMessageToWebhook(@NotNul
137137 });
138138 }
139139
140+ /**
141+ * Resends a specific message using a webhook with a custom content.
142+ *
143+ * @param webhook the webhook used for sending the message
144+ * @param originalMessage the message to copy
145+ * @param newMessageContent the new (custom) content
146+ * @param threadId the thread to send the message in or {@code 0} if the
147+ * message should be sent directly
148+ * @param components A nullable list of {@link LayoutComponent}s.
149+ * @param attachments A list of {@link Message.Attachment}s.
150+ * @return a {@link CompletableFuture} representing the action of sending
151+ * the message
152+ */
153+ public static CompletableFuture <ReadonlyMessage > mirrorMessageToWebhookWithAttachments (@ NotNull Webhook webhook , @ NotNull Message originalMessage , String newMessageContent , long threadId , @ Nullable List <LayoutComponent > components , List <Message .Attachment > attachments ) {
154+ return originalMessage
155+ .getGuild ()
156+ .retrieveMember (originalMessage .getAuthor ())
157+ .submit ()
158+ .exceptionally (e -> null )//if the member cannot be found, use no member information
159+ .thenCompose (member ->
160+ mirrorMessageToWebhookWithAttachments (webhook , originalMessage , newMessageContent , threadId , components , attachments , member ));
161+ }
162+
163+ private static CompletableFuture <ReadonlyMessage > mirrorMessageToWebhookWithAttachments (@ NotNull Webhook webhook , Message originalMessage , String newMessageContent , long threadId ,
164+ List <LayoutComponent > components , List <Message .Attachment > attachments , Member member ) {
165+ WebhookMessageBuilder message = new WebhookMessageBuilder ().setContent (newMessageContent )
166+ .setAllowedMentions (AllowedMentions .none ())
167+ .setAvatarUrl (transformOrNull (member , Member ::getEffectiveAvatarUrl ))
168+ .setUsername (transformOrNull (member , Member ::getEffectiveName ));
169+ JDAWebhookClient client = new WebhookClientBuilder (webhook .getIdLong (), webhook .getToken ()).setThreadId (threadId )
170+ .buildJDA ();
171+ if (components != null && !components .isEmpty ()) {
172+ message .addComponents (components );
173+ }
174+
175+ @ SuppressWarnings ("unchecked" )
176+ CompletableFuture <?>[] futures = new CompletableFuture <?>[attachments .size ()];
177+ for (int i = 0 ; i < attachments .size (); i ++) {
178+ Attachment attachment = attachments .get (i );
179+ futures [i ] = attachment .getProxy ()
180+ .download ()
181+ .thenAccept (is -> message .addFile ((attachment .isSpoiler () ? "SPOILER_" : "" ) + attachment .getFileName (), is ));
182+ }
183+ return CompletableFuture .allOf (futures )
184+ .thenCompose (unused -> sendMessage (client , message ))
185+ .whenComplete ((result , err ) -> {
186+ client .close ();
187+ if (err != null ) {
188+ ExceptionLogger .capture (err , WebhookUtil .class .getSimpleName ());
189+ }
190+ });
191+ }
192+
140193 private static <T , R > R transformOrNull (T toTransform , Function <T , R > transformer ) {
141194 return toTransform == null ? null : transformer .apply (toTransform );
142195 }
@@ -165,4 +218,22 @@ public static void replaceMemberMessage(Webhook webhook, Message originalMessage
165218 return null ;
166219 });
167220 }
221+
222+ /**
223+ * Method for replacing a user's guild message through a webhook while also replacing the attachments.
224+ *
225+ * @param webhook a reference to a webhook
226+ * @param originalMessage a reference to the {@link Message} object that should be replaced
227+ * @param newMessageContent a String containing the new message's content
228+ * @param threadId id of the thread in which the message should be replaced
229+ * @param attachments attachments to be added to the message
230+ */
231+ public static void replaceMemberMessageWithAttachments (Webhook webhook , Message originalMessage , String newMessageContent , long threadId , List <Message .Attachment > attachments ) {
232+ WebhookUtil .mirrorMessageToWebhookWithAttachments (webhook , originalMessage , newMessageContent , threadId , null , attachments )
233+ .thenAccept (unused -> originalMessage .delete ().queue ())
234+ .exceptionally (e -> {
235+ ExceptionLogger .capture (e , WebhookUtil .class .getSimpleName ());
236+ return null ;
237+ });
238+ }
168239}
0 commit comments