Skip to content

Commit d36d3c3

Browse files
AlathreonSquidXTV
authored andcommitted
[FeatureBlacklist] Done (#886)
1 parent 72e69bb commit d36d3c3

File tree

6 files changed

+102
-7
lines changed

6 files changed

+102
-7
lines changed

application/config.json.template

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,11 @@
101101
"pruneMemberAmount": 7,
102102
"inactivateAfterDays": 90,
103103
"recentlyJoinedDays": 4
104+
},
105+
"feature_blacklist": {
106+
"normal": [
107+
],
108+
"special": [
109+
]
104110
}
105111
}

application/src/main/java/org/togetherjava/tjbot/config/Config.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public final class Config {
4040
private final String sourceCodeBaseUrl;
4141
private final JShellConfig jshell;
4242
private final HelperPruneConfig helperPruneConfig;
43+
private final FeatureBlacklistConfig featureBlacklistConfig;
4344

4445
@SuppressWarnings("ConstructorWithTooManyParameters")
4546
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
@@ -79,7 +80,9 @@ private Config(@JsonProperty(value = "token", required = true) String token,
7980
@JsonProperty(value = "sourceCodeBaseUrl", required = true) String sourceCodeBaseUrl,
8081
@JsonProperty(value = "jshell", required = true) JShellConfig jshell,
8182
@JsonProperty(value = "helperPruneConfig",
82-
required = true) HelperPruneConfig helperPruneConfig) {
83+
required = true) HelperPruneConfig helperPruneConfig,
84+
@JsonProperty(value = "featureBlacklist",
85+
required = true) FeatureBlacklistConfig featureBlacklistConfig) {
8386
this.token = Objects.requireNonNull(token);
8487
this.gistApiKey = Objects.requireNonNull(gistApiKey);
8588
this.databasePath = Objects.requireNonNull(databasePath);
@@ -106,6 +109,7 @@ private Config(@JsonProperty(value = "token", required = true) String token,
106109
this.sourceCodeBaseUrl = Objects.requireNonNull(sourceCodeBaseUrl);
107110
this.jshell = Objects.requireNonNull(jshell);
108111
this.helperPruneConfig = Objects.requireNonNull(helperPruneConfig);
112+
this.featureBlacklistConfig = Objects.requireNonNull(featureBlacklistConfig);
109113
}
110114

111115
/**
@@ -355,4 +359,13 @@ public JShellConfig getJshell() {
355359
public HelperPruneConfig getHelperPruneConfig() {
356360
return helperPruneConfig;
357361
}
362+
363+
/**
364+
* The configuration of blacklisted features.
365+
*
366+
* @return configuration of blacklisted features
367+
*/
368+
public FeatureBlacklistConfig getFeatureBlacklistConfig() {
369+
return featureBlacklistConfig;
370+
}
358371
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.togetherjava.tjbot.config;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
5+
import java.util.Set;
6+
7+
/**
8+
* Blacklist of features, use {@link FeatureBlacklist#isEnabled(T)} to test if a feature is enabled.
9+
* If a feature is blacklisted, it won't be enabled by the bot, and so will be ignored.
10+
*
11+
* @param <T> the type of the feature identifier
12+
*/
13+
public class FeatureBlacklist<T> {
14+
private final Set<T> featureIdentifierBlacklist;
15+
16+
/**
17+
* Creates a feature blacklist
18+
*
19+
* @param featureIdentifierBlacklist a set of identifiers which are blacklisted
20+
*/
21+
@JsonCreator
22+
public FeatureBlacklist(Set<T> featureIdentifierBlacklist) {
23+
this.featureIdentifierBlacklist = Set.copyOf(featureIdentifierBlacklist);
24+
}
25+
26+
/**
27+
* Returns if a feature is enabled or not.
28+
*
29+
* @param featureId the identifier of the feature
30+
* @return true if a feature is enabled, false otherwise
31+
*/
32+
public boolean isEnabled(T featureId) {
33+
return !featureIdentifierBlacklist.contains(featureId);
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.togetherjava.tjbot.config;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.util.Objects;
6+
7+
/**
8+
* Configuration of the feature blacklist, any feature present here will be disabled.
9+
*
10+
* @param normal the normal features, which are present in
11+
* {@link org.togetherjava.tjbot.features.Features}
12+
* @param special the special features, which require special code
13+
*/
14+
public record FeatureBlacklistConfig(
15+
@JsonProperty(value = "normal", required = true) FeatureBlacklist<Class<?>> normal,
16+
@JsonProperty(value = "special", required = true) FeatureBlacklist<String> special) {
17+
18+
/**
19+
* Creates a FeatureBlacklistConfig.
20+
*
21+
* @param normal the list of normal features, must be not null
22+
* @param special the list of special features, must be not null
23+
*/
24+
public FeatureBlacklistConfig {
25+
Objects.requireNonNull(normal);
26+
Objects.requireNonNull(special);
27+
}
28+
}

application/src/main/java/org/togetherjava/tjbot/features/Features.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import net.dv8tion.jda.api.JDA;
44

55
import org.togetherjava.tjbot.config.Config;
6+
import org.togetherjava.tjbot.config.FeatureBlacklist;
7+
import org.togetherjava.tjbot.config.FeatureBlacklistConfig;
68
import org.togetherjava.tjbot.db.Database;
79
import org.togetherjava.tjbot.features.basic.PingCommand;
810
import org.togetherjava.tjbot.features.basic.RoleSelectCommand;
@@ -19,6 +21,7 @@
1921
import org.togetherjava.tjbot.features.code.CodeMessageManualDetection;
2022
import org.togetherjava.tjbot.features.filesharing.FileSharingMessageListener;
2123
import org.togetherjava.tjbot.features.help.*;
24+
import org.togetherjava.tjbot.features.jshell.JShellCommand;
2225
import org.togetherjava.tjbot.features.jshell.JShellEval;
2326
import org.togetherjava.tjbot.features.mathcommands.TeXCommand;
2427
import org.togetherjava.tjbot.features.mathcommands.wolframalpha.WolframAlphaCommand;
@@ -73,14 +76,16 @@ private Features() {
7376
* @return a collection of all features
7477
*/
7578
public static Collection<Feature> createFeatures(JDA jda, Database database, Config config) {
79+
FeatureBlacklistConfig blacklistConfig = config.getFeatureBlacklistConfig();
7680
JShellEval jshellEval = new JShellEval(config.getJshell());
7781

7882
TagSystem tagSystem = new TagSystem(database);
7983
BookmarksSystem bookmarksSystem = new BookmarksSystem(config, database);
8084
ModerationActionsStore actionsStore = new ModerationActionsStore(database);
8185
ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config);
8286
ScamHistoryStore scamHistoryStore = new ScamHistoryStore(database);
83-
CodeMessageHandler codeMessageHandler = new CodeMessageHandler(jshellEval);
87+
CodeMessageHandler codeMessageHandler =
88+
new CodeMessageHandler(blacklistConfig.special(), jshellEval);
8489
ChatGptService chatGptService = new ChatGptService(config);
8590
HelpSystemHelper helpSystemHelper = new HelpSystemHelper(config, database, chatGptService);
8691

@@ -151,7 +156,9 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
151156
features.add(new ReportCommand(config));
152157
features.add(new BookmarksCommand(bookmarksSystem));
153158
features.add(new ChatGptCommand(chatGptService));
154-
// features.add(new JShellCommand(jshellEval));
155-
return features;
159+
features.add(new JShellCommand(jshellEval));
160+
161+
FeatureBlacklist<Class<?>> blacklist = blacklistConfig.normal();
162+
return features.stream().filter(f -> blacklist.isEnabled(f.getClass())).toList();
156163
}
157164
}

application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.slf4j.Logger;
1616
import org.slf4j.LoggerFactory;
1717

18+
import org.togetherjava.tjbot.config.FeatureBlacklist;
1819
import org.togetherjava.tjbot.features.MessageReceiverAdapter;
1920
import org.togetherjava.tjbot.features.UserInteractionType;
2021
import org.togetherjava.tjbot.features.UserInteractor;
@@ -30,6 +31,7 @@
3031
import java.util.*;
3132
import java.util.function.Function;
3233
import java.util.stream.Collectors;
34+
import java.util.stream.Stream;
3335

3436
/**
3537
* Handles code in registered messages and offers code actions to the user, such as formatting their
@@ -63,14 +65,18 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements
6365

6466
/**
6567
* Creates a new instance.
66-
*
68+
*
69+
* @param blacklist the feature blacklist, used to test if certain code actions should be
70+
* disabled
6771
* @param jshellEval used to execute java code and build visual result
6872
*/
69-
public CodeMessageHandler(JShellEval jshellEval) {
73+
public CodeMessageHandler(FeatureBlacklist<String> blacklist, JShellEval jshellEval) {
7074
componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName());
7175

7276
List<CodeAction> codeActions =
73-
List.of(new FormatCodeCommand()/* , new EvalCodeCommand(jshellEval) */);
77+
Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval))
78+
.filter(a -> blacklist.isEnabled(a.getClass().getSimpleName()))
79+
.toList();
7480

7581
labelToCodeAction = codeActions.stream()
7682
.collect(Collectors.toMap(CodeAction::getLabel, Function.identity(), (x, y) -> y,

0 commit comments

Comments
 (0)