|
10 | 10 | import club.minnced.discord.webhook.external.JDAWebhookClient; |
11 | 11 | import club.minnced.discord.webhook.send.AllowedMentions; |
12 | 12 | import club.minnced.discord.webhook.send.WebhookMessageBuilder; |
| 13 | +import lombok.extern.slf4j.Slf4j; |
13 | 14 | import net.dv8tion.jda.api.entities.ChannelType; |
14 | 15 | import net.dv8tion.jda.api.entities.GuildMessageChannel; |
15 | 16 | import net.dv8tion.jda.api.entities.Message; |
|
23 | 24 | /** |
24 | 25 | * Replaces all occurences of 'fuck' in incoming messages with 'hug'. |
25 | 26 | */ |
| 27 | +@Slf4j |
26 | 28 | public class HugListener extends ListenerAdapter { |
27 | 29 | @Override |
28 | 30 | public void onMessageReceived(@Nonnull MessageReceivedEvent event) { |
@@ -60,23 +62,21 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) { |
60 | 62 | } |
61 | 63 |
|
62 | 64 | sb.append(content.substring(indexBkp,content.length())); |
63 | | - event.getMessage().delete().queue(unused->{ |
64 | | - textChannel.retrieveWebhooks().queue(webhooks->{ |
65 | | - Optional<Webhook> hook = webhooks |
66 | | - .stream() |
67 | | - .filter(webhook->webhook.getChannel().getIdLong() == event.getChannel().getIdLong()) |
68 | | - .filter(wh->wh.getToken()!=null) |
69 | | - .findAny(); |
70 | | - if(hook.isPresent()) { |
71 | | - sendWebhookMessage(hook.get(), event.getMessage(), sb.toString(),threadId); |
72 | | - }else { |
73 | | - textChannel |
74 | | - .createWebhook("JavaBot-hug") |
75 | | - .queue(wh-> |
76 | | - sendWebhookMessage(wh, event.getMessage(), sb.toString(), threadId) |
77 | | - ); |
78 | | - } |
79 | | - }); |
| 65 | + textChannel.retrieveWebhooks().queue(webhooks->{ |
| 66 | + Optional<Webhook> hook = webhooks |
| 67 | + .stream() |
| 68 | + .filter(webhook->webhook.getChannel().getIdLong() == textChannel.getIdLong()) |
| 69 | + .filter(wh->wh.getToken()!=null) |
| 70 | + .findAny(); |
| 71 | + if(hook.isPresent()) { |
| 72 | + sendWebhookMessage(hook.get(), event.getMessage(), sb.toString(),threadId); |
| 73 | + }else { |
| 74 | + textChannel |
| 75 | + .createWebhook("JavaBot-hug") |
| 76 | + .queue(wh-> |
| 77 | + sendWebhookMessage(wh, event.getMessage(), sb.toString(), threadId) |
| 78 | + ); |
| 79 | + } |
80 | 80 | }); |
81 | 81 | } |
82 | 82 | } |
@@ -107,6 +107,11 @@ private void sendWebhookMessage(Webhook webhook, Message originalMessage, String |
107 | 107 | ); |
108 | 108 | } |
109 | 109 | CompletableFuture.allOf(futures) |
110 | | - .thenAccept(unused->client.send(message.build())); |
| 110 | + .thenAccept(unused -> client.send(message.build())) |
| 111 | + .thenAccept(unused -> originalMessage.delete().queue()) |
| 112 | + .exceptionally(e ->{ |
| 113 | + log.error("replacing the content 'fuck' with 'hug' in an incoming message failed", e); |
| 114 | + return null; |
| 115 | + }); |
111 | 116 | } |
112 | 117 | } |
0 commit comments