|
| 1 | +package net.javadiscord.javabot.util; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Optional; |
| 5 | +import java.util.concurrent.CompletableFuture; |
| 6 | +import java.util.function.Consumer; |
| 7 | + |
| 8 | +import club.minnced.discord.webhook.WebhookClientBuilder; |
| 9 | +import club.minnced.discord.webhook.external.JDAWebhookClient; |
| 10 | +import club.minnced.discord.webhook.send.AllowedMentions; |
| 11 | +import club.minnced.discord.webhook.send.WebhookMessageBuilder; |
| 12 | +import net.dv8tion.jda.api.entities.Message; |
| 13 | +import net.dv8tion.jda.api.entities.TextChannel; |
| 14 | +import net.dv8tion.jda.api.entities.Webhook; |
| 15 | +import net.dv8tion.jda.api.entities.Message.Attachment; |
| 16 | + |
| 17 | +/** |
| 18 | + * Contains utility methods for dealing with Discord Webhooks. |
| 19 | + */ |
| 20 | +public class WebhookUtil { |
| 21 | + private WebhookUtil() { |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Makes sure that a writable webhook exists in a specific channel. if no |
| 26 | + * suitable webhook is found, one is created. |
| 27 | + * |
| 28 | + * @param channel the {@link TextChannel} the webhook should exist in |
| 29 | + * @param callback an action that is executed once a webhook is |
| 30 | + * found/created |
| 31 | + */ |
| 32 | + public static void ensureWebhookExists(TextChannel channel, Consumer<? super Webhook> callback) { |
| 33 | + ensureWebhookExists(channel, callback, err -> { |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Makes sure that a writable webhook exists in a specific channel. if no |
| 39 | + * suitable webhook is found, one is created. |
| 40 | + * |
| 41 | + * @param channel the {@link TextChannel} the webhook should exist in |
| 42 | + * @param callback an action that is executed once a webhook is |
| 43 | + * found/created |
| 44 | + * @param failureCallback an action that is executed if the webhook |
| 45 | + * lookup/creation failed |
| 46 | + */ |
| 47 | + public static void ensureWebhookExists(TextChannel channel, Consumer<? super Webhook> callback, |
| 48 | + Consumer<? super Throwable> failureCallback) { |
| 49 | + |
| 50 | + channel.retrieveWebhooks().queue(webhooks -> { |
| 51 | + Optional<Webhook> hook = webhooks.stream() |
| 52 | + .filter(webhook -> webhook.getChannel().getIdLong() == channel.getIdLong()) |
| 53 | + .filter(wh -> wh.getToken() != null).findAny(); |
| 54 | + if (hook.isPresent()) { |
| 55 | + callback.accept(hook.get()); |
| 56 | + } else { |
| 57 | + channel.createWebhook("JavaBot-webhook").queue(callback, failureCallback); |
| 58 | + } |
| 59 | + }, failureCallback); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Resends a specific message using a webhook with a custom content. |
| 64 | + * |
| 65 | + * @param webhook the webhook used for sending the message |
| 66 | + * @param originalMessage the message to copy |
| 67 | + * @param newMessageContent the new (custom) content |
| 68 | + * @param threadId the thread to send the message in or {@code 0} if the |
| 69 | + * message should be sent directly |
| 70 | + * @return a {@link CompletableFuture} representing the action of sending |
| 71 | + * the message |
| 72 | + */ |
| 73 | + public static CompletableFuture<Void> mirrorMessageToWebhook(Webhook webhook, Message originalMessage, |
| 74 | + String newMessageContent, long threadId) { |
| 75 | + JDAWebhookClient client = new WebhookClientBuilder(webhook.getIdLong(), webhook.getToken()) |
| 76 | + .setThreadId(threadId).buildJDA(); |
| 77 | + WebhookMessageBuilder message = new WebhookMessageBuilder().setContent(newMessageContent) |
| 78 | + .setAllowedMentions(AllowedMentions.none()) |
| 79 | + .setAvatarUrl(originalMessage.getMember().getEffectiveAvatarUrl()) |
| 80 | + .setUsername(originalMessage.getMember().getEffectiveName()); |
| 81 | + |
| 82 | + List<Attachment> attachments = originalMessage.getAttachments(); |
| 83 | + @SuppressWarnings("unchecked") |
| 84 | + CompletableFuture<?>[] futures = new CompletableFuture<?>[attachments.size()]; |
| 85 | + for (int i = 0; i < attachments.size(); i++) { |
| 86 | + Attachment attachment = attachments.get(i); |
| 87 | + futures[i] = attachment.getProxy().download().thenAccept( |
| 88 | + is -> message.addFile((attachment.isSpoiler() ? "SPOILER_" : "") + attachment.getFileName(), is)); |
| 89 | + } |
| 90 | + return CompletableFuture.allOf(futures).thenAccept(unused -> client.send(message.build())) |
| 91 | + .whenComplete((result, err) -> client.close()); |
| 92 | + } |
| 93 | +} |
0 commit comments