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()) {
if(conditionResult.isResult()) {
commandResult = self.executeCommand(foundCommand, commandContext);
} else {
commandResult = CommandResult.fromCondition(conditionResult);
} else {
commandResult = self.executeCommand(foundCommand, commandContext);
}
} else {
commandResult = self.executeCommand(foundCommand, commandContext);
}
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);

View File

@@ -3,10 +3,12 @@ package dev.sheldan.abstracto.core.command.config;
import dev.sheldan.abstracto.core.command.Command;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Builder
@Setter
@Getter
public class SingleLevelPackedModule {
private ModuleInterface moduleInterface;

View File

@@ -1,5 +1,8 @@
package dev.sheldan.abstracto.core.command.service;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.condition.ConditionResult;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.models.database.ACommand;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.models.database.ARole;
@@ -16,4 +19,5 @@ public interface CommandService {
void unRestrictCommand(ACommand aCommand, AServer server);
void disAllowCommandForRole(ACommand aCommand, ARole role);
void disAllowFeatureForRole(FeatureEnum featureEnum, ARole role);
ConditionResult isCommandExecutable(Command command, CommandContext commandContext);
}

View File

@@ -25,6 +25,18 @@ Feature key: `modmail`
Opening a mod mail thread for a user::
* Usage: `contact <member>`
* Description: Creates a new mod mail thread with the `member`. Does not send a notification about the new thread.
Adding a role to the roles responsible for managing mod mail threads::
* Usage: `setModMailRole <role>`
* Description: Adds this role to the roles responsible for mod mail threads, which means: this role will be pinged when a new thread is created and this role is automatically added to the roles allowed to execute all commands related to mod mail.
Removing a role from the roles responsible for managing mod mail threads::
* Usage: `removeModMailRole <role>`
* Description: Removes this role from the roles responsible for mod mail threads, which means: this role will no longer be pinged when a new thread is created and this role will also be removed from the roles allowed to execute all commands related to mod mail.
Changing the category in which the text channels are created::
* Usage: `setModMailCategory <categoryId>`
* Description: Sets the category which Abstracto uses to create the text channels containing the mod mail threads. The existing threads will not be migrated automatically.
NOTE: The following commands are only available within a mod mail thread.
Replying to a mod mail thread::
* Usage `reply [text]`
* Description: Sends `text` to the user if provided. `text` is optional, because its also possible to only send a picture.
@@ -46,15 +58,6 @@ Closing the mod mail thread without notifying the user::
* Usage: `closeSilently [note]`
* Description: Closes the thread, deletes the text channel containing the thread and logs the interactions between the member and the moderators in the `modmailLog` post target.
When closing a thread, a closing header with general information will be send and the note will be displayed there.
Adding a role to the roles responsible for managing mod mail threads::
* Usage: `setModMailRole <role>`
* Description: Adds this role to the roles responsible for mod mail threads, which means: this role will be pinged when a new thread is created and this role is automatically added to the roles allowed to execute all commands related to mod mail.
Removing a role from the roles responsible for managing mod mail threads::
* Usage: `removeModMailRole <role>`
* Description: Removes this role from the roles responsible for mod mail threads, which means: this role will no longer be pinged when a new thread is created and this role will also be removed from the roles allowed to execute all commands related to mod mail.
Changing the category in which the text channels are created::
* Usage: `setModMailCategory <categoryId>`
* Description: Sets the category which Abstracto uses to create the text channels containing the mod mail threads. The existing threads will not be migrated automatically.
=== Mod mail thread logging

View File

@@ -27,7 +27,7 @@ public class DurationMethod implements TemplateMethodModelEx {
* otherwise
* @param arguments The parameters passed to this method, should be only a single duration.
* @return The string representation of the {@link Duration} object
* @throws TemplateModelException
* @throws TemplateModelException In case the amount of parameters is not correct, or the first object was not a {@link Duration}
*/
@Override
public Object exec(List arguments) throws TemplateModelException {