Skip to content

Commit 7562bd7

Browse files
committed
use real forwarded content for message logs
1 parent a1876a1 commit 7562bd7

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

src/main/java/net/discordjug/javabot/data/h2db/message_cache/model/CachedMessage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55

66
import lombok.Data;
7+
import net.discordjug.javabot.util.MessageUtils;
78
import net.dv8tion.jda.api.entities.Message;
89
import net.dv8tion.jda.api.entities.Message.Attachment;
910

@@ -27,13 +28,12 @@ public static CachedMessage of(Message message) {
2728
CachedMessage cachedMessage = new CachedMessage();
2829
cachedMessage.setMessageId(message.getIdLong());
2930
cachedMessage.setAuthorId(message.getAuthor().getIdLong());
30-
cachedMessage.setMessageContent(message.getContentRaw().trim());
31+
cachedMessage.setMessageContent(MessageUtils.getMessageContent(message).trim());
3132
cachedMessage.attachments = message
3233
.getAttachments()
3334
.stream()
3435
.map(Attachment::getUrl)
3536
.toList();
3637
return cachedMessage;
3738
}
38-
3939
}

src/main/java/net/discordjug/javabot/systems/moderation/AutoMod.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import net.discordjug.javabot.systems.moderation.warn.model.WarnSeverity;
66
import net.discordjug.javabot.systems.notification.NotificationService;
77
import net.discordjug.javabot.util.ExceptionLogger;
8+
import net.discordjug.javabot.util.MessageUtils;
89
import net.dv8tion.jda.api.Permission;
910
import net.dv8tion.jda.api.entities.Member;
1011
import net.dv8tion.jda.api.entities.Message;
11-
import net.dv8tion.jda.api.entities.MessageReference;
1212
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion;
13-
import net.dv8tion.jda.api.entities.messages.MessageSnapshot;
1413
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
1514
import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
1615
import net.dv8tion.jda.api.hooks.ListenerAdapter;
@@ -134,7 +133,7 @@ private void checkContentAutomod(@Nonnull Message message) {
134133
}
135134

136135
private void doAutomodActions(Message message, String reason) {
137-
notificationService.withGuild(message.getGuild()).sendToModerationLog(c -> c.sendMessageFormat("Message by %s: `%s`", message.getAuthor().getAsMention(), getMessageContent(message)));
136+
notificationService.withGuild(message.getGuild()).sendToModerationLog(c -> c.sendMessageFormat("Message by %s: `%s`", message.getAuthor().getAsMention(), MessageUtils.getMessageContent(message)));
138137
moderationService
139138
.warn(
140139
message.getAuthor(),
@@ -190,7 +189,7 @@ private void handleSpam(@Nonnull Message msg, Member member) {
190189
* @return True if a link is found and False if not.
191190
*/
192191
public boolean hasSuspiciousLink(@NotNull Message message) {
193-
final String messageRaw = getMessageContent(message);
192+
final String messageRaw = MessageUtils.getMessageContent(message);
194193
Matcher urlMatcher = URL_PATTERN.matcher(messageRaw);
195194
if (messageRaw.contains("http://") || messageRaw.contains("https://")) {
196195
// only do it for a links, so it won't iterate for each message
@@ -219,7 +218,7 @@ public boolean hasSuspiciousLink(@NotNull Message message) {
219218
*/
220219
public boolean hasAdvertisingLink(@NotNull Message message) {
221220
// Advertising
222-
Matcher matcher = INVITE_URL.matcher(cleanString(getMessageContent(message)));
221+
Matcher matcher = INVITE_URL.matcher(cleanString(MessageUtils.getMessageContent(message)));
223222
int start = 0;
224223
while (matcher.find(start)) {
225224
if (botConfig.get(message.getGuild()).getModerationConfig().getAutomodInviteExcludes().stream().noneMatch(matcher.group()::contains)) {
@@ -235,15 +234,4 @@ private boolean isSuggestionsChannel(@NotNull MessageChannelUnion channel) {
235234
channel.getIdLong() == botConfig.get(channel.asGuildMessageChannel().getGuild()).getModerationConfig().getSuggestionChannel().getIdLong();
236235
}
237236

238-
private String getMessageContent(Message msg) {
239-
//see https://github.com/discord-jda/JDA/releases/tag/v5.1.2
240-
MessageReference messageReference = msg.getMessageReference();
241-
if (messageReference != null && messageReference.getType() == MessageReference.MessageReferenceType.FORWARD) {
242-
MessageSnapshot snapshot = msg.getMessageSnapshots().get(0);
243-
if (snapshot != null) {
244-
return snapshot.getContentRaw();
245-
}
246-
}
247-
return msg.getContentRaw();
248-
}
249237
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net.discordjug.javabot.util;
2+
3+
import net.dv8tion.jda.api.entities.Message;
4+
import net.dv8tion.jda.api.entities.MessageReference;
5+
import net.dv8tion.jda.api.entities.messages.MessageSnapshot;
6+
7+
/**
8+
* Utility class for JDA messages.
9+
*/
10+
public class MessageUtils {
11+
12+
private MessageUtils() {
13+
//prevent instantiation
14+
}
15+
16+
/**
17+
* Gets the actual content of a message.
18+
* In case of forwarded messages, this gets the content of the forwarded message.
19+
* @param msg the message to check
20+
* @return the content of the passed message
21+
*/
22+
public static String getMessageContent(Message msg) {
23+
//see https://github.com/discord-jda/JDA/releases/tag/v5.1.2
24+
MessageReference messageReference = msg.getMessageReference();
25+
if (messageReference != null && messageReference.getType() == MessageReference.MessageReferenceType.FORWARD) {
26+
MessageSnapshot snapshot = msg.getMessageSnapshots().get(0);
27+
if (snapshot != null) {
28+
return snapshot.getContentRaw();
29+
}
30+
}
31+
return msg.getContentRaw();
32+
}
33+
}

0 commit comments

Comments
 (0)