[FEATURE] Implement Message Filtering
This commit is contained in:
@@ -62,6 +62,10 @@ public class SDLinkConfig extends ModuleConfig {
|
|||||||
@SpecComment("Execute Minecraft commands in Discord")
|
@SpecComment("Execute Minecraft commands in Discord")
|
||||||
public LinkedCommandsConfig linkedCommands = new LinkedCommandsConfig();
|
public LinkedCommandsConfig linkedCommands = new LinkedCommandsConfig();
|
||||||
|
|
||||||
|
@Path("ignoredMessages")
|
||||||
|
@SpecComment("Configure messages that will be ignored when relaying to discord")
|
||||||
|
public MessageIgnoreConfig ignoreConfig = new MessageIgnoreConfig();
|
||||||
|
|
||||||
public SDLinkConfig() {
|
public SDLinkConfig() {
|
||||||
super("sdlink", "simple-discord-link");
|
super("sdlink", "simple-discord-link");
|
||||||
registerAndSetup(this);
|
registerAndSetup(this);
|
||||||
|
@@ -0,0 +1,52 @@
|
|||||||
|
package com.hypherionmc.sdlink.core.config.impl;
|
||||||
|
|
||||||
|
import me.hypherionmc.moonconfig.core.conversion.Path;
|
||||||
|
import me.hypherionmc.moonconfig.core.conversion.SpecComment;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class MessageIgnoreConfig {
|
||||||
|
|
||||||
|
@Path("ignoredMessages")
|
||||||
|
@SpecComment("Filter certain types of messages from being relayed back to discord")
|
||||||
|
public boolean enabled = true;
|
||||||
|
|
||||||
|
@Path("entries")
|
||||||
|
@SpecComment("List of entries to process")
|
||||||
|
public List<Ignore> entires = new ArrayList<>();
|
||||||
|
|
||||||
|
@Path("ignoredThreads")
|
||||||
|
@SpecComment("Ignore messages sent from certain threads")
|
||||||
|
public List<String> ignoredThread = new ArrayList<>();
|
||||||
|
|
||||||
|
public static class Ignore {
|
||||||
|
@Path("search")
|
||||||
|
@SpecComment("The text to search for in the message")
|
||||||
|
public String search;
|
||||||
|
|
||||||
|
@Path("replace")
|
||||||
|
@SpecComment("Text to replace `search` with, if it's found. Leave empty to ignore")
|
||||||
|
public String replace;
|
||||||
|
|
||||||
|
@Path("searchMode")
|
||||||
|
@SpecComment("How should `search` be found in the text. Valid entries are STARTS_WITH, MATCHES and CONTAINS")
|
||||||
|
public FilterMode searchMode = FilterMode.CONTAINS;
|
||||||
|
|
||||||
|
@Path("action")
|
||||||
|
@SpecComment("How should `replace` be treated, when `search` is found using `searchMode`")
|
||||||
|
public ActionMode action = ActionMode.REPLACE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum FilterMode {
|
||||||
|
STARTS_WITH,
|
||||||
|
MATCHES,
|
||||||
|
CONTAINS
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ActionMode {
|
||||||
|
REPLACE,
|
||||||
|
IGNORE
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -48,6 +48,9 @@ public final class DiscordMessage {
|
|||||||
if (!BotController.INSTANCE.isBotReady())
|
if (!BotController.INSTANCE.isBotReady())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (message.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (messageType == MessageType.CONSOLE) {
|
if (messageType == MessageType.CONSOLE) {
|
||||||
sendConsoleMessage();
|
sendConsoleMessage();
|
||||||
|
@@ -5,8 +5,12 @@
|
|||||||
package com.hypherionmc.sdlink.core.messaging.discord;
|
package com.hypherionmc.sdlink.core.messaging.discord;
|
||||||
|
|
||||||
import com.hypherionmc.sdlink.core.accounts.DiscordAuthor;
|
import com.hypherionmc.sdlink.core.accounts.DiscordAuthor;
|
||||||
|
import com.hypherionmc.sdlink.core.config.SDLinkConfig;
|
||||||
|
import com.hypherionmc.sdlink.core.config.impl.MessageIgnoreConfig;
|
||||||
import com.hypherionmc.sdlink.core.messaging.MessageType;
|
import com.hypherionmc.sdlink.core.messaging.MessageType;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author HypherionSA
|
* @author HypherionSA
|
||||||
* Used to construct a {@link DiscordMessage} to be sent back to discord
|
* Used to construct a {@link DiscordMessage} to be sent back to discord
|
||||||
@@ -43,9 +47,40 @@ public final class DiscordMessageBuilder {
|
|||||||
* The Actual message that will be sent
|
* The Actual message that will be sent
|
||||||
*/
|
*/
|
||||||
public DiscordMessageBuilder message(String message) {
|
public DiscordMessageBuilder message(String message) {
|
||||||
message = message.replace("<@", "");
|
if (this.messageType == MessageType.CHAT) {
|
||||||
message = message.replace("@everyone", "");
|
message = message.replace("<@", "");
|
||||||
message = message.replace("@here", "");
|
message = message.replace("@everyone", "");
|
||||||
|
message = message.replace("@here", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SDLinkConfig.INSTANCE.ignoreConfig.enabled) {
|
||||||
|
for (MessageIgnoreConfig.Ignore i : SDLinkConfig.INSTANCE.ignoreConfig.entires) {
|
||||||
|
if (i.searchMode == MessageIgnoreConfig.FilterMode.MATCHES && message.equalsIgnoreCase(i.search)) {
|
||||||
|
if (i.action == MessageIgnoreConfig.ActionMode.REPLACE) {
|
||||||
|
message = message.replace(i.search, i.replace);
|
||||||
|
} else {
|
||||||
|
message = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i.searchMode == MessageIgnoreConfig.FilterMode.CONTAINS && message.contains(i.search)) {
|
||||||
|
if (i.action == MessageIgnoreConfig.ActionMode.REPLACE) {
|
||||||
|
message = message.replace(i.search, i.replace);
|
||||||
|
} else {
|
||||||
|
message = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i.searchMode == MessageIgnoreConfig.FilterMode.STARTS_WITH && message.startsWith(i.search)) {
|
||||||
|
if (i.action == MessageIgnoreConfig.ActionMode.REPLACE) {
|
||||||
|
message = message.replace(i.search, i.replace);
|
||||||
|
} else {
|
||||||
|
message = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.message = message;
|
this.message = message;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user