[AB-325] adding async command conditions - as this is required for some conditions

adding user parameter to immune user condition evaluation
This commit is contained in:
Sheldan
2021-09-04 13:47:03 +02:00
parent 69abff77fb
commit d7f889971d
8 changed files with 188 additions and 60 deletions

View File

@@ -149,38 +149,44 @@ public class CommandReceivedHandler extends ListenerAdapter {
.userInitiatedContext(userInitiatedContext);
validateCommandParameters(parsedParameters, foundCommand);
CommandContext commandContext = commandContextBuilder.parameters(parsedParameters).build();
ConditionResult conditionResult = commandService.isCommandExecutable(foundCommand, commandContext);
CommandResult commandResult = null;
if(conditionResult.isResult()) {
if(foundCommand.getConfiguration().isAsync()) {
log.info("Executing async command {} for server {} in channel {} based on message {} by user {}.",
foundCommand.getConfiguration().getName(), commandContext.getGuild().getId(), commandContext.getChannel().getId(), commandContext.getMessage().getId(), commandContext.getAuthor().getId());
CompletableFuture<ConditionResult> conditionResultFuture = commandService.isCommandExecutable(foundCommand, commandContext);
conditionResultFuture.thenAccept(conditionResult -> {
CommandResult commandResult = null;
if(conditionResult.isResult()) {
if(foundCommand.getConfiguration().isAsync()) {
log.info("Executing async command {} for server {} in channel {} based on message {} by user {}.",
foundCommand.getConfiguration().getName(), commandContext.getGuild().getId(), commandContext.getChannel().getId(), commandContext.getMessage().getId(), commandContext.getAuthor().getId());
self.executeAsyncCommand(foundCommand, commandContext).exceptionally(throwable -> {
log.error("Asynchronous command {} failed.", foundCommand.getConfiguration().getName(), throwable);
UserInitiatedServerContext rebuildUserContext = buildTemplateParameter(event);
CommandContext rebuildContext = CommandContext.builder()
.author(event.getMember())
.guild(event.getGuild())
.channel(event.getTextChannel())
.message(event.getMessage())
.jda(event.getJDA())
.undoActions(commandContext.getUndoActions()) // TODO really do this? it would need to guarantee that its available and usable
.userInitiatedContext(rebuildUserContext)
.parameters(parsedParameters).build();
CommandResult failedResult = CommandResult.fromError(throwable.getMessage(), throwable);
self.executePostCommandListener(foundCommand, rebuildContext, failedResult);
return null;
});
self.executeAsyncCommand(foundCommand, commandContext)
.exceptionally(throwable -> failedCommandHandling(event, foundCommand, parsedParameters, commandContext, throwable));
} else {
commandResult = self.executeCommand(foundCommand, commandContext);
}
} else {
commandResult = self.executeCommand(foundCommand, commandContext);
commandResult = CommandResult.fromCondition(conditionResult);
}
} else {
commandResult = CommandResult.fromCondition(conditionResult);
}
if(commandResult != null) {
self.executePostCommandListener(foundCommand, commandContext, commandResult);
}
if(commandResult != null) {
self.executePostCommandListener(foundCommand, commandContext, commandResult);
}
}).exceptionally(throwable -> failedCommandHandling(event, foundCommand, parsedParameters, commandContext, throwable));
}
private Void failedCommandHandling(MessageReceivedEvent event, Command foundCommand, Parameters parsedParameters, CommandContext commandContext, Throwable throwable) {
log.error("Asynchronous command {} failed.", foundCommand.getConfiguration().getName(), throwable);
UserInitiatedServerContext rebuildUserContext = buildTemplateParameter(event);
CommandContext rebuildContext = CommandContext.builder()
.author(event.getMember())
.guild(event.getGuild())
.channel(event.getTextChannel())
.message(event.getMessage())
.jda(event.getJDA())
.undoActions(commandContext.getUndoActions()) // TODO really do this? it would need to guarantee that its available and usable
.userInitiatedContext(rebuildUserContext)
.parameters(parsedParameters).build();
CommandResult failedResult = CommandResult.fromError(throwable.getMessage(), throwable);
self.executePostCommandListener(foundCommand, rebuildContext, failedResult);
return null;
}
@Transactional(isolation = Isolation.SERIALIZABLE)

View File

@@ -22,12 +22,14 @@ import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.models.database.AFeature;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.utils.CompletableFutureList;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@@ -142,12 +144,12 @@ public class CommandServiceBean implements CommandService {
log.info("Disallowing feature {} for role {} in server {}.", feature.getKey(), role.getId(), role.getServer().getId());
}
public ConditionResult isCommandExecutable(Command command, CommandContext commandContext) {
public CompletableFuture<ConditionResult> isCommandExecutable(Command command, CommandContext commandContext) {
if(command instanceof ConditionalCommand) {
ConditionalCommand castedCommand = (ConditionalCommand) command;
return checkConditions(commandContext, command, castedCommand.getConditions());
} else {
return ConditionResult.builder().result(true).build();
return ConditionResult.fromAsyncSuccess();
}
}
@@ -179,16 +181,39 @@ public class CommandServiceBean implements CommandService {
.build();
}
private ConditionResult checkConditions(CommandContext commandContext, Command command, List<CommandCondition> conditions) {
if(conditions != null) {
private CompletableFuture<ConditionResult> checkConditions(CommandContext commandContext, Command command, List<CommandCondition> conditions) {
if(conditions != null && !conditions.isEmpty()) {
List<CompletableFuture<ConditionResult>> futures = new ArrayList<>();
for (CommandCondition condition : conditions) {
ConditionResult conditionResult = condition.shouldExecute(commandContext, command);
if(!conditionResult.isResult()) {
return conditionResult;
if(condition.isAsync()) {
futures.add(condition.shouldExecuteAsync(commandContext, command));
} else {
futures.add(CompletableFuture.completedFuture(condition.shouldExecute(commandContext, command)));
}
}
CompletableFuture<ConditionResult> resultFuture = new CompletableFuture<>();
CompletableFutureList<ConditionResult> futureList = new CompletableFutureList<>(futures);
futureList.getMainFuture().whenComplete((unused, throwable) -> {
List<ConditionResult> results = futureList.getObjects();
boolean foundResult = false;
for (ConditionResult conditionResult : results) {
if (!conditionResult.isResult()) {
foundResult = true;
resultFuture.complete(conditionResult);
break;
}
}
if(!foundResult) {
resultFuture.complete(ConditionResult.fromSuccess());
}
}).exceptionally(throwable -> {
resultFuture.completeExceptionally(throwable);
return null;
});
return resultFuture;
} else {
return ConditionResult.fromAsyncSuccess();
}
return ConditionResult.builder().result(true).build();
}

View File

@@ -1,6 +1,7 @@
package dev.sheldan.abstracto.core.commands.help;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.condition.ConditionResult;
import dev.sheldan.abstracto.core.command.config.*;
import dev.sheldan.abstracto.core.command.config.features.CoreFeatureDefinition;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
@@ -21,6 +22,7 @@ import dev.sheldan.abstracto.core.models.template.commands.help.HelpModuleDetail
import dev.sheldan.abstracto.core.models.template.commands.help.HelpModuleOverviewModel;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.RoleService;
import dev.sheldan.abstracto.core.utils.CompletableFutureList;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
@@ -114,14 +116,25 @@ public class Help implements Command {
ModuleDefinition moduleDefinition = moduleService.getModuleByName(parameter);
log.debug("Displaying help for module {}.", moduleDefinition.getInfo().getName());
SingleLevelPackedModule module = moduleService.getPackedModule(moduleDefinition);
List<Command> commands = module.getCommands();
List<Command> filteredCommands = new ArrayList<>();
commands.forEach(command -> {
if(commandService.isCommandExecutable(command, commandContext).isResult()) {
filteredCommands.add(command);
}
List<Command> filteredCommand = new ArrayList<>();
List<CompletableFuture<ConditionResult>> conditionFutures = new ArrayList<>();
Map<CompletableFuture<ConditionResult>, Command> futureCommandMap = new HashMap<>();
module.getCommands().forEach(command -> {
// TODO dont provide the parameters, else the condition uses the wrong parameters, as we are not actually executing the command
CompletableFuture<ConditionResult> future = commandService.isCommandExecutable(command, commandContext);
conditionFutures.add(future);
futureCommandMap.put(future, command);
});
module.setCommands(filteredCommands);
CompletableFutureList<ConditionResult> conditionFuturesList = new CompletableFutureList<>(conditionFutures);
conditionFuturesList.getMainFuture().thenAccept(unused -> conditionFutures.forEach(conditionResultCompletableFuture -> {
if(!conditionResultCompletableFuture.isCompletedExceptionally()) {
ConditionResult result = conditionResultCompletableFuture.join();
if(result.isResult()) {
filteredCommand.add(futureCommandMap.get(conditionResultCompletableFuture));
}
}
}));
module.setCommands(filteredCommand);
List<ModuleDefinition> subModules = moduleService.getSubModules(moduleDefinition);
HelpModuleDetailsModel model = (HelpModuleDetailsModel) ContextConverter.fromCommandContext(commandContext, HelpModuleDetailsModel.class);
model.setModule(module);

View File

@@ -62,7 +62,7 @@ public class RoleImmunityServiceBean implements RoleImmunityService {
if(immuneRoles.isEmpty()) {
return Optional.empty();
}
return immuneRoles
return immuneRoles
.stream()
.filter(role -> member
.getRoles()