[AB-166] refactored conditions to not use exceptions for their regular case, split up feature mode exception into condition and exception, if the conditions need to be checked somewhere else, a separate exception is required, fixed command not being disabled properly in channels

This commit is contained in:
Sheldan
2020-11-21 22:07:34 +01:00
parent 04f1db2408
commit 448d555dba
39 changed files with 259 additions and 208 deletions

View File

@@ -159,10 +159,7 @@ public class CommandReceivedHandler extends ListenerAdapter {
commandResult = self.executeCommand(foundCommand, commandContext);
}
} else {
// TODO can it be done nicer?
if(conditionResult.getException() != null) {
throw conditionResult.getException();
}
commandResult = CommandResult.fromCondition(conditionResult);
}
if(commandResult != null) {
self.executePostCommandListener(foundCommand, commandContext, commandResult);

View File

@@ -4,7 +4,10 @@ import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ResultState;
import dev.sheldan.abstracto.core.command.models.condition.GenericConditionModel;
import dev.sheldan.abstracto.core.command.service.PostCommandExecution;
import dev.sheldan.abstracto.core.models.GuildChannelMember;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -12,17 +15,29 @@ import org.springframework.stereotype.Service;
@Service
public class ConditionPostExecution implements PostCommandExecution {
public static final String WARN_REACTION_EMOTE = "warnReaction";
public static final String GENERIC_COMMAND_EXCEPTION_MODEL_KEY = "generic_condition_notification";
@Autowired
private MessageService messageService;
@Autowired
private ChannelService channelService;
@Override
public void execute(CommandContext commandContext, CommandResult commandResult, Command command) {
if(commandResult.getResult().equals(ResultState.CONDITION)) {
if(commandResult.getResult().equals(ResultState.CONDITION) && commandResult.getConditionResult() != null && !commandResult.getConditionResult().isResult() && commandResult.getConditionResult().getConditionDetail() != null) {
messageService.addReactionToMessage(WARN_REACTION_EMOTE, commandContext.getGuild().getIdLong(), commandContext.getMessage());
if(commandResult.getConditionResult() != null && commandResult.getConditionResult().getReason() != null){
commandContext.getChannel().sendMessage(commandResult.getConditionResult().getReason()).queue();
}
GenericConditionModel conditionModel = GenericConditionModel
.builder()
.conditionDetail(commandResult.getConditionResult().getConditionDetail())
.guildChannelMember(GuildChannelMember
.builder()
.guild(commandContext.getGuild())
.textChannel(commandContext.getChannel())
.member(commandContext.getAuthor())
.build())
.build();
channelService.sendEmbedTemplateInChannel(GENERIC_COMMAND_EXCEPTION_MODEL_KEY, conditionModel, commandContext.getChannel());
}
}
}

View File

@@ -23,10 +23,10 @@ public class ChannelGroupCommandServiceBean implements ChannelGroupCommandServic
List<AChannelGroupCommand> allChannelGroupsOfCommand = channelGroupCommandService.getAllGroupCommandsForCommand(command);
for (AChannelGroupCommand aChannelGroupCommand : allChannelGroupsOfCommand) {
Optional<AChannel> channelInGroup = aChannelGroupCommand.getGroup()
.getChannels().stream().filter(channel1 -> channel1.getId().equals(channel.getId())).findAny();
if (channelInGroup.isPresent() && aChannelGroupCommand.getEnabled()) {
log.trace("Command {} is enabled because the channel is part of group {} in server.", command.getName(), aChannelGroupCommand.getGroup().getId());
return true;
.getChannels().stream().filter(innerChannel -> innerChannel.getId().equals(channel.getId())).findAny();
if (channelInGroup.isPresent() && !aChannelGroupCommand.getEnabled()) {
log.trace("Command {} is disabled because the channel is part of group {} in server.", command.getName(), aChannelGroupCommand.getGroup().getId());
return false;
}
}
// empty -> no groups, command enabled

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.command.exception.IncorrectFeatureModeException;
import dev.sheldan.abstracto.core.command.service.management.FeatureManagementService;
import dev.sheldan.abstracto.core.config.FeatureConfig;
import dev.sheldan.abstracto.core.config.FeatureEnum;
@@ -14,10 +15,7 @@ import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@@ -91,6 +89,14 @@ public class FeatureModeServiceBean implements FeatureModeService {
return featureModeActive(featureEnum, server, mode);
}
@Override
public void validateActiveFeatureMode(Long serverId, FeatureEnum featureEnum, FeatureMode mode) {
boolean featureModeActive = featureModeActive(featureEnum, serverId, mode);
if(!featureModeActive) {
throw new IncorrectFeatureModeException(featureEnum, Arrays.asList(mode));
}
}
@Override
public FeatureMode getFeatureModeForKey(String key) {
return getAllAvailableFeatureModes().stream().filter(mode -> mode.getKey().equalsIgnoreCase(key)).findAny().orElseThrow(() -> new FeatureModeNotFoundException(key, getFeatureModesAsStrings()));