22
33import lombok .extern .slf4j .Slf4j ;
44import net .discordjug .javabot .data .config .BotConfig ;
5+ import net .discordjug .javabot .data .config .guild .ModerationConfig ;
56import net .discordjug .javabot .data .h2db .message_cache .MessageCache ;
7+ import net .discordjug .javabot .data .h2db .message_cache .model .CachedMessage ;
68import net .discordjug .javabot .systems .moderation .warn .model .WarnSeverity ;
79import net .discordjug .javabot .systems .notification .NotificationService ;
810import net .discordjug .javabot .util .ExceptionLogger ;
911import net .discordjug .javabot .util .MessageUtils ;
1012import net .dv8tion .jda .api .Permission ;
13+ import net .dv8tion .jda .api .entities .Guild ;
1114import net .dv8tion .jda .api .entities .Member ;
1215import net .dv8tion .jda .api .entities .Message ;
1316import net .dv8tion .jda .api .entities .channel .unions .MessageChannelUnion ;
2427import java .net .URL ;
2528import java .time .Duration ;
2629import java .time .temporal .ChronoUnit ;
30+ import java .util .ArrayList ;
2731import java .util .Collections ;
2832import java .util .List ;
2933import java .util .Scanner ;
@@ -50,7 +54,7 @@ public class AutoMod extends ListenerAdapter {
5054 private final MessageCache messageCache ;
5155
5256 /**
53- * Constructor of the class, that creates a list of strings with potential spam/scam urls .
57+ * Constructor of the class, that creates a list of strings with potential spam/scam URLs .
5458 * @param notificationService The {@link QOTWPointsService}
5559 * @param botConfig The main configuration of the bot
5660 * @param moderationService Service object for moderating members
@@ -108,18 +112,33 @@ private void checkNewMessageAutomod(@Nonnull Message message) {
108112 .stream ()
109113 .filter (cached -> cached .getMessageId () != message .getIdLong ()) // exclude new/current message
110114 .filter (cached -> cached .getAuthorId () == message .getAuthor ().getIdLong ())
111- .filter (cached ->
115+ .filter (cached ->
112116 // only java files -> not spam
113- cached .getAttachments ().isEmpty () ||
117+ cached .getAttachments ().isEmpty () ||
114118 cached .getAttachments ().stream ()
115119 .anyMatch (attachment -> !attachment .contains (".java?" )))
116120 .count () + 1 ; // include new message
117-
121+
118122 if (spamCount >= 5 ) {
119123 handleSpam (message , message .getMember ());
120124 }
121125
122126 checkContentAutomod (message );
127+ checkCrossChannelSpam (message );
128+ }
129+
130+ private void checkCrossChannelSpam (@ Nonnull Message message ) {
131+ List <CachedMessage > spamMessages = new ArrayList <>();
132+ messageCache .getMessagesAfter (message .getTimeCreated ().minusSeconds ((botConfig .get (message .getGuild ()).getModerationConfig ()).getCrossChannelSpamWindowSeconds ())).stream ()
133+ .filter (cachedMessage -> cachedMessage .getAuthorId () == message .getAuthor ().getIdLong ())
134+ .filter (cachedMessage -> spamMessages .stream ().noneMatch (
135+ spamMessage -> spamMessage .getChannelId () == cachedMessage .getChannelId ()))
136+ .forEach (spamMessages ::add );
137+
138+ if (spamMessages .size () > (botConfig .get (message .getGuild ()).getModerationConfig ()).getCrossChannelSpamMinChannels ()){
139+ handleSpam (spamMessages ,message );
140+ }
141+
123142 }
124143
125144 /**
@@ -174,6 +193,22 @@ private void handleSpam(@Nonnull Message msg, Member member) {
174193 msg .delete ().queue ();
175194 }
176195
196+ private void handleSpam (@ Nonnull List <CachedMessage > cachedMessages , Message message ) {
197+ Guild guild = message .getGuild ();
198+ moderationService
199+ .timeout (
200+ message .getAuthor (),
201+ "Automod: Spam" ,
202+ message .getGuild ().getSelfMember (),
203+ Duration .of (6 , ChronoUnit .HOURS ),
204+ message .getChannel (),
205+ false
206+ );
207+
208+ cachedMessages .forEach (cachedMessage -> guild .getTextChannelById (cachedMessage .getChannelId ())
209+ .deleteMessageById (cachedMessage .getMessageId ()).queue ());
210+ }
211+
177212 /**
178213 * returns the original String cleaned up of unused code points and spaces.
179214 *
@@ -218,7 +253,7 @@ public boolean hasSuspiciousLink(@NotNull Message message) {
218253 * Checks whether the given message contains a discord invite link.
219254 *
220255 * @param message The Message to check.
221- * @return True if an invite is found and False if not.
256+ * @return True if an invitation is found and False if not.
222257 */
223258 public boolean hasAdvertisingLink (@ NotNull Message message ) {
224259 // Advertising
@@ -237,5 +272,5 @@ private boolean isSuggestionsChannel(@NotNull MessageChannelUnion channel) {
237272 return channel .getType ().isGuild () &&
238273 channel .getIdLong () == botConfig .get (channel .asGuildMessageChannel ().getGuild ()).getModerationConfig ().getSuggestionChannel ().getIdLong ();
239274 }
240-
275+
241276}
0 commit comments