diff --git a/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java b/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java index be69163841..c14e0534c6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/FeatureBlacklist.java @@ -1,8 +1,12 @@ package org.togetherjava.tjbot.config; import com.fasterxml.jackson.annotation.JsonCreator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Set; +import java.util.function.Function; +import java.util.stream.Stream; /** * Blacklist of features, use {@link FeatureBlacklist#isEnabled(T)} to test if a feature is enabled. @@ -12,6 +16,7 @@ */ public class FeatureBlacklist { private final Set featureIdentifierBlacklist; + private static final Logger logger = LoggerFactory.getLogger(FeatureBlacklist.class); /** * Creates a feature blacklist @@ -32,4 +37,15 @@ public FeatureBlacklist(Set featureIdentifierBlacklist) { public boolean isEnabled(T featureId) { return !featureIdentifierBlacklist.contains(featureId); } + + public Stream disableMatching(Stream features, Function idExtractor) { + return features.filter(f -> { + T id = idExtractor.apply(f); + if (!this.isEnabled(id)) { + logger.info("Feature {} is disabled", id); + return false; + } + return true; + }); + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/features/Features.java b/application/src/main/java/org/togetherjava/tjbot/features/Features.java index b98fc75d12..947920fa4d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/Features.java @@ -159,6 +159,6 @@ public static Collection createFeatures(JDA jda, Database database, Con features.add(new JShellCommand(jshellEval)); FeatureBlacklist> blacklist = blacklistConfig.normal(); - return features.stream().filter(f -> blacklist.isEnabled(f.getClass())).toList(); + return blacklist.disableMatching(features.stream(), Feature::getClass).toList(); } } diff --git a/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java b/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java index 916cef8bb6..b764be6717 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java @@ -73,10 +73,11 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements public CodeMessageHandler(FeatureBlacklist blacklist, JShellEval jshellEval) { componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName()); - List codeActions = - Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval)) - .filter(a -> blacklist.isEnabled(a.getClass().getSimpleName())) - .toList(); + List codeActions = blacklist + .disableMatching(Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval)), + e -> e.getClass().getName()) + .toList(); + labelToCodeAction = codeActions.stream() .collect(Collectors.toMap(CodeAction::getLabel, Function.identity(), (x, y) -> y,