From 6403c57db56274637a5e7e3857924b17a1de332d Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 30 Jun 2024 08:24:14 +0100 Subject: [PATCH] add checks for some events to see if the feature is enabled and more cleanup --- .../features/autorole/AutoRoleListener.java | 14 ++++++++++++++ .../server/feature/DisableSubCommand.java | 2 +- .../server/feature/EnableSubCommand.java | 2 +- .../features/base/profile/FeatureProfile.java | 18 ++++++++++++++++++ .../namehistory/NameHistoryListener.java | 10 +++++++++- .../scoresaber/NumberOneScoreFeedListener.java | 11 ++++++++++- .../scoresaber/UserScoreFeedListener.java | 10 +++++++++- .../fascinated/bat/service/CommandService.java | 2 +- .../fascinated/bat/service/FeatureService.java | 11 +++++++++++ 9 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/features/autorole/AutoRoleListener.java b/src/main/java/cc/fascinated/bat/features/autorole/AutoRoleListener.java index e6cc5a6..ee501c1 100644 --- a/src/main/java/cc/fascinated/bat/features/autorole/AutoRoleListener.java +++ b/src/main/java/cc/fascinated/bat/features/autorole/AutoRoleListener.java @@ -4,10 +4,12 @@ import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.features.autorole.profile.AutoRoleProfile; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; +import cc.fascinated.bat.service.FeatureService; import lombok.NonNull; import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; @@ -19,8 +21,20 @@ import java.util.List; @Component @Log4j2 public class AutoRoleListener implements EventListener { + private final FeatureService featureService; + + @Autowired + public AutoRoleListener(@NonNull FeatureService featureService) { + this.featureService = featureService; + } + @Override public void onGuildMemberJoin(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberJoinEvent event) { + AutoRoleFeature autoRoleFeature = featureService.getFeature(AutoRoleFeature.class); + if (!guild.getFeatureProfile().isFeatureEnabled(autoRoleFeature)) { // Check if the feature is enabled + return; + } + AutoRoleProfile profile = guild.getProfile(AutoRoleProfile.class); if (profile.getRoles().isEmpty()) { return; diff --git a/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/DisableSubCommand.java b/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/DisableSubCommand.java index ed13b5e..8aa5248 100644 --- a/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/DisableSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/DisableSubCommand.java @@ -50,7 +50,7 @@ public class DisableSubCommand extends BatSubCommand { return; } Feature feature = FeatureService.INSTANCE.getFeature(featureName); - if (featureProfile.getFeatureState(feature) == FeatureProfile.FeatureState.DISABLED) { + if (featureProfile.isFeatureDisabled(feature)) { interaction.replyEmbeds(EmbedUtils.errorEmbed() .setDescription("The feature `%s` is already enabled".formatted(feature.getName())) .build()).queue(); diff --git a/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/EnableSubCommand.java b/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/EnableSubCommand.java index 50b4d44..def7690 100644 --- a/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/EnableSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/base/commands/server/feature/EnableSubCommand.java @@ -50,7 +50,7 @@ public class EnableSubCommand extends BatSubCommand { return; } Feature feature = FeatureService.INSTANCE.getFeature(featureName); - if (featureProfile.getFeatureState(feature) == FeatureProfile.FeatureState.ENABLED) { + if (featureProfile.isFeatureEnabled(feature)) { interaction.replyEmbeds(EmbedUtils.errorEmbed() .setDescription("The feature `%s` is already enabled".formatted(feature.getName())) .build()).queue(); diff --git a/src/main/java/cc/fascinated/bat/features/base/profile/FeatureProfile.java b/src/main/java/cc/fascinated/bat/features/base/profile/FeatureProfile.java index 00bfb51..8bbad5f 100644 --- a/src/main/java/cc/fascinated/bat/features/base/profile/FeatureProfile.java +++ b/src/main/java/cc/fascinated/bat/features/base/profile/FeatureProfile.java @@ -40,6 +40,24 @@ public class FeatureProfile extends Profile { return this.featureStates.get(featureName); } + /** + * Gets whether the feature is enabled + * + * @return the feature state + */ + public boolean isFeatureEnabled(Feature feature) { + return this.getFeatureState(feature) == FeatureState.ENABLED; + } + + /** + * Gets whether the feature is disabled + * + * @return the feature state + */ + public boolean isFeatureDisabled(Feature feature) { + return this.getFeatureState(feature) == FeatureState.DISABLED; + } + /** * Enables the feature * diff --git a/src/main/java/cc/fascinated/bat/features/namehistory/NameHistoryListener.java b/src/main/java/cc/fascinated/bat/features/namehistory/NameHistoryListener.java index 2bbe3b1..d7a66da 100644 --- a/src/main/java/cc/fascinated/bat/features/namehistory/NameHistoryListener.java +++ b/src/main/java/cc/fascinated/bat/features/namehistory/NameHistoryListener.java @@ -4,6 +4,7 @@ import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.features.namehistory.profile.user.NameHistoryProfile; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; +import cc.fascinated.bat.service.FeatureService; import cc.fascinated.bat.service.GuildService; import cc.fascinated.bat.service.UserService; import lombok.NonNull; @@ -19,11 +20,13 @@ import org.springframework.stereotype.Component; public class NameHistoryListener implements EventListener { private final UserService userService; private final GuildService guildService; + private final FeatureService featureService; @Autowired - public NameHistoryListener(@NonNull UserService userService, @NonNull GuildService guildService) { + public NameHistoryListener(@NonNull UserService userService, @NonNull GuildService guildService, @NonNull FeatureService featureService) { this.userService = userService; this.guildService = guildService; + this.featureService = featureService; } @Override @@ -36,6 +39,11 @@ public class NameHistoryListener implements EventListener { @Override public void onGuildMemberUpdateNickname(@NonNull BatGuild guild, @NonNull BatUser user, String oldName, String newName, @NonNull GuildMemberUpdateNicknameEvent event) { + NameHistoryFeature nameHistoryFeature = featureService.getFeature(NameHistoryFeature.class); + if (!guild.getFeatureProfile().isFeatureEnabled(nameHistoryFeature)) { // Check if the feature is enabled + return; + } + cc.fascinated.bat.features.namehistory.profile.guild.NameHistoryProfile profile = guild.getNameHistoryProfile(); profile.addName(user, newName); guildService.saveGuild(guild); diff --git a/src/main/java/cc/fascinated/bat/features/scoresaber/NumberOneScoreFeedListener.java b/src/main/java/cc/fascinated/bat/features/scoresaber/NumberOneScoreFeedListener.java index a19ff7f..7bb16a3 100644 --- a/src/main/java/cc/fascinated/bat/features/scoresaber/NumberOneScoreFeedListener.java +++ b/src/main/java/cc/fascinated/bat/features/scoresaber/NumberOneScoreFeedListener.java @@ -8,7 +8,9 @@ import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberLeaderboardT import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberPlayerScoreToken; import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken; import cc.fascinated.bat.service.DiscordService; +import cc.fascinated.bat.service.FeatureService; import cc.fascinated.bat.service.GuildService; +import lombok.NonNull; import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; @@ -23,10 +25,12 @@ import org.springframework.stereotype.Component; @Log4j2 public class NumberOneScoreFeedListener implements EventListener { private final GuildService guildService; + private final FeatureService featureService; @Autowired - public NumberOneScoreFeedListener(GuildService guildService) { + public NumberOneScoreFeedListener(@NonNull GuildService guildService, @NonNull FeatureService featureService) { this.guildService = guildService; + this.featureService = featureService; } @Override @@ -49,6 +53,11 @@ public class NumberOneScoreFeedListener implements EventListener { if (batGuild == null) { continue; } + ScoreSaberFeature scoreSaberFeature = featureService.getFeature(ScoreSaberFeature.class); + if (!batGuild.getFeatureProfile().isFeatureEnabled(scoreSaberFeature)) { // Check if the feature is enabled + return; + } + NumberOneScoreFeedProfile profile = batGuild.getProfile(NumberOneScoreFeedProfile.class); if (profile == null || profile.getChannelId() == null) { continue; diff --git a/src/main/java/cc/fascinated/bat/features/scoresaber/UserScoreFeedListener.java b/src/main/java/cc/fascinated/bat/features/scoresaber/UserScoreFeedListener.java index 793976e..826c352 100644 --- a/src/main/java/cc/fascinated/bat/features/scoresaber/UserScoreFeedListener.java +++ b/src/main/java/cc/fascinated/bat/features/scoresaber/UserScoreFeedListener.java @@ -7,7 +7,9 @@ import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberLeaderboardT import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberPlayerScoreToken; import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken; import cc.fascinated.bat.service.DiscordService; +import cc.fascinated.bat.service.FeatureService; import cc.fascinated.bat.service.GuildService; +import lombok.NonNull; import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; @@ -22,10 +24,12 @@ import org.springframework.stereotype.Component; @Log4j2 public class UserScoreFeedListener implements EventListener { private final GuildService guildService; + private final FeatureService featureService; @Autowired - public UserScoreFeedListener(GuildService guildService) { + public UserScoreFeedListener(@NonNull GuildService guildService, @NonNull FeatureService featureService) { this.guildService = guildService; + this.featureService = featureService; } @Override @@ -36,6 +40,10 @@ public class UserScoreFeedListener implements EventListener { if (batGuild == null) { continue; } + ScoreSaberFeature scoreSaberFeature = featureService.getFeature(ScoreSaberFeature.class); + if (!batGuild.getFeatureProfile().isFeatureEnabled(scoreSaberFeature)) { // Check if the feature is enabled + return; + } UserScoreFeedProfile profile = batGuild.getProfile(UserScoreFeedProfile.class); if (profile == null || profile.getChannelId() == null || !profile.getTrackedUsers().contains(player.getId())) { continue; diff --git a/src/main/java/cc/fascinated/bat/service/CommandService.java b/src/main/java/cc/fascinated/bat/service/CommandService.java index ec054ae..1371da8 100644 --- a/src/main/java/cc/fascinated/bat/service/CommandService.java +++ b/src/main/java/cc/fascinated/bat/service/CommandService.java @@ -222,7 +222,7 @@ public class CommandService extends ListenerAdapter { if (guild != null) { FeatureProfile featureProfile = guild.getFeatureProfile(); - if (featureProfile.getFeatureState(command.getFeature()) == FeatureProfile.FeatureState.DISABLED) { + if (featureProfile.isFeatureDisabled(command.getFeature())) { event.replyEmbeds(EmbedUtils.errorEmbed() .setDescription("The feature `%s` is disabled in this guild".formatted(command.getFeature().getName())) .build()).setEphemeral(true).queue(); diff --git a/src/main/java/cc/fascinated/bat/service/FeatureService.java b/src/main/java/cc/fascinated/bat/service/FeatureService.java index bef3baa..b690cbf 100644 --- a/src/main/java/cc/fascinated/bat/service/FeatureService.java +++ b/src/main/java/cc/fascinated/bat/service/FeatureService.java @@ -70,6 +70,17 @@ public class FeatureService { return features.get(name.toLowerCase()); } + /** + * Gets a feature by class + * + * @param clazz The class of the feature + * @return The feature + */ + public T getFeature(Class clazz) { + Feature feature = features.values().stream().filter(featureClazz -> featureClazz.getClass().equals(clazz)).findFirst().orElse(null); + return clazz.cast(feature); + } + /** * Checks if a feature is registered *