use an enum for feature states

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -13,6 +13,7 @@ import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -78,4 +79,13 @@ public class FeatureService {
public boolean isFeature(@NonNull String name) {
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();
}
}