Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update JDA, use forwarding information in automod #499

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {

// DIH4JDA (Command Framework) & JDA
implementation("com.github.DynxstyGIT:DIH4JDA:120a15ad2e")
implementation("net.dv8tion:JDA:5.0.0") {
implementation("net.dv8tion:JDA:5.1.2") {
exclude(module = "opus-java")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import lombok.Data;
import net.discordjug.javabot.util.MessageUtils;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.Message.Attachment;

Expand All @@ -27,13 +28,12 @@ public static CachedMessage of(Message message) {
CachedMessage cachedMessage = new CachedMessage();
cachedMessage.setMessageId(message.getIdLong());
cachedMessage.setAuthorId(message.getAuthor().getIdLong());
cachedMessage.setMessageContent(message.getContentRaw().trim());
cachedMessage.setMessageContent(MessageUtils.getMessageContent(message).trim());
cachedMessage.attachments = message
.getAttachments()
.stream()
.map(Attachment::getUrl)
.toList();
return cachedMessage;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.discordjug.javabot.systems.moderation.warn.model.WarnSeverity;
import net.discordjug.javabot.systems.notification.NotificationService;
import net.discordjug.javabot.util.ExceptionLogger;
import net.discordjug.javabot.util.MessageUtils;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
Expand Down Expand Up @@ -132,7 +133,7 @@ private void checkContentAutomod(@Nonnull Message message) {
}

private void doAutomodActions(Message message, String reason) {
notificationService.withGuild(message.getGuild()).sendToModerationLog(c -> c.sendMessageFormat("Message by %s: `%s`", message.getAuthor().getAsMention(), message.getContentRaw()));
notificationService.withGuild(message.getGuild()).sendToModerationLog(c -> c.sendMessageFormat("Message by %s: `%s`", message.getAuthor().getAsMention(), MessageUtils.getMessageContent(message)));
moderationService
.warn(
message.getAuthor(),
Expand Down Expand Up @@ -188,7 +189,7 @@ private void handleSpam(@Nonnull Message msg, Member member) {
* @return True if a link is found and False if not.
*/
public boolean hasSuspiciousLink(@NotNull Message message) {
final String messageRaw = message.getContentRaw();
final String messageRaw = MessageUtils.getMessageContent(message);
Matcher urlMatcher = URL_PATTERN.matcher(messageRaw);
if (messageRaw.contains("http://") || messageRaw.contains("https://")) {
// only do it for a links, so it won't iterate for each message
Expand Down Expand Up @@ -217,7 +218,7 @@ public boolean hasSuspiciousLink(@NotNull Message message) {
*/
public boolean hasAdvertisingLink(@NotNull Message message) {
// Advertising
Matcher matcher = INVITE_URL.matcher(cleanString(message.getContentRaw()));
Matcher matcher = INVITE_URL.matcher(cleanString(MessageUtils.getMessageContent(message)));
int start = 0;
while (matcher.find(start)) {
if (botConfig.get(message.getGuild()).getModerationConfig().getAutomodInviteExcludes().stream().noneMatch(matcher.group()::contains)) {
Expand All @@ -232,4 +233,5 @@ private boolean isSuggestionsChannel(@NotNull MessageChannelUnion channel) {
return channel.getType().isGuild() &&
channel.getIdLong() == botConfig.get(channel.asGuildMessageChannel().getGuild()).getModerationConfig().getSuggestionChannel().getIdLong();
}

}
33 changes: 33 additions & 0 deletions src/main/java/net/discordjug/javabot/util/MessageUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.discordjug.javabot.util;

import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageReference;
import net.dv8tion.jda.api.entities.messages.MessageSnapshot;

/**
* Utility class for JDA messages.
*/
public class MessageUtils {

private MessageUtils() {
//prevent instantiation
}

/**
* Gets the actual content of a message.
* In case of forwarded messages, this gets the content of the forwarded message.
* @param msg the message to check
* @return the content of the passed message
*/
public static String getMessageContent(Message msg) {
//see https://github.com/discord-jda/JDA/releases/tag/v5.1.2
MessageReference messageReference = msg.getMessageReference();
if (messageReference != null && messageReference.getType() == MessageReference.MessageReferenceType.FORWARD) {
MessageSnapshot snapshot = msg.getMessageSnapshots().get(0);
if (snapshot != null) {
return snapshot.getContentRaw();
}
}
return msg.getContentRaw();
}
}