[AB-205] making exception message for command not found configurable

making feature config classes more unified in the naming
This commit is contained in:
Sheldan
2021-03-21 22:59:01 +01:00
parent 5eefc3909e
commit f53f0cb66c
44 changed files with 176 additions and 129 deletions

View File

@@ -1,6 +1,7 @@
package dev.sheldan.abstracto.core.command.post;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.features.CoreFeatureConfig;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ResultState;
@@ -14,7 +15,6 @@ 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
@@ -26,7 +26,7 @@ public class ConditionPostExecution implements PostCommandExecution {
@Override
public void execute(CommandContext commandContext, CommandResult commandResult, Command command) {
if(commandResult.getResult().equals(ResultState.CONDITION) && commandResult.getConditionResult() != null && !commandResult.getConditionResult().isResult() && commandResult.getConditionResult().getConditionDetail() != null) {
reactionService.addReactionToMessage(WARN_REACTION_EMOTE, commandContext.getGuild().getIdLong(), commandContext.getMessage());
reactionService.addReactionToMessage(CoreFeatureConfig.WARN_REACTION_KEY, commandContext.getGuild().getIdLong(), commandContext.getMessage());
GenericConditionModel conditionModel = GenericConditionModel
.builder()
.conditionDetail(commandResult.getConditionResult().getConditionDetail())

View File

@@ -1,12 +1,16 @@
package dev.sheldan.abstracto.core.command.post;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.features.CoreFeatureConfig;
import dev.sheldan.abstracto.core.command.exception.CommandNotFoundException;
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.service.ExceptionService;
import dev.sheldan.abstracto.core.command.service.PostCommandExecution;
import dev.sheldan.abstracto.core.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -17,12 +21,21 @@ public class ExceptionPostExecution implements PostCommandExecution {
@Autowired
private ExceptionService exceptionService;
@Autowired
private ConfigService configService;
@Override
public void execute(CommandContext commandContext, CommandResult commandResult, Command command) {
ResultState result = commandResult.getResult();
if(result.equals(ResultState.ERROR)) {
Throwable throwable = commandResult.getThrowable();
if(throwable != null) {
if(throwable instanceof CommandNotFoundException){
String configValue = configService.getStringValueOrConfigDefault(CoreFeatureConfig.NO_COMMAND_REPORTING_CONFIG_KEY, commandContext.getGuild().getIdLong());
if(!BooleanUtils.toBoolean(configValue)) {
return;
}
}
log.info("Exception handling for exception {}.", throwable.getClass().getSimpleName());
exceptionService.reportExceptionToContext(throwable, commandContext, command);
}

View File

@@ -1,36 +1,47 @@
package dev.sheldan.abstracto.core.command.post;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.features.CoreFeatureConfig;
import dev.sheldan.abstracto.core.command.exception.CommandNotFoundException;
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.service.PostCommandExecution;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.ConfigService;
import dev.sheldan.abstracto.core.service.ReactionService;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ReactionPostExecution implements PostCommandExecution {
public static final String WARN_REACTION_EMOTE = "warnReaction";
public static final String SUCCESS_REACTION_EMOTE = "successReaction";
@Autowired
private ReactionService reactionService;
@Autowired
private ChannelService channelService;
@Autowired
private ConfigService configService;
@Override
public void execute(CommandContext commandContext, CommandResult commandResult, Command command) {
ResultState result = commandResult.getResult();
if(result.equals(ResultState.ERROR) || result.equals(ResultState.REPORTED_ERROR)) {
reactionService.addReactionToMessage(WARN_REACTION_EMOTE, commandContext.getGuild().getIdLong(), commandContext.getMessage());
if(commandResult.getThrowable() instanceof CommandNotFoundException){
String configValue = configService.getStringValueOrConfigDefault(CoreFeatureConfig.NO_COMMAND_REPORTING_CONFIG_KEY, commandContext.getGuild().getIdLong());
if(!BooleanUtils.toBoolean(configValue)) {
return;
}
}
reactionService.addReactionToMessage(CoreFeatureConfig.WARN_REACTION_KEY, commandContext.getGuild().getIdLong(), commandContext.getMessage());
if(commandResult.getMessage() != null && commandResult.getThrowable() == null){
channelService.sendTextToChannel(commandResult.getMessage(), commandContext.getChannel());
}
} else if(result.equals(ResultState.SUCCESSFUL) && command.getConfiguration().isCausesReaction()) {
reactionService.addReactionToMessage(SUCCESS_REACTION_EMOTE, commandContext.getGuild().getIdLong(), commandContext.getMessage());
reactionService.addReactionToMessage(CoreFeatureConfig.SUCCESS_REACTION_KEY, commandContext.getGuild().getIdLong(), commandContext.getMessage());
}
}

View File

@@ -46,7 +46,7 @@ public class ConfigServiceBean implements ConfigService {
}
@Override
public Double getDoubleValueOrConfigDefault(String name, Long serverId, Double defaultValue) {
public Double getDoubleValueOrConfigDefault(String name, Long serverId) {
return getDoubleValue(name, serverId, defaultConfigManagementService.getDefaultConfig(name).getDoubleValue());
}
@@ -60,7 +60,7 @@ public class ConfigServiceBean implements ConfigService {
}
@Override
public String getStringValueOrConfigDefault(String name, Long serverId, String defaultValue) {
public String getStringValueOrConfigDefault(String name, Long serverId) {
return getStringValue(name, serverId, defaultConfigManagementService.getDefaultConfig(name).getStringValue());
}

View File

@@ -24,6 +24,9 @@ public class ListenerServiceBean implements ListenerService {
@Autowired
private FeatureModeService featureModeService;
@Autowired
private ListenerServiceBean self;
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public <T extends FeatureAwareListenerModel, R extends ListenerExecutionResult> void executeFeatureAwareListener(FeatureAwareListener<T, R> listener, T model) {
@@ -52,7 +55,7 @@ public class ListenerServiceBean implements ListenerService {
return;
}
try {
CompletableFuture.runAsync(() -> listener.execute(model), executor).exceptionally(throwable -> {
CompletableFuture.runAsync(() -> self.executeFeatureListenerInTransaction(listener, model), executor).exceptionally(throwable -> {
log.error("Feature aware async Listener {} failed with async exception:", listener.getClass().getName(), throwable);
return null;
});
@@ -61,6 +64,11 @@ public class ListenerServiceBean implements ListenerService {
}
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public <T extends FeatureAwareListenerModel, R extends ListenerExecutionResult> void executeFeatureListenerInTransaction(FeatureAwareListener<T, R> listener, T model) {
listener.execute(model);
}
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public <T extends ListenerModel, R extends ListenerExecutionResult> void executeListener(AbstractoListener<T, R> listener, T model) {
@@ -72,10 +80,9 @@ public class ListenerServiceBean implements ListenerService {
}
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public <T extends ListenerModel, R extends ListenerExecutionResult> void executeListener(AbstractoListener<T, R> listener, T model, TaskExecutor executor) {
try {
CompletableFuture.runAsync(() -> listener.execute(model), executor).exceptionally(throwable -> {
CompletableFuture.runAsync(() -> self.executeListenerInTransaction(listener, model), executor).exceptionally(throwable -> {
log.error("Async Listener {} failed with async exception:", listener.getClass().getName(), throwable);
return null;
});
@@ -84,4 +91,9 @@ public class ListenerServiceBean implements ListenerService {
}
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public <T extends ListenerModel, R extends ListenerExecutionResult> void executeListenerInTransaction(AbstractoListener<T, R> listener, T model) {
listener.execute(model);
}
}

View File

@@ -3,15 +3,15 @@ abstracto.startup.synchronize=true
abstracto.eventWaiter.threads=10
server.port=8080
abstracto.allowedmention.everyone=false
abstracto.allowedmention.role=true
abstracto.allowedmention.user=true
abstracto.systemConfigs.prefix.name=prefix
abstracto.systemConfigs.prefix.stringValue=!
abstracto.systemConfigs.noCommandFoundReporting.name=noCommandFoundReporting
abstracto.systemConfigs.noCommandFoundReporting.stringValue=true
abstracto.featureFlags.core.featureName=core
abstracto.featureFlags.core.enabled=true

View File

@@ -1,22 +0,0 @@
package dev.sheldan.abstracto.core.command.config.features;
import dev.sheldan.abstracto.core.config.FeatureConfig;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
public class CoreFeature implements FeatureConfig {
@Override
public FeatureDefinition getFeature() {
return CoreFeatureDefinition.CORE_FEATURE;
}
@Override
public List<String> getRequiredEmotes() {
return Arrays.asList("warnReaction", "successReaction");
}
}

View File

@@ -0,0 +1,31 @@
package dev.sheldan.abstracto.core.command.config.features;
import dev.sheldan.abstracto.core.config.FeatureConfig;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
public class CoreFeatureConfig implements FeatureConfig {
public static final String NO_COMMAND_REPORTING_CONFIG_KEY = "noCommandFoundReporting";
public static final String SUCCESS_REACTION_KEY = "successReaction";
public static final String WARN_REACTION_KEY = "warnReaction";
@Override
public FeatureDefinition getFeature() {
return CoreFeatureDefinition.CORE_FEATURE;
}
@Override
public List<String> getRequiredEmotes() {
return Arrays.asList(WARN_REACTION_KEY, SUCCESS_REACTION_KEY);
}
@Override
public List<String> getRequiredSystemConfigKeys() {
return Arrays.asList(NO_COMMAND_REPORTING_CONFIG_KEY);
}
}

View File

@@ -7,9 +7,9 @@ public interface ConfigService {
Long getLongValue(String name, Long serverId);
Long getLongValueOrConfigDefault(String name, Long serverId);
Double getDoubleValue(String name, Long serverId, Double defaultValue);
Double getDoubleValueOrConfigDefault(String name, Long serverId, Double defaultValue);
Double getDoubleValueOrConfigDefault(String name, Long serverId);
String getStringValue(String name, Long serverId, String defaultValue);
String getStringValueOrConfigDefault(String name, Long serverId, String defaultValue);
String getStringValueOrConfigDefault(String name, Long serverId);
Long getLongValue(String name, Long serverId, Long defaultValue);
AConfig setOrCreateConfigValue(Long serverId, String name, AConfig value);
void setDoubleValue(String name, Long serverId, Double value);