From 4bf4a451467357282aa39e87db7a7215239398d5 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 30 Sep 2022 14:16:26 +0200 Subject: [PATCH 1/2] Ensure config is present and not null --- .../org/togetherjava/tjbot/config/Config.java | 74 ++++++++++--------- .../tjbot/config/HelpSystemConfig.java | 22 +++--- .../tjbot/config/ScamBlockerConfig.java | 27 ++++--- .../tjbot/config/SuggestionsConfig.java | 16 ++-- 4 files changed, 78 insertions(+), 61 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index 1b3eb5d634..7d59d09d97 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -9,6 +9,7 @@ import java.nio.file.Path; import java.util.Collections; import java.util.List; +import java.util.Objects; /** * Configuration of the application. Create instances using {@link #load(Path)}. @@ -36,39 +37,46 @@ public final class Config { @SuppressWarnings("ConstructorWithTooManyParameters") @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) private Config(@JsonProperty("token") String token, - @JsonProperty("gistApiKey") String gistApiKey, - @JsonProperty("databasePath") String databasePath, - @JsonProperty("projectWebsite") String projectWebsite, - @JsonProperty("discordGuildInvite") String discordGuildInvite, - @JsonProperty("modAuditLogChannelPattern") String modAuditLogChannelPattern, - @JsonProperty("mutedRolePattern") String mutedRolePattern, - @JsonProperty("heavyModerationRolePattern") String heavyModerationRolePattern, - @JsonProperty("softModerationRolePattern") String softModerationRolePattern, - @JsonProperty("tagManageRolePattern") String tagManageRolePattern, - @JsonProperty("suggestions") SuggestionsConfig suggestions, - @JsonProperty("quarantinedRolePattern") String quarantinedRolePattern, - @JsonProperty("scamBlocker") ScamBlockerConfig scamBlocker, - @JsonProperty("wolframAlphaAppId") String wolframAlphaAppId, - @JsonProperty("helpSystem") HelpSystemConfig helpSystem, - @JsonProperty("mediaOnlyChannelPattern") String mediaOnlyChannelPattern, - @JsonProperty("blacklistedFileExtension") List blacklistedFileExtension) { - this.token = token; - this.gistApiKey = gistApiKey; - this.databasePath = databasePath; - this.projectWebsite = projectWebsite; - this.discordGuildInvite = discordGuildInvite; - this.modAuditLogChannelPattern = modAuditLogChannelPattern; - this.mutedRolePattern = mutedRolePattern; - this.heavyModerationRolePattern = heavyModerationRolePattern; - this.softModerationRolePattern = softModerationRolePattern; - this.tagManageRolePattern = tagManageRolePattern; - this.suggestions = suggestions; - this.quarantinedRolePattern = quarantinedRolePattern; - this.scamBlocker = scamBlocker; - this.wolframAlphaAppId = wolframAlphaAppId; - this.helpSystem = helpSystem; - this.mediaOnlyChannelPattern = mediaOnlyChannelPattern; - this.blacklistedFileExtension = blacklistedFileExtension; + @JsonProperty(value = "gistApiKey", required = true) String gistApiKey, + @JsonProperty(value = "databasePath", required = true) String databasePath, + @JsonProperty(value = "projectWebsite", required = true) String projectWebsite, + @JsonProperty(value = "discordGuildInvite", required = true) String discordGuildInvite, + @JsonProperty(value = "modAuditLogChannelPattern", + required = true) String modAuditLogChannelPattern, + @JsonProperty(value = "mutedRolePattern", required = true) String mutedRolePattern, + @JsonProperty(value = "heavyModerationRolePattern", + required = true) String heavyModerationRolePattern, + @JsonProperty(value = "softModerationRolePattern", + required = true) String softModerationRolePattern, + @JsonProperty(value = "tagManageRolePattern", + required = true) String tagManageRolePattern, + @JsonProperty(value = "suggestions", required = true) SuggestionsConfig suggestions, + @JsonProperty(value = "quarantinedRolePattern", + required = true) String quarantinedRolePattern, + @JsonProperty(value = "scamBlocker", required = true) ScamBlockerConfig scamBlocker, + @JsonProperty(value = "wolframAlphaAppId", required = true) String wolframAlphaAppId, + @JsonProperty(value = "helpSystem", required = true) HelpSystemConfig helpSystem, + @JsonProperty(value = "mediaOnlyChannelPattern", + required = true) String mediaOnlyChannelPattern, + @JsonProperty(value = "blacklistedFileExtension", + required = true) List blacklistedFileExtension) { + this.token = Objects.requireNonNull(token); + this.gistApiKey = Objects.requireNonNull(gistApiKey); + this.databasePath = Objects.requireNonNull(databasePath); + this.projectWebsite = Objects.requireNonNull(projectWebsite); + this.discordGuildInvite = Objects.requireNonNull(discordGuildInvite); + this.modAuditLogChannelPattern = Objects.requireNonNull(modAuditLogChannelPattern); + this.mutedRolePattern = Objects.requireNonNull(mutedRolePattern); + this.heavyModerationRolePattern = Objects.requireNonNull(heavyModerationRolePattern); + this.softModerationRolePattern = Objects.requireNonNull(softModerationRolePattern); + this.tagManageRolePattern = Objects.requireNonNull(tagManageRolePattern); + this.suggestions = Objects.requireNonNull(suggestions); + this.quarantinedRolePattern = Objects.requireNonNull(quarantinedRolePattern); + this.scamBlocker = Objects.requireNonNull(scamBlocker); + this.wolframAlphaAppId = Objects.requireNonNull(wolframAlphaAppId); + this.helpSystem = Objects.requireNonNull(helpSystem); + this.mediaOnlyChannelPattern = Objects.requireNonNull(mediaOnlyChannelPattern); + this.blacklistedFileExtension = Objects.requireNonNull(blacklistedFileExtension); } /** diff --git a/application/src/main/java/org/togetherjava/tjbot/config/HelpSystemConfig.java b/application/src/main/java/org/togetherjava/tjbot/config/HelpSystemConfig.java index 4d001adfbc..e95c4ec25f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/HelpSystemConfig.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/HelpSystemConfig.java @@ -7,11 +7,11 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Objects; /** * Configuration for the help system, see {@link org.togetherjava.tjbot.commands.help.AskCommand}. */ -@SuppressWarnings("ClassCanBeRecord") @JsonRootName("helpSystem") public final class HelpSystemConfig { private final String stagingChannelPattern; @@ -20,14 +20,18 @@ public final class HelpSystemConfig { private final String categoryRoleSuffix; @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private HelpSystemConfig(@JsonProperty("stagingChannelPattern") String stagingChannelPattern, - @JsonProperty("overviewChannelPattern") String overviewChannelPattern, - @JsonProperty("categories") List categories, - @JsonProperty("categoryRoleSuffix") String categoryRoleSuffix) { - this.stagingChannelPattern = stagingChannelPattern; - this.overviewChannelPattern = overviewChannelPattern; - this.categories = new ArrayList<>(categories); - this.categoryRoleSuffix = categoryRoleSuffix; + private HelpSystemConfig( + @JsonProperty(value = "stagingChannelPattern", + required = true) String stagingChannelPattern, + @JsonProperty(value = "overviewChannelPattern", + required = true) String overviewChannelPattern, + @JsonProperty(value = "categories", required = true) List categories, + @JsonProperty(value = "categoryRoleSuffix", + required = true) String categoryRoleSuffix) { + this.stagingChannelPattern = Objects.requireNonNull(stagingChannelPattern); + this.overviewChannelPattern = Objects.requireNonNull(overviewChannelPattern); + this.categories = new ArrayList<>(Objects.requireNonNull(categories)); + this.categoryRoleSuffix = Objects.requireNonNull(categoryRoleSuffix); } /** diff --git a/application/src/main/java/org/togetherjava/tjbot/config/ScamBlockerConfig.java b/application/src/main/java/org/togetherjava/tjbot/config/ScamBlockerConfig.java index 2de5d29d1a..3881600250 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/ScamBlockerConfig.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/ScamBlockerConfig.java @@ -6,13 +6,13 @@ import java.util.Collections; import java.util.HashSet; +import java.util.Objects; import java.util.Set; /** * Configuration for the scam blocker system, see * {@link org.togetherjava.tjbot.commands.moderation.scam.ScamBlocker}. */ -@SuppressWarnings("ClassCanBeRecord") @JsonRootName("scamBlocker") public final class ScamBlockerConfig { private final Mode mode; @@ -23,17 +23,20 @@ public final class ScamBlockerConfig { private final int isHostSimilarToKeywordDistanceThreshold; @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private ScamBlockerConfig(@JsonProperty("mode") Mode mode, - @JsonProperty("reportChannelPattern") String reportChannelPattern, - @JsonProperty("hostWhitelist") Set hostWhitelist, - @JsonProperty("hostBlacklist") Set hostBlacklist, - @JsonProperty("suspiciousHostKeywords") Set suspiciousHostKeywords, - @JsonProperty("isHostSimilarToKeywordDistanceThreshold") int isHostSimilarToKeywordDistanceThreshold) { - this.mode = mode; - this.reportChannelPattern = reportChannelPattern; - this.hostWhitelist = new HashSet<>(hostWhitelist); - this.hostBlacklist = new HashSet<>(hostBlacklist); - this.suspiciousHostKeywords = new HashSet<>(suspiciousHostKeywords); + private ScamBlockerConfig(@JsonProperty(value = "mode", required = true) Mode mode, + @JsonProperty(value = "reportChannelPattern", + required = true) String reportChannelPattern, + @JsonProperty(value = "hostWhitelist", required = true) Set hostWhitelist, + @JsonProperty(value = "hostBlacklist", required = true) Set hostBlacklist, + @JsonProperty(value = "suspiciousHostKeywords", + required = true) Set suspiciousHostKeywords, + @JsonProperty(value = "isHostSimilarToKeywordDistanceThreshold", + required = true) int isHostSimilarToKeywordDistanceThreshold) { + this.mode = Objects.requireNonNull(mode); + this.reportChannelPattern = Objects.requireNonNull(reportChannelPattern); + this.hostWhitelist = new HashSet<>(Objects.requireNonNull(hostWhitelist)); + this.hostBlacklist = new HashSet<>(Objects.requireNonNull(hostBlacklist)); + this.suspiciousHostKeywords = new HashSet<>(Objects.requireNonNull(suspiciousHostKeywords)); this.isHostSimilarToKeywordDistanceThreshold = isHostSimilarToKeywordDistanceThreshold; } diff --git a/application/src/main/java/org/togetherjava/tjbot/config/SuggestionsConfig.java b/application/src/main/java/org/togetherjava/tjbot/config/SuggestionsConfig.java index 01f18beb70..d64b671fa0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/SuggestionsConfig.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/SuggestionsConfig.java @@ -4,11 +4,12 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonRootName; +import java.util.Objects; + /** * Configuration for the suggestion system, see * {@link org.togetherjava.tjbot.commands.basic.SuggestionsUpDownVoter}. */ -@SuppressWarnings("ClassCanBeRecord") @JsonRootName("suggestions") public final class SuggestionsConfig { private final String channelPattern; @@ -16,12 +17,13 @@ public final class SuggestionsConfig { private final String downVoteEmoteName; @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private SuggestionsConfig(@JsonProperty("channelPattern") String channelPattern, - @JsonProperty("upVoteEmoteName") String upVoteEmoteName, - @JsonProperty("downVoteEmoteName") String downVoteEmoteName) { - this.channelPattern = channelPattern; - this.upVoteEmoteName = upVoteEmoteName; - this.downVoteEmoteName = downVoteEmoteName; + private SuggestionsConfig( + @JsonProperty(value = "channelPattern", required = true) String channelPattern, + @JsonProperty(value = "upVoteEmoteName", required = true) String upVoteEmoteName, + @JsonProperty(value = "downVoteEmoteName", required = true) String downVoteEmoteName) { + this.channelPattern = Objects.requireNonNull(channelPattern); + this.upVoteEmoteName = Objects.requireNonNull(upVoteEmoteName); + this.downVoteEmoteName = Objects.requireNonNull(downVoteEmoteName); } /** From 470e99504fc4621f75aecb6d215116603e22add0 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Tue, 4 Oct 2022 10:36:21 +0200 Subject: [PATCH 2/2] token is also required --- .../src/main/java/org/togetherjava/tjbot/config/Config.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index 7d59d09d97..b2ac4062b9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -36,7 +36,7 @@ public final class Config { @SuppressWarnings("ConstructorWithTooManyParameters") @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - private Config(@JsonProperty("token") String token, + private Config(@JsonProperty(value = "token", required = true) String token, @JsonProperty(value = "gistApiKey", required = true) String gistApiKey, @JsonProperty(value = "databasePath", required = true) String databasePath, @JsonProperty(value = "projectWebsite", required = true) String projectWebsite,