Skip to content

Commit 9547d3a

Browse files
committed
add new crazy amazing feature that deletes all the bad attachments the wonderful moderation of a given server marked as blacklisted
1 parent fd5403c commit 9547d3a

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

src/main/java/net/discordjug/javabot/data/config/GuildConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.lang.reflect.Field;
1919
import java.nio.file.Files;
2020
import java.nio.file.Path;
21+
import java.util.List;
2122
import java.util.Optional;
2223

2324
/**
@@ -37,6 +38,7 @@ public class GuildConfig {
3738
private StarboardConfig starboardConfig;
3839
private MessageCacheConfig messageCacheConfig;
3940
private ServerLockConfig serverLockConfig;
41+
private List<String> blacklistedMessageExtensions;
4042

4143
/**
4244
* Constructor that initializes all Config classes.
@@ -54,6 +56,8 @@ public GuildConfig(Guild guild, Path file) {
5456
this.starboardConfig = new StarboardConfig();
5557
this.messageCacheConfig = new MessageCacheConfig();
5658
this.serverLockConfig = new ServerLockConfig();
59+
this.blacklistedMessageExtensions = List.of("application/x-msdos-program", "application/java-archive",
60+
"application/zip");
5761
this.setGuild(guild);
5862
}
5963

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package net.discordjug.javabot.listener;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import net.discordjug.javabot.data.config.BotConfig;
5+
import net.discordjug.javabot.data.config.GuildConfig;
6+
import net.discordjug.javabot.util.ExceptionLogger;
7+
import net.discordjug.javabot.util.WebhookUtil;
8+
import net.dv8tion.jda.api.entities.Message;
9+
import net.dv8tion.jda.api.entities.channel.ChannelType;
10+
import net.dv8tion.jda.api.entities.channel.attribute.IWebhookContainer;
11+
import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildChannel;
12+
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
13+
import net.dv8tion.jda.api.hooks.ListenerAdapter;
14+
import org.jetbrains.annotations.NotNull;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
19+
@RequiredArgsConstructor
20+
public class BlacklistedMessageAttachmentListener extends ListenerAdapter {
21+
22+
private final BotConfig botConfig;
23+
24+
@Override
25+
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
26+
if (event.getAuthor().isBot() || event.getAuthor().isSystem()) return;
27+
GuildConfig guildConfig = botConfig.get(event.getGuild());
28+
List<String> blacklistedMessageExtensions = guildConfig.getBlacklistedMessageExtensions();
29+
Message message = event.getMessage();
30+
List<Message.Attachment> attachments = message.getAttachments();
31+
List<Message.Attachment> allowedAttachments = new ArrayList<>();
32+
attachments.forEach(attachment -> {
33+
boolean contains = blacklistedMessageExtensions.contains(attachment.getContentType());
34+
if (!contains) {
35+
allowedAttachments.add(attachment);
36+
}
37+
});
38+
if (message.getAttachments().size() != allowedAttachments.size()) {
39+
IWebhookContainer tc = null;
40+
if (event.isFromType(ChannelType.TEXT)) {
41+
tc = event.getChannel().asTextChannel();
42+
}
43+
if (event.isFromThread()) {
44+
StandardGuildChannel parentChannel = event.getChannel()
45+
.asThreadChannel()
46+
.getParentChannel()
47+
.asStandardGuildChannel();
48+
tc = (IWebhookContainer) parentChannel;
49+
}
50+
if (tc == null) {
51+
return;
52+
}
53+
long threadId = event.isFromThread() ? event.getChannel().getIdLong() : 0;
54+
WebhookUtil.ensureWebhookExists(
55+
tc,
56+
wh -> WebhookUtil.replaceMemberMessageWithAttachments(
57+
wh,
58+
event.getMessage(),
59+
event.getMessage().getContentRaw(),
60+
threadId,
61+
allowedAttachments
62+
),
63+
e -> ExceptionLogger.capture(
64+
e,
65+
"Error creating webhook for UnformattedCodeListener"
66+
)
67+
);
68+
}
69+
}
70+
}

src/main/java/net/discordjug/javabot/util/WebhookUtil.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)