Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 26
Feat/cross channel spam automod#553
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
Merged
danthe1st
merged 4 commits into
Java-Discord:main
from
Neil-Tomar:feat/cross-channel-spam-automodJul 21, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12 src/main/java/net/discordjug/javabot/data/config/guild/ModerationConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4 src/main/java/net/discordjug/javabot/data/h2db/message_cache/MessageCacheListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8 src/main/java/net/discordjug/javabot/data/h2db/message_cache/dao/MessageCacheRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10 src/main/java/net/discordjug/javabot/data/h2db/message_cache/model/CachedMessage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 63 additions & 29 deletions
92 src/main/java/net/discordjug/javabot/systems/moderation/AutoMod.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3,6 +3,7 @@ | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import net.discordjug.javabot.data.config.BotConfig; | ||
| import net.discordjug.javabot.data.h2db.message_cache.MessageCache; | ||
| import net.discordjug.javabot.data.h2db.message_cache.model.CachedMessage; | ||
| import net.discordjug.javabot.systems.moderation.warn.model.WarnSeverity; | ||
| import net.discordjug.javabot.systems.notification.NotificationService; | ||
| import net.discordjug.javabot.util.ExceptionLogger; | ||
| @@ -24,9 +25,7 @@ | ||
| import java.net.URL; | ||
| import java.time.Duration; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
| import java.util.*; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| @@ -50,18 +49,19 @@ public class AutoMod extends ListenerAdapter { | ||
| private final MessageCache messageCache; | ||
| /** | ||
| * Constructor of the class, that creates a list of strings with potential spam/scam urls. | ||
| * Constructor of the class, that creates a list of strings with potential spam/scam URLs. | ||
| * | ||
| * @param notificationService The {@link QOTWPointsService} | ||
| * @param botConfig The main configuration of the bot | ||
| * @param moderationService Service object for moderating members | ||
| * @param messageCache service for retrieving cached messages | ||
| * @param botConfig The main configuration of the bot | ||
| * @param moderationService Service object for moderating members | ||
| * @param messageCache service for retrieving cached messages | ||
| */ | ||
| public AutoMod(NotificationService notificationService, BotConfig botConfig, ModerationService moderationService, MessageCache messageCache) { | ||
| this.notificationService = notificationService; | ||
| this.botConfig = botConfig; | ||
| this.moderationService = moderationService; | ||
| this.messageCache = messageCache; | ||
| try(Scanner scan = new Scanner(new URL("https://raw.githubusercontent.com/DevSpen/scam-links/master/src/links.txt").openStream()).useDelimiter("\\A")) { | ||
| try(Scanner scan = new Scanner(new URL("https://raw.githubusercontent.com/DevSpen/scam-links/master/src/links.txt").openStream()).useDelimiter("\\A")) { | ||
| String response = scan.next(); | ||
| spamUrls = List.of(response.split("\n")); | ||
| } catch (IOException e) { | ||
| @@ -105,21 +105,46 @@ private boolean canBypassAutomod(Member member) { | ||
| private void checkNewMessageAutomod(@Nonnull Message message) { | ||
| // spam | ||
| long spamCount = messageCache.getMessagesAfter(message.getTimeCreated().minusSeconds(6)) | ||
| .stream() | ||
| .filter(cached -> cached.getMessageId() != message.getIdLong()) // exclude new/current message | ||
| .filter(cached -> cached.getAuthorId() == message.getAuthor().getIdLong()) | ||
| .filter(cached -> | ||
| // only java files -> not spam | ||
| cached.getAttachments().isEmpty() || | ||
| cached.getAttachments().stream() | ||
| .anyMatch(attachment -> !attachment.contains(".java?"))) | ||
| .count() + 1; // include new message | ||
| .stream() | ||
| .filter(cached -> cached.getMessageId() != message.getIdLong()) // exclude new/current message | ||
| .filter(cached -> cached.getAuthorId() == message.getAuthor().getIdLong()) | ||
| .filter(cached -> | ||
| // only java files -> not spam | ||
| cached.getAttachments().isEmpty() || | ||
| cached.getAttachments().stream() | ||
| .anyMatch(attachment -> !attachment.contains(".java?"))) | ||
| .count() + 1; // include new message | ||
| if (spamCount >= 5) { | ||
| handleSpam(message, message.getMember()); | ||
| handleSpam(message); | ||
| } | ||
| checkContentAutomod(message); | ||
| checkCrossChannelSpam(message); | ||
| } | ||
| private void checkCrossChannelSpam(@Nonnull Message message) { | ||
| int spamWindowSeconds = (botConfig.get(message.getGuild()).getModerationConfig()).getCrossChannelSpamWindowSeconds(); | ||
| Set<Long> channelIds = new HashSet<>(); | ||
| List<CachedMessage> spamMessages = new ArrayList<>(); | ||
danthe1st marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (spamWindowSeconds <= 0) { | ||
| return; | ||
| } | ||
| for (CachedMessage cachedMessage : messageCache.getMessagesAfter(message.getTimeCreated().minusSeconds(spamWindowSeconds))) { | ||
| if (cachedMessage.getMessageId() == message.getIdLong()) { | ||
| continue; | ||
| } | ||
| if (cachedMessage.getAuthorId() != message.getAuthor().getIdLong()) { | ||
| continue; | ||
| } | ||
| channelIds.add(cachedMessage.getChannelId()); | ||
| spamMessages.add(cachedMessage); | ||
| } | ||
| if (channelIds.size() >= (botConfig.get(message.getGuild()).getModerationConfig()).getCrossChannelSpamMinChannels()) { | ||
| handleSpam(spamMessages, message); | ||
| } | ||
| } | ||
| /** | ||
| @@ -130,7 +155,7 @@ private void checkNewMessageAutomod(@Nonnull Message message) { | ||
| private void checkContentAutomod(@Nonnull Message message) { | ||
| //Check for Advertising Links | ||
| if (hasAdvertisingLink(message)) { | ||
| doAutomodActions(message,"Advertising"); | ||
| doAutomodActions(message,"Advertising"); | ||
| } | ||
| //Check for suspicious Links | ||
| @@ -158,20 +183,29 @@ private void doAutomodActions(Message message, String reason) { | ||
| /** | ||
| * Handles detected spam messages. | ||
| * | ||
| * @param msg the (last) spam message | ||
| * @param member the member to be potentially warned | ||
| * @param msg the (last) spam message | ||
| */ | ||
| private void handleSpam(@Nonnull Message msg, Member member) { | ||
| private void handleSpam(@Nonnull Message msg) { | ||
| timeoutForSpam(msg); | ||
| msg.delete().queue(); | ||
| } | ||
| private void handleSpam(@Nonnull List<CachedMessage> cachedMessages, Message message) { | ||
| timeoutForSpam(message); | ||
| cachedMessages.forEach(cachedMessage -> message.getGuild().getTextChannelById(cachedMessage.getChannelId()) | ||
| .deleteMessageById(cachedMessage.getMessageId()).queue()); | ||
| } | ||
| private void timeoutForSpam(@Nonnull Message message) { | ||
| moderationService | ||
danthe1st marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| .timeout( | ||
| member.getUser(), | ||
| message.getAuthor(), | ||
| "Automod: Spam", | ||
| msg.getGuild().getSelfMember(), | ||
| message.getGuild().getSelfMember(), | ||
| Duration.of(6, ChronoUnit.HOURS), | ||
| msg.getChannel(), | ||
| message.getChannel(), | ||
| false | ||
| ); | ||
| msg.delete().queue(); | ||
| } | ||
| /** | ||
| @@ -237,5 +271,5 @@ private boolean isSuggestionsChannel(@NotNull MessageChannelUnion channel) { | ||
| return channel.getType().isGuild() && | ||
| channel.getIdLong() == botConfig.get(channel.asGuildMessageChannel().getGuild()).getModerationConfig().getSuggestionChannel().getIdLong(); | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1 src/main/resources/database/migrations/07-19-2026_add_channel_column_to_cache.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE message_cache ADD COLUMN channel_id BIGINT DEFAULT -1; |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.