add feature toggling

This commit is contained in:
Lee
2024-06-30 05:15:37 +01:00
parent 93350f1506
commit ee6456e4d8
14 changed files with 315 additions and 11 deletions

View File

@ -0,0 +1,58 @@
package cc.fascinated.bat.features.command;
import cc.fascinated.bat.command.BatSubCommand;
import cc.fascinated.bat.command.CommandInfo;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.features.Feature;
import cc.fascinated.bat.features.FeatureProfile;
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 lombok.NonNull;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author Fascinated (fascinated7)
*/
@Component("feature:enable.sub")
@CommandInfo(name = "enable", description = "Enables a feature")
public class EnableSubCommand extends BatSubCommand {
private final GuildService guildService;
@Autowired
public EnableSubCommand(@NonNull GuildService guildService) {
this.guildService = guildService;
super.addOption(OptionType.STRING, "feature", "The feature to enable", true);
}
@Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
FeatureProfile featureProfile = guild.getFeatureProfile();
OptionMapping featureOption = interaction.getOption("feature");
if (featureOption == null) {
interaction.replyEmbeds(EmbedUtils.errorEmbed().setDescription("You must provide a feature to enable").build()).queue();
return;
}
String featureName = featureOption.getAsString();
if (!FeatureService.INSTANCE.isFeature(featureName)) {
interaction.replyEmbeds(EmbedUtils.errorEmbed().setDescription("That feature does not exist").build()).queue();
return;
}
Feature feature = FeatureService.INSTANCE.getFeature(featureName);
if (!featureProfile.isFeatureDisabled(feature)) {
interaction.replyEmbeds(EmbedUtils.errorEmbed().setDescription("That feature is already enabled").build()).queue();
return;
}
featureProfile.setFeatureState(feature, false);
guildService.saveGuild(guild);
interaction.replyEmbeds(EmbedUtils.successEmbed().setDescription("Successfully enabled the feature " + feature.getName()).build()).queue();
}
}