|
1 | 1 | package net.javadiscord.javabot.listener; |
2 | 2 |
|
3 | | -import java.util.List; |
4 | | -import java.util.Optional; |
5 | | -import java.util.concurrent.CompletableFuture; |
6 | | - |
7 | 3 | import javax.annotation.Nonnull; |
8 | 4 |
|
9 | | -import club.minnced.discord.webhook.WebhookClientBuilder; |
10 | | -import club.minnced.discord.webhook.external.JDAWebhookClient; |
11 | | -import club.minnced.discord.webhook.send.AllowedMentions; |
12 | | -import club.minnced.discord.webhook.send.WebhookMessageBuilder; |
13 | 5 | import lombok.extern.slf4j.Slf4j; |
14 | 6 | import net.dv8tion.jda.api.entities.ChannelType; |
15 | 7 | import net.dv8tion.jda.api.entities.GuildMessageChannel; |
16 | 8 | import net.dv8tion.jda.api.entities.Message; |
17 | | -import net.dv8tion.jda.api.entities.Message.Attachment; |
18 | 9 | import net.dv8tion.jda.api.entities.TextChannel; |
19 | 10 | import net.dv8tion.jda.api.entities.Webhook; |
20 | 11 | import net.dv8tion.jda.api.events.message.MessageReceivedEvent; |
21 | 12 | import net.dv8tion.jda.api.hooks.ListenerAdapter; |
22 | 13 | import net.javadiscord.javabot.Bot; |
| 14 | +import net.javadiscord.javabot.util.WebhookUtil; |
23 | 15 |
|
24 | 16 | /** |
25 | 17 | * Replaces all occurences of 'fuck' in incoming messages with 'hug'. |
|
28 | 20 | public class HugListener extends ListenerAdapter { |
29 | 21 | @Override |
30 | 22 | public void onMessageReceived(@Nonnull MessageReceivedEvent event) { |
31 | | - if(!event.isFromGuild()) { |
| 23 | + if (!event.isFromGuild()) { |
32 | 24 | return; |
33 | 25 | } |
34 | 26 | if (Bot.autoMod.hasSuspiciousLink(event.getMessage()) || Bot.autoMod.hasAdvertisingLink(event.getMessage())) { |
35 | 27 | return; |
36 | 28 | } |
| 29 | + if (!event.getMessage().getMentions().getUsers().isEmpty()) { |
| 30 | + return; |
| 31 | + } |
| 32 | + if (event.isWebhookMessage()) { |
| 33 | + return; |
| 34 | + } |
37 | 35 | TextChannel tc = null; |
38 | | - if(event.isFromType(ChannelType.TEXT)) { |
| 36 | + if (event.isFromType(ChannelType.TEXT)) { |
39 | 37 | tc = event.getTextChannel(); |
40 | 38 | } |
41 | | - if(event.isFromThread()) { |
| 39 | + if (event.isFromThread()) { |
42 | 40 | GuildMessageChannel parentChannel = event.getThreadChannel().getParentMessageChannel(); |
43 | | - if(parentChannel instanceof TextChannel textChannel) { |
| 41 | + if (parentChannel instanceof TextChannel textChannel) { |
44 | 42 | tc = textChannel; |
45 | 43 | } |
46 | 44 | } |
47 | | - if(tc == null) { |
| 45 | + if (tc == null) { |
48 | 46 | return; |
49 | 47 | } |
50 | 48 | final TextChannel textChannel = tc; |
51 | 49 | String content = event.getMessage().getContentRaw(); |
52 | 50 | String lowerCaseContent = content.toLowerCase(); |
53 | | - if(lowerCaseContent.contains("fuck")) { |
54 | | - long threadId = event.isFromThread()?event.getThreadChannel().getIdLong():0; |
| 51 | + if (lowerCaseContent.contains("fuck")) { |
| 52 | + long threadId = event.isFromThread() ? event.getThreadChannel().getIdLong() : 0; |
55 | 53 | StringBuilder sb = new StringBuilder(content.length()); |
56 | 54 | int index = 0; |
57 | 55 | int indexBkp = index; |
58 | | - while((index = lowerCaseContent.indexOf("fuck",index)) != -1) { |
59 | | - sb.append(content.substring(indexBkp,index)); |
| 56 | + while ((index = lowerCaseContent.indexOf("fuck", index)) != -1) { |
| 57 | + sb.append(content.substring(indexBkp, index)); |
60 | 58 | sb.append("hug"); |
61 | | - indexBkp=index++ +4; |
| 59 | + indexBkp = index++ + 4; |
62 | 60 | } |
63 | | - |
64 | | - sb.append(content.substring(indexBkp,content.length())); |
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 | | - }); |
| 61 | + |
| 62 | + sb.append(content.substring(indexBkp, content.length())); |
| 63 | + WebhookUtil.ensureWebhookExists(textChannel, |
| 64 | + wh -> sendWebhookMessage(wh, event.getMessage(), sb.toString(), threadId), |
| 65 | + e -> log.error("Webhook lookup/creation failed", e)); |
81 | 66 | } |
82 | 67 | } |
83 | | - |
84 | | - private void sendWebhookMessage(Webhook webhook, Message originalMessage, String newMessageContent, long threadId){ |
85 | | - JDAWebhookClient client = new WebhookClientBuilder(webhook.getIdLong(), webhook.getToken()) |
86 | | - .setThreadId(threadId) |
87 | | - .buildJDA(); |
88 | | - WebhookMessageBuilder message = new WebhookMessageBuilder() |
89 | | - .setContent(newMessageContent) |
90 | | - .setAllowedMentions(AllowedMentions.none()) |
91 | | - .setAvatarUrl(originalMessage.getMember().getEffectiveAvatarUrl()) |
92 | | - .setUsername(originalMessage.getMember().getEffectiveName()); |
93 | | - |
94 | | - List<Attachment> attachments = originalMessage.getAttachments(); |
95 | | - @SuppressWarnings("unchecked") |
96 | | - CompletableFuture<?>[] futures = new CompletableFuture<?>[attachments.size()]; |
97 | | - for(int i = 0; i < attachments.size(); i++){ |
98 | | - Attachment attachment = attachments.get(i); |
99 | | - futures[i] = attachment |
100 | | - .getProxy() |
101 | | - .download() |
102 | | - .thenAccept(is -> |
103 | | - message.addFile( |
104 | | - (attachment.isSpoiler()?"SPOILER_":"")+attachment.getFileName(), |
105 | | - is |
106 | | - ) |
107 | | - ); |
108 | | - } |
109 | | - CompletableFuture.allOf(futures) |
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 | | - }); |
| 68 | + |
| 69 | + private void sendWebhookMessage(Webhook webhook, Message originalMessage, String newMessageContent, long threadId) { |
| 70 | + WebhookUtil.mirrorMessageToWebhook(webhook, originalMessage, newMessageContent, threadId) |
| 71 | + .thenAccept(unused -> originalMessage.delete().queue()).exceptionally(e -> { |
| 72 | + log.error("replacing the content 'fuck' with 'hug' in an incoming message failed", e); |
| 73 | + return null; |
| 74 | + }); |
116 | 75 | } |
117 | 76 | } |
0 commit comments