Skip to content

Commit df81696

Browse files
committed
delete messages containing 'fucks' only after replacing them with 'hugs'
1 parent 5fe1d32 commit df81696

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/main/java/net/javadiscord/javabot/listener/HugListener.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import club.minnced.discord.webhook.external.JDAWebhookClient;
1111
import club.minnced.discord.webhook.send.AllowedMentions;
1212
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
13+
import lombok.extern.slf4j.Slf4j;
1314
import net.dv8tion.jda.api.entities.ChannelType;
1415
import net.dv8tion.jda.api.entities.GuildMessageChannel;
1516
import net.dv8tion.jda.api.entities.Message;
@@ -23,6 +24,7 @@
2324
/**
2425
* Replaces all occurences of 'fuck' in incoming messages with 'hug'.
2526
*/
27+
@Slf4j
2628
public class HugListener extends ListenerAdapter {
2729
@Override
2830
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
@@ -60,23 +62,21 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
6062
}
6163

6264
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+
}
8080
});
8181
}
8282
}
@@ -107,6 +107,11 @@ private void sendWebhookMessage(Webhook webhook, Message originalMessage, String
107107
);
108108
}
109109
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+
});
111116
}
112117
}

0 commit comments

Comments
 (0)