[AB-76] replaced usage templates for commands with a generated usage string, removed possibility to define the usage directly on the help

added Google guava dependency
minor fixes in documentation
fixed versions in main pom
This commit is contained in:
Sheldan
2020-09-24 02:40:44 +02:00
parent 76adda90a3
commit 5081c3174f
14 changed files with 358 additions and 14 deletions

View File

@@ -1,9 +1,11 @@
package dev.sheldan.abstracto.core.command.service;
import com.google.common.collect.Iterables;
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.config.CommandConfiguration;
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;
@@ -27,6 +29,8 @@ import java.util.List;
public class CommandServiceBean implements CommandService {
public static final String NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE = "no_feature_command_found_exception";
private static final String[] MANDATORY_ENCLOSING = {"<", ">"};
private static final String[] OPTIONAL_ENCLOSING = {"[", "]"};
@Autowired
private ModuleManagementService moduleManagementService;
@@ -91,6 +95,26 @@ public class CommandServiceBean implements CommandService {
commandForServer.setRestricted(true);
}
@Override
public String generateUsage(Command command) {
StringBuilder builder = new StringBuilder();
CommandConfiguration commandConfig = command.getConfiguration();
builder.append(commandConfig.getName());
if(!commandConfig.getParameters().isEmpty()) {
builder.append(" ");
}
commandConfig.getParameters().forEach(parameter -> {
String[] enclosing = parameter.isOptional() ? OPTIONAL_ENCLOSING : MANDATORY_ENCLOSING;
builder.append(enclosing[0]);
builder.append(parameter.getName());
builder.append(enclosing[1]);
if(!parameter.equals(Iterables.getLast(commandConfig.getParameters()))) {
builder.append(" ");
}
});
return builder.toString();
}
@Override
public void unRestrictCommand(ACommand aCommand, AServer server) {
ACommandInAServer commandForServer = commandInServerManagementService.getCommandForServer(aCommand, server);

View File

@@ -94,6 +94,7 @@ public class Help implements Command {
model.setAllowedRoles(roleService.getRolesFromGuild(aCommandInAServer.getAllowedRoles()));
model.setRestricted(true);
}
model.setUsage(commandService.generateUsage(command));
model.setCommand(command.getConfiguration());
MessageToSend messageToSend = templateService.renderEmbedTemplate("help_command_details_response", model);
channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel());

View File

@@ -0,0 +1,95 @@
package dev.sheldan.abstracto.core.command.service;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.Parameter;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class CommandServiceBeanTest {
private static final String COMMAND_NAME = "test";
private static final String PARAMETER_1_NAME = "param1";
private static final String PARAMETER_2_NAME = "param2";
@InjectMocks
private CommandServiceBean commandServiceBean;
@Test
public void testUsageWithoutParameters() {
executeTest("test", commandWithConfig(getNoParameters()));
}
@Test
public void testUsageWithOptionalParameter() {
executeTest("test [param1]", commandWithConfig(getOptionalParameterConfig()));
}
@Test
public void testUsageWithMandatoryParameter() {
executeTest("test <param1>", commandWithConfig(getMandatoryParameterConfig()));
}
@Test
public void testUsageWithMixedParameters() {
executeTest("test <param1> [param2]", commandWithConfig(getMixedParameterConfig()));
}
private void executeTest(String expectedUsage, Command commandToExecute) {
String generatedUsage = commandServiceBean.generateUsage(commandToExecute);
Assert.assertEquals(expectedUsage, generatedUsage);
}
private Command commandWithConfig(CommandConfiguration commandConfiguration) {
Command command = Mockito.mock(Command.class);
when(command.getConfiguration()).thenReturn(commandConfiguration);
return command;
}
private CommandConfiguration getNoParameters() {
return CommandConfiguration
.builder()
.name(COMMAND_NAME)
.build();
}
private CommandConfiguration getOptionalParameterConfig() {
return CommandConfiguration
.builder()
.name(COMMAND_NAME)
.parameters(Arrays.asList(getOptionalParameter(true)))
.build();
}
private CommandConfiguration getMandatoryParameterConfig() {
return CommandConfiguration
.builder()
.name(COMMAND_NAME)
.parameters(Arrays.asList(getOptionalParameter(false)))
.build();
}
private CommandConfiguration getMixedParameterConfig() {
Parameter param1 = Parameter.builder().name(PARAMETER_1_NAME).optional(false).build();
Parameter param2 = Parameter.builder().name(PARAMETER_2_NAME).optional(true).build();
return CommandConfiguration
.builder()
.name(COMMAND_NAME)
.parameters(Arrays.asList(param1, param2))
.build();
}
private Parameter getOptionalParameter(boolean b) {
return Parameter.builder().name(PARAMETER_1_NAME).optional(b).build();
}
}

View File

@@ -6,7 +6,6 @@ import lombok.Getter;
@Getter
@Builder
public class HelpInfo {
private String usage;
private String longHelp;
private String example;
@Builder.Default

View File

@@ -16,6 +16,7 @@ public interface CommandService {
void makeRoleImmuneForCommand(ACommand aCommand, ARole role);
void makeRoleAffectedByCommand(ACommand aCommand, ARole role);
void restrictCommand(ACommand aCommand, AServer server);
String generateUsage(Command command);
void unRestrictCommand(ACommand aCommand, AServer server);
void disAllowCommandForRole(ACommand aCommand, ARole role);
void disAllowFeatureForRole(FeatureEnum featureEnum, ARole role);

View File

@@ -14,6 +14,7 @@ import java.util.List;
@SuperBuilder
public class HelpCommandDetailsModel extends UserInitiatedServerContext {
private CommandConfiguration command;
private String usage;
private List<Role> allowedRoles;
private List<Role> immuneRoles;
private Boolean restricted;

View File

@@ -17,8 +17,6 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

View File

@@ -66,6 +66,11 @@
<groupId>dev.sheldan.abstracto.scheduling</groupId>
<artifactId>scheduling-impl</artifactId>
</dependency>
<dependency>
<groupId>dev.sheldan.abstracto.core</groupId>
<artifactId>core-impl</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -33,7 +33,7 @@ Echo::
* Description: Echos `text` in the same channel this command as executed in.
Changing the prefix::
* Usage: `setPrefix <prefix>`
* Description: Changes the prefix of the bot on this value to `prefix`. This can be one or multiple characters.
* Description: Changes the prefix of the bot in this guild to `prefix`. This can be one or multiple characters.
Changing a post target::
* Usage: `posttarget <key> <channel>`
* Description: Changes the given post target identified by `key` to the given channel. All messages using this post target will be send to this channel from now on.

View File

@@ -26,7 +26,7 @@ Weeks in this case are just a short hand for 7 days.
Pagination navigation:: If a pagination is used for the output of a command you can navigate the pages with `◀️` and `▶️` and `⏹️` to close the pagination.
Role as parameter:: Whenever a role is a parameter for a command, this can be done by either providing the role ID or mentioning the role.
Channel groups:: This concept enables you to group channels together into channel groups and enact certain restrictions or features on this whole group.
System configuration:: Some properties can be configured while the bot is running.
System configuration:: Some properties can be configured while the bot is running and can be changed for each guild respectively.
In the respective features they are noted under `Relevant system configuration`.
In order to change this you need to use the command `setConfig` with the provided key and the new desired value.
Emotes:: The features have section of the keys of used emotes in the feature, you can change this emote with the `setEmote` command.

View File

@@ -58,6 +58,14 @@
<jruby.version>9.2.11.1</jruby.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<guava.version>29.0-jre</guava.version>
<gson.version>2.8.6</gson.version>
<commons-lang3.version>3.9</commons-lang3.version>
<commons-io.version>2.6</commons-io.version>
<mockito-core.version>3.3.3</mockito-core.version>
<commons-collections.version>4.3</commons-collections.version>
<spring-boot-starter.version>2.3.1.RELEASE</spring-boot-starter.version>
<junit.version>4.13</junit.version>
</properties>
<build>
@@ -119,43 +127,49 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.3</version>
<version>${commons-collections.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
<version>3.3.3</version>
<version>${mockito-core.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.2.6.RELEASE</version>
<version>${spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
@@ -172,6 +186,11 @@
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>

View File

@@ -31,11 +31,9 @@ public class CommandConfigValidator {
Assert.assertNotNull(helpInfo);
if(helpInfo.isTemplated()) {
Assert.assertNull(helpInfo.getLongHelp());
Assert.assertNull(helpInfo.getUsage());
Assert.assertNull(helpInfo.getExample());
} else {
Assert.assertNotNull(helpInfo.getLongHelp());
Assert.assertNotNull(helpInfo.getUsage());
if(helpInfo.isHasExample()) {
Assert.assertNotNull(helpInfo.getExample());
} else {