Skip to content

Commit

Permalink
Clarify config comments for threads and command execution filters, re…
Browse files Browse the repository at this point in the history
…name filters to execution conditions
  • Loading branch information
Vankka committed Nov 28, 2023
1 parent 1d5a538 commit 91f9f36
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.command.game.GameCommandExecutionHelper;
import com.discordsrv.common.config.main.DiscordCommandConfig;
import com.discordsrv.common.config.main.generic.GameCommandFilterConfig;
import com.discordsrv.common.config.main.generic.GameCommandExecutionConditionConfig;
import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.logging.NamedLogger;
import net.dv8tion.jda.api.entities.Message;
Expand Down Expand Up @@ -59,7 +59,7 @@ public ExecuteCommand(DiscordSRV discordSRV) {
public boolean isNotAcceptableCommand(DiscordGuildMember member, DiscordUser user, String command, boolean suggestions) {
DiscordCommandConfig.ExecuteConfig config = discordSRV.config().discordCommand.execute;

for (GameCommandFilterConfig filter : config.filters) {
for (GameCommandExecutionConditionConfig filter : config.executionConditions) {
if (!filter.isAcceptableCommand(member, user, command, suggestions, helper)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.discordsrv.common.config.main;

import com.discordsrv.common.config.main.generic.DestinationConfig;
import com.discordsrv.common.config.main.generic.GameCommandFilterConfig;
import com.discordsrv.common.config.main.generic.GameCommandExecutionConditionConfig;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;

Expand Down Expand Up @@ -67,15 +67,15 @@ public static class Loggers {
public static class Execution {

public Execution() {
filters.add(
new GameCommandFilterConfig(
executionConditions.add(
new GameCommandExecutionConditionConfig(
new ArrayList<>(),
false,
new ArrayList<>(Arrays.asList("list", "whitelist"))
)
);
filters.add(
new GameCommandFilterConfig(
executionConditions.add(
new GameCommandExecutionConditionConfig(
new ArrayList<>(),
true,
new ArrayList<>(Arrays.asList(
Expand All @@ -92,7 +92,7 @@ public Execution() {
public boolean enabled = true;

@Comment("At least one condition has to match to allow execution")
public List<GameCommandFilterConfig> filters = new ArrayList<>();
public List<GameCommandExecutionConditionConfig> executionConditions = new ArrayList<>();

@Comment("If a command is inputted starting with /, a warning response will be given if this is enabled")
public boolean enableSlashWarning = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.discordsrv.common.config.main;

import com.discordsrv.common.config.configurate.annotation.Constants;
import com.discordsrv.common.config.main.generic.GameCommandFilterConfig;
import com.discordsrv.common.config.main.generic.GameCommandExecutionConditionConfig;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;

Expand All @@ -18,8 +18,8 @@ public class DiscordCommandConfig {
public static class ExecuteConfig {

public ExecuteConfig() {
filters.add(
new GameCommandFilterConfig(
executionConditions.add(
new GameCommandExecutionConditionConfig(
new ArrayList<>(),
false,
new ArrayList<>(Arrays.asList("say", "/gamemode(?: (?:survival|spectator)(?: .+)?)?/"))
Expand All @@ -42,7 +42,7 @@ public ExecuteConfig() {
public OutputMode outputMode = OutputMode.MARKDOWN;

@Comment("At least one condition has to match to allow execution")
public List<GameCommandFilterConfig> filters = new ArrayList<>();
public List<GameCommandExecutionConditionConfig> executionConditions = new ArrayList<>();

@Comment("If commands should be suggested while typing\n" +
"Suggestions go through the server's main thread (on servers with a main thread) to ensure compatability.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
import java.util.regex.Pattern;

@ConfigSerializable
public class GameCommandFilterConfig {
public class GameCommandExecutionConditionConfig {

public GameCommandFilterConfig() {}
@SuppressWarnings("unused") // Configurate
public GameCommandExecutionConditionConfig() {}

public GameCommandFilterConfig(List<Long> roleAndUserIds, boolean blacklist, List<String> commands) {
public GameCommandExecutionConditionConfig(List<Long> roleAndUserIds, boolean blacklist, List<String> commands) {
this.roleAndUserIds = roleAndUserIds;
this.blacklist = blacklist;
this.commands = commands;
}

@Comment("The role and user ids which this filter applies to")
@Comment("The role and user ids that should be allowed to run the commands specified in this condition")
public List<Long> roleAndUserIds = new ArrayList<>();

@Comment("true for blacklist (blocking commands), false for whitelist (allowing commands)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
package com.discordsrv.common.config.main.generic;

import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;

@ConfigSerializable
public class ThreadConfig {

@Comment("Specify the text or forum channel id and the name of the thread (the thread will be automatically created if it doesn't exist)")
public Long channelId = 0L;

public String threadName = "Minecraft Server chat bridge";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.discordsrv.common.command.game.GameCommandExecutionHelper;
import com.discordsrv.common.config.main.ConsoleConfig;
import com.discordsrv.common.config.main.generic.DestinationConfig;
import com.discordsrv.common.config.main.generic.GameCommandFilterConfig;
import com.discordsrv.common.config.main.generic.GameCommandExecutionConditionConfig;
import com.discordsrv.common.console.entry.LogEntry;
import com.discordsrv.common.console.entry.LogMessage;
import com.discordsrv.common.console.message.ConsoleMessage;
Expand Down Expand Up @@ -133,7 +133,7 @@ public void onDiscordMessageReceived(DiscordMessageReceiveEvent event) {
}

boolean pass = false;
for (GameCommandFilterConfig filter : config.commandExecution.filters) {
for (GameCommandExecutionConditionConfig filter : config.commandExecution.executionConditions) {
if (filter.isAcceptableCommand(member, user, command, false, helper)) {
pass = true;
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.discordsrv.common.command.game;

import com.discordsrv.common.config.main.generic.GameCommandFilterConfig;
import com.discordsrv.common.config.main.generic.GameCommandExecutionConditionConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -15,67 +15,72 @@ public class GameCommandFilterTest {

@Test
public void test1() {
Assertions.assertTrue(GameCommandFilterConfig.isCommandMatch("test", "test", false, helper));
Assertions.assertTrue(GameCommandExecutionConditionConfig.isCommandMatch("test", "test", false, helper));
}

@Test
public void test2() {
Assertions.assertFalse(GameCommandFilterConfig.isCommandMatch("test", "tester", false, helper));
Assertions.assertFalse(GameCommandExecutionConditionConfig.isCommandMatch("test", "tester", false, helper));
}

@Test
public void argumentTest() {
Assertions.assertTrue(GameCommandFilterConfig.isCommandMatch("test arg", "test arg", false, helper));
Assertions.assertTrue(GameCommandExecutionConditionConfig.isCommandMatch("test arg", "test arg", false, helper));
}

@Test
public void suggestTest() {
Assertions.assertTrue(GameCommandFilterConfig.isCommandMatch("test arg", "test", true, helper));
Assertions.assertTrue(GameCommandExecutionConditionConfig.isCommandMatch("test arg", "test", true, helper));
}

@Test
public void extraTest() {
Assertions.assertTrue(GameCommandFilterConfig.isCommandMatch("test arg", "test arg extra arguments after that", false, helper));
Assertions.assertTrue(
GameCommandExecutionConditionConfig.isCommandMatch("test arg", "test arg extra arguments after that", false, helper));
}

@Test
public void argumentOverflowTest1() {
Assertions.assertFalse(GameCommandFilterConfig.isCommandMatch("test arg", "test argument", false, helper));
Assertions.assertFalse(
GameCommandExecutionConditionConfig.isCommandMatch("test arg", "test argument", false, helper));
}

@Test
public void sameCommandTest1() {
Assertions.assertFalse(GameCommandFilterConfig.isCommandMatch("plugin1:test", "test", false, helper));
Assertions.assertFalse(GameCommandExecutionConditionConfig.isCommandMatch("plugin1:test", "test", false, helper));
}

@Test
public void sameCommandTest2() {
Assertions.assertTrue(GameCommandFilterConfig.isCommandMatch("plugin2:test", "test", false, helper));
Assertions.assertTrue(GameCommandExecutionConditionConfig.isCommandMatch("plugin2:test", "test", false, helper));
}

@Test
public void regexTest1() {
Assertions.assertTrue(GameCommandFilterConfig.isCommandMatch("/test/", "test", false, helper));
Assertions.assertTrue(GameCommandExecutionConditionConfig.isCommandMatch("/test/", "test", false, helper));
}

@Test
public void regexTest2() {
Assertions.assertFalse(GameCommandFilterConfig.isCommandMatch("/test/", "test extra", false, helper));
Assertions.assertFalse(GameCommandExecutionConditionConfig.isCommandMatch("/test/", "test extra", false, helper));
}

@Test
public void regexTest3() {
Assertions.assertTrue(GameCommandFilterConfig.isCommandMatch("/test( argument)?/", "test argument", false, helper));
Assertions.assertTrue(
GameCommandExecutionConditionConfig.isCommandMatch("/test( argument)?/", "test argument", false, helper));
}

@Test
public void regexTest4() {
Assertions.assertFalse(GameCommandFilterConfig.isCommandMatch("/test( argument)?/", "test fail", false, helper));
Assertions.assertFalse(
GameCommandExecutionConditionConfig.isCommandMatch("/test( argument)?/", "test fail", false, helper));
}

@Test
public void regexTest5() {
Assertions.assertTrue(GameCommandFilterConfig.isCommandMatch("/test( argument)?/", "test", true, helper));
Assertions.assertTrue(
GameCommandExecutionConditionConfig.isCommandMatch("/test( argument)?/", "test", true, helper));
}

public static class ExecutionHelper implements GameCommandExecutionHelper {
Expand Down

0 comments on commit 91f9f36

Please sign in to comment.