Skip to content

Commit 6d9f6c0

Browse files
authored
Ensure config is present and not null (#592)
* Ensure config is present and not null * token is also required
1 parent 802097f commit 6d9f6c0

File tree

4 files changed

+79
-62
lines changed

4 files changed

+79
-62
lines changed

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

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.nio.file.Path;
1010
import java.util.Collections;
1111
import java.util.List;
12+
import java.util.Objects;
1213

1314
/**
1415
* Configuration of the application. Create instances using {@link #load(Path)}.
@@ -35,40 +36,47 @@ public final class Config {
3536

3637
@SuppressWarnings("ConstructorWithTooManyParameters")
3738
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
38-
private Config(@JsonProperty("token") String token,
39-
@JsonProperty("gistApiKey") String gistApiKey,
40-
@JsonProperty("databasePath") String databasePath,
41-
@JsonProperty("projectWebsite") String projectWebsite,
42-
@JsonProperty("discordGuildInvite") String discordGuildInvite,
43-
@JsonProperty("modAuditLogChannelPattern") String modAuditLogChannelPattern,
44-
@JsonProperty("mutedRolePattern") String mutedRolePattern,
45-
@JsonProperty("heavyModerationRolePattern") String heavyModerationRolePattern,
46-
@JsonProperty("softModerationRolePattern") String softModerationRolePattern,
47-
@JsonProperty("tagManageRolePattern") String tagManageRolePattern,
48-
@JsonProperty("suggestions") SuggestionsConfig suggestions,
49-
@JsonProperty("quarantinedRolePattern") String quarantinedRolePattern,
50-
@JsonProperty("scamBlocker") ScamBlockerConfig scamBlocker,
51-
@JsonProperty("wolframAlphaAppId") String wolframAlphaAppId,
52-
@JsonProperty("helpSystem") HelpSystemConfig helpSystem,
53-
@JsonProperty("mediaOnlyChannelPattern") String mediaOnlyChannelPattern,
54-
@JsonProperty("blacklistedFileExtension") List<String> blacklistedFileExtension) {
55-
this.token = token;
56-
this.gistApiKey = gistApiKey;
57-
this.databasePath = databasePath;
58-
this.projectWebsite = projectWebsite;
59-
this.discordGuildInvite = discordGuildInvite;
60-
this.modAuditLogChannelPattern = modAuditLogChannelPattern;
61-
this.mutedRolePattern = mutedRolePattern;
62-
this.heavyModerationRolePattern = heavyModerationRolePattern;
63-
this.softModerationRolePattern = softModerationRolePattern;
64-
this.tagManageRolePattern = tagManageRolePattern;
65-
this.suggestions = suggestions;
66-
this.quarantinedRolePattern = quarantinedRolePattern;
67-
this.scamBlocker = scamBlocker;
68-
this.wolframAlphaAppId = wolframAlphaAppId;
69-
this.helpSystem = helpSystem;
70-
this.mediaOnlyChannelPattern = mediaOnlyChannelPattern;
71-
this.blacklistedFileExtension = blacklistedFileExtension;
39+
private Config(@JsonProperty(value = "token", required = true) String token,
40+
@JsonProperty(value = "gistApiKey", required = true) String gistApiKey,
41+
@JsonProperty(value = "databasePath", required = true) String databasePath,
42+
@JsonProperty(value = "projectWebsite", required = true) String projectWebsite,
43+
@JsonProperty(value = "discordGuildInvite", required = true) String discordGuildInvite,
44+
@JsonProperty(value = "modAuditLogChannelPattern",
45+
required = true) String modAuditLogChannelPattern,
46+
@JsonProperty(value = "mutedRolePattern", required = true) String mutedRolePattern,
47+
@JsonProperty(value = "heavyModerationRolePattern",
48+
required = true) String heavyModerationRolePattern,
49+
@JsonProperty(value = "softModerationRolePattern",
50+
required = true) String softModerationRolePattern,
51+
@JsonProperty(value = "tagManageRolePattern",
52+
required = true) String tagManageRolePattern,
53+
@JsonProperty(value = "suggestions", required = true) SuggestionsConfig suggestions,
54+
@JsonProperty(value = "quarantinedRolePattern",
55+
required = true) String quarantinedRolePattern,
56+
@JsonProperty(value = "scamBlocker", required = true) ScamBlockerConfig scamBlocker,
57+
@JsonProperty(value = "wolframAlphaAppId", required = true) String wolframAlphaAppId,
58+
@JsonProperty(value = "helpSystem", required = true) HelpSystemConfig helpSystem,
59+
@JsonProperty(value = "mediaOnlyChannelPattern",
60+
required = true) String mediaOnlyChannelPattern,
61+
@JsonProperty(value = "blacklistedFileExtension",
62+
required = true) List<String> blacklistedFileExtension) {
63+
this.token = Objects.requireNonNull(token);
64+
this.gistApiKey = Objects.requireNonNull(gistApiKey);
65+
this.databasePath = Objects.requireNonNull(databasePath);
66+
this.projectWebsite = Objects.requireNonNull(projectWebsite);
67+
this.discordGuildInvite = Objects.requireNonNull(discordGuildInvite);
68+
this.modAuditLogChannelPattern = Objects.requireNonNull(modAuditLogChannelPattern);
69+
this.mutedRolePattern = Objects.requireNonNull(mutedRolePattern);
70+
this.heavyModerationRolePattern = Objects.requireNonNull(heavyModerationRolePattern);
71+
this.softModerationRolePattern = Objects.requireNonNull(softModerationRolePattern);
72+
this.tagManageRolePattern = Objects.requireNonNull(tagManageRolePattern);
73+
this.suggestions = Objects.requireNonNull(suggestions);
74+
this.quarantinedRolePattern = Objects.requireNonNull(quarantinedRolePattern);
75+
this.scamBlocker = Objects.requireNonNull(scamBlocker);
76+
this.wolframAlphaAppId = Objects.requireNonNull(wolframAlphaAppId);
77+
this.helpSystem = Objects.requireNonNull(helpSystem);
78+
this.mediaOnlyChannelPattern = Objects.requireNonNull(mediaOnlyChannelPattern);
79+
this.blacklistedFileExtension = Objects.requireNonNull(blacklistedFileExtension);
7280
}
7381

7482
/**

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import java.util.ArrayList;
88
import java.util.Collections;
99
import java.util.List;
10+
import java.util.Objects;
1011

1112
/**
1213
* Configuration for the help system, see {@link org.togetherjava.tjbot.commands.help.AskCommand}.
1314
*/
14-
@SuppressWarnings("ClassCanBeRecord")
1515
@JsonRootName("helpSystem")
1616
public final class HelpSystemConfig {
1717
private final String stagingChannelPattern;
@@ -20,14 +20,18 @@ public final class HelpSystemConfig {
2020
private final String categoryRoleSuffix;
2121

2222
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
23-
private HelpSystemConfig(@JsonProperty("stagingChannelPattern") String stagingChannelPattern,
24-
@JsonProperty("overviewChannelPattern") String overviewChannelPattern,
25-
@JsonProperty("categories") List<String> categories,
26-
@JsonProperty("categoryRoleSuffix") String categoryRoleSuffix) {
27-
this.stagingChannelPattern = stagingChannelPattern;
28-
this.overviewChannelPattern = overviewChannelPattern;
29-
this.categories = new ArrayList<>(categories);
30-
this.categoryRoleSuffix = categoryRoleSuffix;
23+
private HelpSystemConfig(
24+
@JsonProperty(value = "stagingChannelPattern",
25+
required = true) String stagingChannelPattern,
26+
@JsonProperty(value = "overviewChannelPattern",
27+
required = true) String overviewChannelPattern,
28+
@JsonProperty(value = "categories", required = true) List<String> categories,
29+
@JsonProperty(value = "categoryRoleSuffix",
30+
required = true) String categoryRoleSuffix) {
31+
this.stagingChannelPattern = Objects.requireNonNull(stagingChannelPattern);
32+
this.overviewChannelPattern = Objects.requireNonNull(overviewChannelPattern);
33+
this.categories = new ArrayList<>(Objects.requireNonNull(categories));
34+
this.categoryRoleSuffix = Objects.requireNonNull(categoryRoleSuffix);
3135
}
3236

3337
/**

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
import java.util.Collections;
88
import java.util.HashSet;
9+
import java.util.Objects;
910
import java.util.Set;
1011

1112
/**
1213
* Configuration for the scam blocker system, see
1314
* {@link org.togetherjava.tjbot.commands.moderation.scam.ScamBlocker}.
1415
*/
15-
@SuppressWarnings("ClassCanBeRecord")
1616
@JsonRootName("scamBlocker")
1717
public final class ScamBlockerConfig {
1818
private final Mode mode;
@@ -23,17 +23,20 @@ public final class ScamBlockerConfig {
2323
private final int isHostSimilarToKeywordDistanceThreshold;
2424

2525
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
26-
private ScamBlockerConfig(@JsonProperty("mode") Mode mode,
27-
@JsonProperty("reportChannelPattern") String reportChannelPattern,
28-
@JsonProperty("hostWhitelist") Set<String> hostWhitelist,
29-
@JsonProperty("hostBlacklist") Set<String> hostBlacklist,
30-
@JsonProperty("suspiciousHostKeywords") Set<String> suspiciousHostKeywords,
31-
@JsonProperty("isHostSimilarToKeywordDistanceThreshold") int isHostSimilarToKeywordDistanceThreshold) {
32-
this.mode = mode;
33-
this.reportChannelPattern = reportChannelPattern;
34-
this.hostWhitelist = new HashSet<>(hostWhitelist);
35-
this.hostBlacklist = new HashSet<>(hostBlacklist);
36-
this.suspiciousHostKeywords = new HashSet<>(suspiciousHostKeywords);
26+
private ScamBlockerConfig(@JsonProperty(value = "mode", required = true) Mode mode,
27+
@JsonProperty(value = "reportChannelPattern",
28+
required = true) String reportChannelPattern,
29+
@JsonProperty(value = "hostWhitelist", required = true) Set<String> hostWhitelist,
30+
@JsonProperty(value = "hostBlacklist", required = true) Set<String> hostBlacklist,
31+
@JsonProperty(value = "suspiciousHostKeywords",
32+
required = true) Set<String> suspiciousHostKeywords,
33+
@JsonProperty(value = "isHostSimilarToKeywordDistanceThreshold",
34+
required = true) int isHostSimilarToKeywordDistanceThreshold) {
35+
this.mode = Objects.requireNonNull(mode);
36+
this.reportChannelPattern = Objects.requireNonNull(reportChannelPattern);
37+
this.hostWhitelist = new HashSet<>(Objects.requireNonNull(hostWhitelist));
38+
this.hostBlacklist = new HashSet<>(Objects.requireNonNull(hostBlacklist));
39+
this.suspiciousHostKeywords = new HashSet<>(Objects.requireNonNull(suspiciousHostKeywords));
3740
this.isHostSimilarToKeywordDistanceThreshold = isHostSimilarToKeywordDistanceThreshold;
3841
}
3942

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44
import com.fasterxml.jackson.annotation.JsonProperty;
55
import com.fasterxml.jackson.annotation.JsonRootName;
66

7+
import java.util.Objects;
8+
79
/**
810
* Configuration for the suggestion system, see
911
* {@link org.togetherjava.tjbot.commands.basic.SuggestionsUpDownVoter}.
1012
*/
11-
@SuppressWarnings("ClassCanBeRecord")
1213
@JsonRootName("suggestions")
1314
public final class SuggestionsConfig {
1415
private final String channelPattern;
1516
private final String upVoteEmoteName;
1617
private final String downVoteEmoteName;
1718

1819
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
19-
private SuggestionsConfig(@JsonProperty("channelPattern") String channelPattern,
20-
@JsonProperty("upVoteEmoteName") String upVoteEmoteName,
21-
@JsonProperty("downVoteEmoteName") String downVoteEmoteName) {
22-
this.channelPattern = channelPattern;
23-
this.upVoteEmoteName = upVoteEmoteName;
24-
this.downVoteEmoteName = downVoteEmoteName;
20+
private SuggestionsConfig(
21+
@JsonProperty(value = "channelPattern", required = true) String channelPattern,
22+
@JsonProperty(value = "upVoteEmoteName", required = true) String upVoteEmoteName,
23+
@JsonProperty(value = "downVoteEmoteName", required = true) String downVoteEmoteName) {
24+
this.channelPattern = Objects.requireNonNull(channelPattern);
25+
this.upVoteEmoteName = Objects.requireNonNull(upVoteEmoteName);
26+
this.downVoteEmoteName = Objects.requireNonNull(downVoteEmoteName);
2527
}
2628

2729
/**

0 commit comments

Comments
 (0)