Visitar URL original
Spam detection by cbrt-x · Pull Request #14 · Java-Discord/JavaBot2 · GitHub
Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Refactor inhertitance to normal reference
  • Loading branch information
cbrt-x committed Nov 6, 2021
commit b63e9709e63ac37d3eff42d70cfdb13107174339
12 changes: 9 additions & 3 deletions src/main/java/net/javadiscord/javabot2/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.entity.intent.Intent;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.MessageAuthor;

import java.nio.file.Path;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

Expand Down Expand Up @@ -51,7 +55,7 @@ public class Bot {
/**
* The message cache.
*/
public static MessageCache messageCache;
public static ConcurrentHashMap<MessageAuthor, LinkedList<Message>> messageCache;
Comment thread
cbrt-x marked this conversation as resolved.
Comment thread
cbrt-x marked this conversation as resolved.

// Hide constructor.
private Bot() {}
Expand All @@ -68,7 +72,6 @@ public static void main(String[] args) {
.setAllIntentsExcept(Intent.GUILD_MESSAGE_TYPING, Intent.GUILD_PRESENCES, Intent.GUILD_VOICE_STATES)
.login().join();

messageCache = new MessageCache();
initListeners(api);

Comment thread
cbrt-x marked this conversation as resolved.
config.loadGuilds(api.getServers()); // Once we've logged in, load all guild config files.
Expand All @@ -87,8 +90,11 @@ public static void main(String[] args) {
* @param api the API
*/
private static void initListeners(DiscordApi api) {
MessageCache cache = new MessageCache();
messageCache = cache.getCache();
Comment thread
cbrt-x marked this conversation as resolved.

api.addMessageCreateListener(new SpamListener());
api.addMessageCreateListener(messageCache);
api.addMessageCreateListener(cache);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
/**
* Caches incoming messages and cleans it out when needed.
*/
public class MessageCache extends ConcurrentHashMap<MessageAuthor, LinkedList<Message>> implements MessageCreateListener {
public class MessageCache implements MessageCreateListener {

private final ConcurrentHashMap<MessageAuthor, Instant> lastModifications;
private final ConcurrentHashMap<MessageAuthor, LinkedList<Message>> cache;
Comment thread
cbrt-x marked this conversation as resolved.

/**
* Creates the cache.
*/
public MessageCache() {
lastModifications = new ConcurrentHashMap<>();
cache = new ConcurrentHashMap<>();
Comment thread
cbrt-x marked this conversation as resolved.
// TODO config
Bot.asyncPool.scheduleAtFixedRate(this::clean, -1, -1, TimeUnit.MINUTES);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

}
Expand All @@ -43,7 +45,7 @@ private void clean() {
// if last modification is older than n minutes
// TODO config
if (instant.isAfter(Instant.now().minus(Duration.ofMinutes(-1)))) {
Comment thread
cbrt-x marked this conversation as resolved.
this.remove(messageAuthor);
cache.remove(messageAuthor);
lastModifications.remove(messageAuthor);
}
});
Expand All @@ -55,8 +57,8 @@ private void clean() {
* @param msg the message to be added
*/
private void add(Message msg) {
if (this.containsKey(msg.getAuthor())) {
LinkedList<Message> msgList = this.get(msg.getAuthor());
if (cache.containsKey(msg.getAuthor())) {
LinkedList<Message> msgList = cache.get(msg.getAuthor());
Comment thread
cbrt-x marked this conversation as resolved.
msgList.offer(msg);
Comment thread
cbrt-x marked this conversation as resolved.
lastModifications.replace(msg.getAuthor(), Instant.now());

Expand All @@ -68,9 +70,13 @@ private void add(Message msg) {
} else {
LinkedList<Message> messages = new LinkedList<>();
Comment thread
cbrt-x marked this conversation as resolved.
messages.add(msg);
this.put(msg.getAuthor(), messages);
cache.put(msg.getAuthor(), messages);
lastModifications.put(msg.getAuthor(), Instant.now());
}
}

public ConcurrentHashMap<MessageAuthor, LinkedList<Message>> getCache() {
return cache;
}
Comment thread
cbrt-x marked this conversation as resolved.
}