use an enum for feature states
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 38s

This commit is contained in:
Lee 2024-06-30 08:10:49 +01:00
parent ea546f02ca
commit 22d4558d84
6 changed files with 53 additions and 27 deletions

@ -38,7 +38,7 @@ public class DisableSubCommand extends BatSubCommand {
OptionMapping featureOption = interaction.getOption("feature"); OptionMapping featureOption = interaction.getOption("feature");
if (featureOption == null) { if (featureOption == null) {
interaction.replyEmbeds(EmbedUtils.errorEmbed() interaction.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("You must provide a feature to disable") .setDescription("You must provide a feature to enabled")
.build()).queue(); .build()).queue();
return; return;
} }
@ -50,24 +50,17 @@ public class DisableSubCommand extends BatSubCommand {
return; return;
} }
Feature feature = FeatureService.INSTANCE.getFeature(featureName); Feature feature = FeatureService.INSTANCE.getFeature(featureName);
if (!feature.isCanBeDisabled()) { if (featureProfile.getFeatureState(feature) == FeatureProfile.FeatureState.DISABLED) {
interaction.replyEmbeds(EmbedUtils.errorEmbed() interaction.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("The feature `%s` cannot be disabled".formatted(feature.getName())) .setDescription("The feature `%s` is already enabled".formatted(feature.getName()))
.build()).queue(); .build()).queue();
return; return;
} }
if (featureProfile.isFeatureDisabled(feature)) { featureProfile.disableFeature(feature);
interaction.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("The feature `%s` is already disabled".formatted(feature.getName()))
.build()).queue();
return;
}
featureProfile.setFeatureState(feature, true);
guildService.saveGuild(guild); guildService.saveGuild(guild);
interaction.replyEmbeds(EmbedUtils.successEmbed() interaction.replyEmbeds(EmbedUtils.successEmbed()
.setDescription("Successfully disabled the `%s` feature".formatted(feature.getName())) .setDescription("Successfully enabled the `%s` feature".formatted(feature.getName()))
.build()).queue(); .build()).queue();
} }
} }

@ -50,14 +50,14 @@ public class EnableSubCommand extends BatSubCommand {
return; return;
} }
Feature feature = FeatureService.INSTANCE.getFeature(featureName); Feature feature = FeatureService.INSTANCE.getFeature(featureName);
if (!featureProfile.isFeatureDisabled(feature)) { if (featureProfile.getFeatureState(feature) == FeatureProfile.FeatureState.ENABLED) {
interaction.replyEmbeds(EmbedUtils.errorEmbed() interaction.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("The feature `%s` is already enabled".formatted(feature.getName())) .setDescription("The feature `%s` is already enabled".formatted(feature.getName()))
.build()).queue(); .build()).queue();
return; return;
} }
featureProfile.setFeatureState(feature, false); featureProfile.enableFeature(feature);
guildService.saveGuild(guild); guildService.saveGuild(guild);
interaction.replyEmbeds(EmbedUtils.successEmbed() interaction.replyEmbeds(EmbedUtils.successEmbed()
.setDescription("Successfully enabled the `%s` feature".formatted(feature.getName())) .setDescription("Successfully enabled the `%s` feature".formatted(feature.getName()))

@ -23,15 +23,13 @@ public class ListSubCommand extends BatSubCommand {
@Override @Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
StringBuilder featureStates = new StringBuilder(); StringBuilder featureStates = new StringBuilder();
for (Feature feature : FeatureService.INSTANCE.getFeatures().values()) { for (Feature feature : FeatureService.INSTANCE.getFeaturesSorted()) {
FeatureProfile featureProfile = guild.getFeatureProfile(); FeatureProfile featureProfile = guild.getFeatureProfile();
if (featureProfile.isFeatureDisabled(feature)) { featureStates.append("%s `%s`\n".formatted(
featureStates.append("").append(feature.getName()).append("\n"); featureProfile.getFeatureState(feature) == FeatureProfile.FeatureState.ENABLED ? "" : "",
} else { feature.getName()
featureStates.append("").append(feature.getName()).append("\n"); ));
} }
}
interaction.replyEmbeds(EmbedUtils.genericEmbed() interaction.replyEmbeds(EmbedUtils.genericEmbed()
.setTitle("Feature List") .setTitle("Feature List")
.setDescription(featureStates.toString()) .setDescription(featureStates.toString())

@ -10,10 +10,12 @@ import java.util.Map;
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
public class FeatureProfile extends Profile { public class FeatureProfile extends Profile {
private static final FeatureState DEFAULT_STATE = FeatureState.ENABLED;
/** /**
* The feature states * The feature states
*/ */
private Map<String, Boolean> featureStates; private Map<String, FeatureState> featureStates;
public FeatureProfile() { public FeatureProfile() {
super("feature"); super("feature");
@ -24,27 +26,45 @@ public class FeatureProfile extends Profile {
* *
* @return the feature states * @return the feature states
*/ */
public boolean isFeatureDisabled(Feature feature) { public FeatureState getFeatureState(Feature feature) {
if (this.featureStates == null) { if (this.featureStates == null) {
this.featureStates = new HashMap<>(); this.featureStates = new HashMap<>();
} }
if (feature == null) { if (feature == null) {
return false; return DEFAULT_STATE;
} }
String featureName = feature.getName().toUpperCase(); String featureName = feature.getName().toUpperCase();
if (!this.featureStates.containsKey(featureName)) { if (!this.featureStates.containsKey(featureName)) {
this.featureStates.put(featureName, false); this.featureStates.put(featureName, DEFAULT_STATE);
} }
return this.featureStates.get(featureName); return this.featureStates.get(featureName);
} }
/**
* Enables the feature
*
* @param feature the feature to enable
*/
public void enableFeature(Feature feature) {
this.setFeatureState(feature, FeatureState.ENABLED);
}
/**
* Disables the feature
*
* @param feature the feature to disable
*/
public void disableFeature(Feature feature) {
this.setFeatureState(feature, FeatureState.DISABLED);
}
/** /**
* Sets the feature state * Sets the feature state
* *
* @param feature the feature to set the state for * @param feature the feature to set the state for
* @param state the state to set * @param state the state to set
*/ */
public void setFeatureState(Feature feature, boolean state) { public void setFeatureState(Feature feature, FeatureState state) {
if (this.featureStates == null) { if (this.featureStates == null) {
this.featureStates = new HashMap<>(); this.featureStates = new HashMap<>();
} }
@ -55,4 +75,9 @@ public class FeatureProfile extends Profile {
public void reset() { public void reset() {
this.featureStates = null; this.featureStates = null;
} }
public enum FeatureState {
ENABLED,
DISABLED
}
} }

@ -222,7 +222,7 @@ public class CommandService extends ListenerAdapter {
if (guild != null) { if (guild != null) {
FeatureProfile featureProfile = guild.getFeatureProfile(); FeatureProfile featureProfile = guild.getFeatureProfile();
if (featureProfile.isFeatureDisabled(command.getFeature())) { if (featureProfile.getFeatureState(command.getFeature()) == FeatureProfile.FeatureState.DISABLED) {
event.replyEmbeds(EmbedUtils.errorEmbed() event.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("The feature `%s` is disabled in this guild".formatted(command.getFeature().getName())) .setDescription("The feature `%s` is disabled in this guild".formatted(command.getFeature().getName()))
.build()).setEphemeral(true).queue(); .build()).setEphemeral(true).queue();

@ -13,6 +13,7 @@ import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -78,4 +79,13 @@ public class FeatureService {
public boolean isFeature(@NonNull String name) { public boolean isFeature(@NonNull String name) {
return features.containsKey(name.toLowerCase()); return features.containsKey(name.toLowerCase());
} }
/**
* Gets the features sorted by name and status
*
* @return The features sorted
*/
public List<Feature> getFeaturesSorted() {
return features.values().stream().sorted((feature1, feature2) -> feature1.getName().compareToIgnoreCase(feature2.getName())).toList();
}
} }