22
33import lombok .extern .slf4j .Slf4j ;
44import net .discordjug .javabot .data .config .BotConfig ;
5+ import net .discordjug .javabot .data .h2db .message_cache .MessageCache ;
56import net .discordjug .javabot .systems .moderation .warn .model .WarnSeverity ;
67import net .discordjug .javabot .systems .notification .NotificationService ;
78import net .discordjug .javabot .util .ExceptionLogger ;
2526import java .time .temporal .ChronoUnit ;
2627import java .util .Collections ;
2728import java .util .List ;
28- import java .util .Objects ;
2929import java .util .Scanner ;
3030import java .util .concurrent .TimeUnit ;
3131import java .util .regex .Matcher ;
3535 * This class checks all incoming messages for potential spam/advertising and warns or mutes the potential offender.
3636 */
3737@ Slf4j
38- // TODO: Refactor this to be more efficient. Especially AutoMod#checkNewMessageAutomod
3938public class AutoMod extends ListenerAdapter {
4039
4140 private static final Pattern INVITE_URL = Pattern .compile ("discord(?:(\\ .(?:me|io|gg)|sites\\ .com)/.{0,4}|(?:app)?\\ .com.{1,4}(?:invite|oauth2).{0,5}/)\\ w+" );
@@ -48,17 +47,20 @@ public class AutoMod extends ListenerAdapter {
4847 private final BotConfig botConfig ;
4948 private List <String > spamUrls ;
5049 private final ModerationService moderationService ;
50+ private final MessageCache messageCache ;
5151
5252 /**
5353 * Constructor of the class, that creates a list of strings with potential spam/scam urls.
5454 * @param notificationService The {@link QOTWPointsService}
5555 * @param botConfig The main configuration of the bot
5656 * @param moderationService Service object for moderating members
57+ * @param messageCache service for retrieving cached messages
5758 */
58- public AutoMod (NotificationService notificationService , BotConfig botConfig , ModerationService moderationService ) {
59+ public AutoMod (NotificationService notificationService , BotConfig botConfig , ModerationService moderationService , MessageCache messageCache ) {
5960 this .notificationService = notificationService ;
6061 this .botConfig = botConfig ;
6162 this .moderationService = moderationService ;
63+ this .messageCache = messageCache ;
6264 try (Scanner scan = new Scanner (new URL ("https://raw.githubusercontent.com/DevSpen/scam-links/master/src/links.txt" ).openStream ()).useDelimiter ("\\ A" )) {
6365 String response = scan .next ();
6466 spamUrls = List .of (response .split ("\n " ));
@@ -102,15 +104,20 @@ private boolean canBypassAutomod(Member member) {
102104 */
103105 private void checkNewMessageAutomod (@ Nonnull Message message ) {
104106 // spam
105- message .getChannel ().getHistory ().retrievePast (10 ).queue (messages -> {
106- int spamCount = (int ) messages .stream ().filter (msg -> !msg .equals (message ))
107- // filter for spam
108- .filter (msg -> msg .getAuthor ().equals (message .getAuthor ()) && !msg .getAuthor ().isBot ())
109- .filter (msg -> (message .getTimeCreated ().toEpochSecond () - msg .getTimeCreated ().toEpochSecond ()) < 6 ).count ();
110- if (spamCount > 5 ) {
111- handleSpam (message , message .getMember ());
112- }
113- });
107+ long spamCount = messageCache .getMessagesAfter (message .getTimeCreated ().minusSeconds (6 ))
108+ .stream ()
109+ .filter (cached -> cached .getMessageId () != message .getIdLong ()) // exclude new/current message
110+ .filter (cached -> cached .getAuthorId () == message .getAuthor ().getIdLong ())
111+ .filter (cached ->
112+ // only java files -> not spam
113+ cached .getAttachments ().isEmpty () ||
114+ cached .getAttachments ().stream ()
115+ .anyMatch (attachment -> !attachment .contains (".java?" )))
116+ .count () + 1 ; // include new message
117+
118+ if (spamCount >= 5 ) {
119+ handleSpam (message , message .getMember ());
120+ }
114121
115122 checkContentAutomod (message );
116123 }
@@ -149,16 +156,12 @@ private void doAutomodActions(Message message, String reason) {
149156 }
150157
151158 /**
152- * Handles potential spam messages.
159+ * Handles detected spam messages.
153160 *
154- * @param msg the message
161+ * @param msg the (last) spam message
155162 * @param member the member to be potentially warned
156163 */
157164 private void handleSpam (@ Nonnull Message msg , Member member ) {
158- // java files -> not spam
159- if (!msg .getAttachments ().isEmpty () && msg .getAttachments ().stream ().allMatch (a -> Objects .equals (a .getFileExtension (), "java" ))) {
160- return ;
161- }
162165 moderationService
163166 .timeout (
164167 member .getUser (),
@@ -168,6 +171,7 @@ private void handleSpam(@Nonnull Message msg, Member member) {
168171 msg .getChannel (),
169172 false
170173 );
174+ msg .delete ().queue ();
171175 }
172176
173177 /**
0 commit comments