add checks for some events to see if the feature is enabled and more cleanup

This commit is contained in:
Lee 2024-06-30 08:24:14 +01:00
parent 22d4558d84
commit 6403c57db5
9 changed files with 74 additions and 6 deletions

@ -4,10 +4,12 @@ import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.features.autorole.profile.AutoRoleProfile; import cc.fascinated.bat.features.autorole.profile.AutoRoleProfile;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.service.FeatureService;
import lombok.NonNull; import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
@ -19,8 +21,20 @@ import java.util.List;
@Component @Component
@Log4j2 @Log4j2
public class AutoRoleListener implements EventListener { public class AutoRoleListener implements EventListener {
private final FeatureService featureService;
@Autowired
public AutoRoleListener(@NonNull FeatureService featureService) {
this.featureService = featureService;
}
@Override @Override
public void onGuildMemberJoin(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberJoinEvent event) { 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); AutoRoleProfile profile = guild.getProfile(AutoRoleProfile.class);
if (profile.getRoles().isEmpty()) { if (profile.getRoles().isEmpty()) {
return; return;

@ -50,7 +50,7 @@ public class DisableSubCommand extends BatSubCommand {
return; return;
} }
Feature feature = FeatureService.INSTANCE.getFeature(featureName); Feature feature = FeatureService.INSTANCE.getFeature(featureName);
if (featureProfile.getFeatureState(feature) == FeatureProfile.FeatureState.DISABLED) { if (featureProfile.isFeatureDisabled(feature)) {
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();

@ -50,7 +50,7 @@ public class EnableSubCommand extends BatSubCommand {
return; return;
} }
Feature feature = FeatureService.INSTANCE.getFeature(featureName); Feature feature = FeatureService.INSTANCE.getFeature(featureName);
if (featureProfile.getFeatureState(feature) == FeatureProfile.FeatureState.ENABLED) { if (featureProfile.isFeatureEnabled(feature)) {
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();

@ -40,6 +40,24 @@ public class FeatureProfile extends Profile {
return this.featureStates.get(featureName); 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 * Enables the feature
* *

@ -4,6 +4,7 @@ import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.features.namehistory.profile.user.NameHistoryProfile; import cc.fascinated.bat.features.namehistory.profile.user.NameHistoryProfile;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.service.FeatureService;
import cc.fascinated.bat.service.GuildService; import cc.fascinated.bat.service.GuildService;
import cc.fascinated.bat.service.UserService; import cc.fascinated.bat.service.UserService;
import lombok.NonNull; import lombok.NonNull;
@ -19,11 +20,13 @@ import org.springframework.stereotype.Component;
public class NameHistoryListener implements EventListener { public class NameHistoryListener implements EventListener {
private final UserService userService; private final UserService userService;
private final GuildService guildService; private final GuildService guildService;
private final FeatureService featureService;
@Autowired @Autowired
public NameHistoryListener(@NonNull UserService userService, @NonNull GuildService guildService) { public NameHistoryListener(@NonNull UserService userService, @NonNull GuildService guildService, @NonNull FeatureService featureService) {
this.userService = userService; this.userService = userService;
this.guildService = guildService; this.guildService = guildService;
this.featureService = featureService;
} }
@Override @Override
@ -36,6 +39,11 @@ public class NameHistoryListener implements EventListener {
@Override @Override
public void onGuildMemberUpdateNickname(@NonNull BatGuild guild, @NonNull BatUser user, String oldName, String newName, public void onGuildMemberUpdateNickname(@NonNull BatGuild guild, @NonNull BatUser user, String oldName, String newName,
@NonNull GuildMemberUpdateNicknameEvent event) { @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(); cc.fascinated.bat.features.namehistory.profile.guild.NameHistoryProfile profile = guild.getNameHistoryProfile();
profile.addName(user, newName); profile.addName(user, newName);
guildService.saveGuild(guild); guildService.saveGuild(guild);

@ -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.ScoreSaberPlayerScoreToken;
import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken; import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken;
import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.DiscordService;
import cc.fascinated.bat.service.FeatureService;
import cc.fascinated.bat.service.GuildService; import cc.fascinated.bat.service.GuildService;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
@ -23,10 +25,12 @@ import org.springframework.stereotype.Component;
@Log4j2 @Log4j2
public class NumberOneScoreFeedListener implements EventListener { public class NumberOneScoreFeedListener implements EventListener {
private final GuildService guildService; private final GuildService guildService;
private final FeatureService featureService;
@Autowired @Autowired
public NumberOneScoreFeedListener(GuildService guildService) { public NumberOneScoreFeedListener(@NonNull GuildService guildService, @NonNull FeatureService featureService) {
this.guildService = guildService; this.guildService = guildService;
this.featureService = featureService;
} }
@Override @Override
@ -49,6 +53,11 @@ public class NumberOneScoreFeedListener implements EventListener {
if (batGuild == null) { if (batGuild == null) {
continue; 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); NumberOneScoreFeedProfile profile = batGuild.getProfile(NumberOneScoreFeedProfile.class);
if (profile == null || profile.getChannelId() == null) { if (profile == null || profile.getChannelId() == null) {
continue; continue;

@ -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.ScoreSaberPlayerScoreToken;
import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken; import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken;
import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.DiscordService;
import cc.fascinated.bat.service.FeatureService;
import cc.fascinated.bat.service.GuildService; import cc.fascinated.bat.service.GuildService;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
@ -22,10 +24,12 @@ import org.springframework.stereotype.Component;
@Log4j2 @Log4j2
public class UserScoreFeedListener implements EventListener { public class UserScoreFeedListener implements EventListener {
private final GuildService guildService; private final GuildService guildService;
private final FeatureService featureService;
@Autowired @Autowired
public UserScoreFeedListener(GuildService guildService) { public UserScoreFeedListener(@NonNull GuildService guildService, @NonNull FeatureService featureService) {
this.guildService = guildService; this.guildService = guildService;
this.featureService = featureService;
} }
@Override @Override
@ -36,6 +40,10 @@ public class UserScoreFeedListener implements EventListener {
if (batGuild == null) { if (batGuild == null) {
continue; 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); UserScoreFeedProfile profile = batGuild.getProfile(UserScoreFeedProfile.class);
if (profile == null || profile.getChannelId() == null || !profile.getTrackedUsers().contains(player.getId())) { if (profile == null || profile.getChannelId() == null || !profile.getTrackedUsers().contains(player.getId())) {
continue; continue;

@ -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.getFeatureState(command.getFeature()) == FeatureProfile.FeatureState.DISABLED) { if (featureProfile.isFeatureDisabled(command.getFeature())) {
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();

@ -70,6 +70,17 @@ public class FeatureService {
return features.get(name.toLowerCase()); return features.get(name.toLowerCase());
} }
/**
* Gets a feature by class
*
* @param clazz The class of the feature
* @return The feature
*/
public <T extends Feature> T getFeature(Class<T> 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 * Checks if a feature is registered
* *