added evaluation of the allowed commands when executing help

added a note that a few commands are only available within a mod mail thread
This commit is contained in:
Sheldan
2020-05-12 00:45:13 +02:00
parent c234266b7b
commit 59c449f4d3
7 changed files with 72 additions and 33 deletions

View File

@@ -1,13 +1,12 @@
package dev.sheldan.abstracto.core.command;
import dev.sheldan.abstracto.core.command.condition.CommandCondition;
import dev.sheldan.abstracto.core.command.condition.ConditionResult;
import dev.sheldan.abstracto.core.command.condition.ConditionalCommand;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.Parameters;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameter;
import dev.sheldan.abstracto.core.command.exception.ParameterTooLong;
import dev.sheldan.abstracto.core.command.service.CommandManager;
import dev.sheldan.abstracto.core.command.service.CommandService;
import dev.sheldan.abstracto.core.command.service.PostCommandExecution;
import dev.sheldan.abstracto.core.command.execution.*;
import dev.sheldan.abstracto.core.command.execution.UnParsedCommandParameter;
@@ -63,6 +62,9 @@ public class CommandReceivedHandler extends ListenerAdapter {
@Autowired
private RoleManagementService roleManagementService;
@Autowired
private CommandService commandService;
@Override
@Transactional
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
@@ -89,17 +91,12 @@ public class CommandReceivedHandler extends ListenerAdapter {
foundCommand = commandManager.findCommandByParameters(commandName, unparsedParameter);
Parameters parsedParameters = getParsedParameters(unparsedParameter, foundCommand, event.getMessage(), userInitiatedContext);
CommandContext commandContext = commandContextBuilder.parameters(parsedParameters).build();
ConditionResult conditionResult = commandService.isCommandExecutable(foundCommand, commandContext);
CommandResult commandResult;
if(foundCommand instanceof ConditionalCommand) {
ConditionalCommand castedCommand = (ConditionalCommand) foundCommand;
ConditionResult conditionResult = checkConditions(commandContext, foundCommand, castedCommand.getConditions());
if(!conditionResult.isResult()) {
commandResult = CommandResult.fromCondition(conditionResult);
} else {
commandResult = self.executeCommand(foundCommand, commandContext);
}
} else {
if(conditionResult.isResult()) {
commandResult = self.executeCommand(foundCommand, commandContext);
} else {
commandResult = CommandResult.fromCondition(conditionResult);
}
for (PostCommandExecution postCommandExecution : executions) {
postCommandExecution.execute(commandContext, commandResult, foundCommand);
@@ -118,18 +115,6 @@ public class CommandReceivedHandler extends ListenerAdapter {
return foundCommand.execute(commandContext);
}
public ConditionResult checkConditions(CommandContext commandContext, Command command, List<CommandCondition> conditions) {
if(conditions != null) {
for (CommandCondition condition : conditions) {
ConditionResult conditionResult = condition.shouldExecute(commandContext, command);
if(!conditionResult.isResult()) {
return conditionResult;
}
}
}
return ConditionResult.builder().result(true).build();
}
private UserInitiatedServerContext buildTemplateParameter(MessageReceivedEvent event) {
AChannel channel = channelManagementService.loadChannel(event.getChannel().getIdLong());
AServer server = serverManagementService.loadOrCreate(event.getGuild().getIdLong());

View File

@@ -1,5 +1,10 @@
package dev.sheldan.abstracto.core.command.service;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.condition.CommandCondition;
import dev.sheldan.abstracto.core.command.condition.ConditionResult;
import dev.sheldan.abstracto.core.command.condition.ConditionalCommand;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.models.database.ACommand;
import dev.sheldan.abstracto.core.command.models.database.ACommandInAServer;
import dev.sheldan.abstracto.core.command.models.database.AModule;
@@ -15,6 +20,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Slf4j
public class CommandServiceBean implements CommandService {
@@ -105,5 +112,26 @@ public class CommandServiceBean implements CommandService {
});
}
public 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();
}
}
private ConditionResult checkConditions(CommandContext commandContext, Command command, List<CommandCondition> conditions) {
if(conditions != null) {
for (CommandCondition condition : conditions) {
ConditionResult conditionResult = condition.shouldExecute(commandContext, command);
if(!conditionResult.isResult()) {
return conditionResult;
}
}
}
return ConditionResult.builder().result(true).build();
}
}

View File

@@ -1,11 +1,16 @@
package dev.sheldan.abstracto.core.commands.help;
import dev.sheldan.abstracto.core.command.*;
import dev.sheldan.abstracto.core.command.condition.CommandCondition;
import dev.sheldan.abstracto.core.command.condition.ConditionResult;
import dev.sheldan.abstracto.core.command.condition.ConditionalCommand;
import dev.sheldan.abstracto.core.command.config.*;
import dev.sheldan.abstracto.core.command.execution.*;
import dev.sheldan.abstracto.core.command.models.database.ACommand;
import dev.sheldan.abstracto.core.command.models.database.ACommandInAServer;
import dev.sheldan.abstracto.core.command.service.CommandRegistry;
import dev.sheldan.abstracto.core.command.service.CommandService;
import dev.sheldan.abstracto.core.command.service.CommandServiceBean;
import dev.sheldan.abstracto.core.command.service.ModuleRegistry;
import dev.sheldan.abstracto.core.command.service.management.CommandInServerManagementService;
import dev.sheldan.abstracto.core.command.service.management.CommandManagementService;
@@ -21,6 +26,7 @@ import dev.sheldan.abstracto.templating.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -50,6 +56,9 @@ public class Help implements Command {
@Autowired
private RoleService roleService;
@Autowired
private CommandService commandService;
@Override
public CommandResult execute(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
@@ -65,6 +74,14 @@ public class Help implements Command {
if(moduleService.moduleExists(parameter)){
ModuleInterface moduleInterface = moduleService.getModuleByName(parameter);
SingleLevelPackedModule module = moduleService.getPackedModule(moduleInterface);
List<Command> commands = module.getCommands();
List<Command> filteredCommands = new ArrayList<>();
commands.forEach(command -> {
if(commandService.isCommandExecutable(command, commandContext).isResult()) {
filteredCommands.add(command);
}
});
module.setCommands(filteredCommands);
List<ModuleInterface> subModules = moduleService.getSubModules(moduleInterface);
HelpModuleDetailsModel model = (HelpModuleDetailsModel) ContextConverter.fromCommandContext(commandContext, HelpModuleDetailsModel.class);
model.setModule(module);