This repository was archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Spam detection #14
Open
cbrt-x
wants to merge
3
commits into
Java-Discord:main
Choose a base branch
from
cbrt-x:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Spam detection #14
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
82 changes: 82 additions & 0 deletions
82
src/main/java/net/javadiscord/javabot2/systems/moderation/MessageCache.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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package net.javadiscord.javabot2.systems.moderation; | ||
|
||
import net.javadiscord.javabot2.Bot; | ||
import org.javacord.api.entity.message.Message; | ||
import org.javacord.api.entity.message.MessageAuthor; | ||
import org.javacord.api.event.message.MessageCreateEvent; | ||
import org.javacord.api.listener.message.MessageCreateListener; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.LinkedList; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Caches incoming messages and cleans it out when needed. | ||
*/ | ||
public class MessageCache implements MessageCreateListener { | ||
|
||
private final ConcurrentHashMap<MessageAuthor, Instant> lastModifications; | ||
private final ConcurrentHashMap<MessageAuthor, LinkedList<Message>> cache; | ||
cbrt-x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Creates the cache. | ||
*/ | ||
public MessageCache() { | ||
lastModifications = new ConcurrentHashMap<>(); | ||
cache = new ConcurrentHashMap<>(); | ||
cbrt-x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// TODO config | ||
Bot.asyncPool.scheduleAtFixedRate(this::clean, -1, -1, TimeUnit.MINUTES); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't do this here; I would never expect constructing an object to begin an irreversible scheduled task. |
||
} | ||
|
||
@Override | ||
public void onMessageCreate(MessageCreateEvent event) { | ||
if (!event.getMessageAuthor().isYourself()) { | ||
cbrt-x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
add(event.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Removes all Map entries which have not been modified within the last 10 minutes. | ||
*/ | ||
private void clean() { | ||
lastModifications.forEach((messageAuthor, instant) -> { | ||
// if last modification is older than n minutes | ||
// TODO config | ||
if (instant.isAfter(Instant.now().minus(Duration.ofMinutes(-1)))) { | ||
cbrt-x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cache.remove(messageAuthor); | ||
lastModifications.remove(messageAuthor); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Add the message either as a new Map entry or to the existing list per user. | ||
* Also removes if the amount of messages per user is over a certain treshold. | ||
* @param msg the message to be added | ||
*/ | ||
private void add(Message msg) { | ||
if (cache.containsKey(msg.getAuthor())) { | ||
LinkedList<Message> msgList = cache.get(msg.getAuthor()); | ||
cbrt-x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
msgList.offer(msg); | ||
cbrt-x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lastModifications.replace(msg.getAuthor(), Instant.now()); | ||
|
||
// TODO config | ||
if (msgList.size() >= -1) { | ||
msgList.poll(); | ||
} | ||
cbrt-x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} else { | ||
LinkedList<Message> messages = new LinkedList<>(); | ||
cbrt-x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
messages.add(msg); | ||
cache.put(msg.getAuthor(), messages); | ||
lastModifications.put(msg.getAuthor(), Instant.now()); | ||
} | ||
} | ||
|
||
public ConcurrentHashMap<MessageAuthor, LinkedList<Message>> getCache() { | ||
return cache; | ||
} | ||
cbrt-x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
34 changes: 34 additions & 0 deletions
34
src/main/java/net/javadiscord/javabot2/systems/moderation/SpamListener.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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package net.javadiscord.javabot2.systems.moderation; | ||
|
||
import net.javadiscord.javabot2.Bot; | ||
import org.javacord.api.event.message.MessageCreateEvent; | ||
import org.javacord.api.listener.message.MessageCreateListener; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
|
||
/** | ||
* Listens for spam using the {@link MessageCache}. | ||
*/ | ||
public class SpamListener implements MessageCreateListener { | ||
cbrt-x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public void onMessageCreate(MessageCreateEvent event) { | ||
if (!event.getMessageAuthor().isYourself() && Bot.messageCache.containsKey(event.getMessageAuthor())) { | ||
cbrt-x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int amountOfMessages = (int) Bot.messageCache.get(event.getMessageAuthor()) | ||
.stream() | ||
//TODO get time from config | ||
.filter(msg -> msg.getCreationTimestamp().isAfter(Instant.now().minus(Duration.ofSeconds(-1)))) | ||
.count(); | ||
|
||
//TODO get amount from config | ||
if (amountOfMessages >= -1) { | ||
//TODO spam detected | ||
// placeholder for checkstyle not to complain. | ||
// should be replaced with a warn + purge which is not implemented yet | ||
event.deleteMessage("spam"); | ||
} | ||
Comment on lines
+18
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Address these TODOs |
||
} | ||
} | ||
|
||
} |
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.