added initial version containing ping/echo command and first draft of the command handling structure

This commit is contained in:
Sheldan
2019-12-09 23:50:24 +01:00
parent 78a4d814ad
commit 42cfe33b3a
41 changed files with 1023 additions and 1 deletions

View File

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>dev.sheldan.abstracto.command</groupId>
<artifactId>command</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>command-base</artifactId>
<packaging>pom</packaging>
<modules>
<module>utility</module>
<module>support</module>
</modules>
<dependencies>
<dependency>
<groupId>dev.sheldan.abstracto.command</groupId>
<artifactId>command-int</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<configuration>
<proc>only</proc>
<annotationProcessors>
<annotationProcessor>dev.sheldan.abstracto.command.annotations.CommandSpecProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>command-base</artifactId>
<groupId>dev.sheldan.abstracto.command</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>support</artifactId>
</project>

View File

@@ -0,0 +1,38 @@
package dev.sheldan.abstracto.command.support;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.execution.Configuration;
import dev.sheldan.abstracto.command.execution.Context;
import dev.sheldan.abstracto.command.execution.Result;
import dev.sheldan.abstracto.command.meta.CommandRegistry;
import net.dv8tion.jda.api.EmbedBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Help implements Command {
@Autowired
private CommandRegistry registry;
@Override
public Result execute(Context context) {
EmbedBuilder builder = new EmbedBuilder();
registry.getAllCommands().forEach(command -> {
builder.addField(command.getConfiguration().getName(), command.getConfiguration().getDescription(), false);
});
context.getChannel().sendMessage(builder.build()).queue();
return Result.fromSuccess();
}
@Override
public Configuration getConfiguration() {
return Configuration.builder()
.name("help")
.module("utility")
.description("Prints the help")
.causesReaction(false)
.build();
}
}

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>dev.sheldan.abstracto.command</groupId>
<artifactId>command-base</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>utility</artifactId>
</project>

View File

@@ -0,0 +1,38 @@
package dev.sheldan.abstracto.command.utility;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.execution.Configuration;
import dev.sheldan.abstracto.command.execution.Context;
import dev.sheldan.abstracto.command.execution.Parameter;
import dev.sheldan.abstracto.command.execution.Result;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class Echo implements Command {
@Override
public Result execute(Context context) {
StringBuilder sb = new StringBuilder();
context.getParameters().getParameters().forEach(o -> {
sb.append(o.toString());
});
context.getChannel().sendMessage(sb.toString()).queue();
return Result.fromSuccess();
}
@Override
public Configuration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("input").type(String.class).remainder(true).build());
return Configuration.builder()
.name("echo")
.module("utility")
.description("Echos the input back to the same channel")
.causesReaction(false)
.parameters(parameters)
.build();
}
}

View File

@@ -0,0 +1,28 @@
package dev.sheldan.abstracto.command.utility;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.execution.Configuration;
import dev.sheldan.abstracto.command.execution.Context;
import dev.sheldan.abstracto.command.execution.Result;
import org.springframework.stereotype.Service;
@Service
public class Ping implements Command {
@Override
public Result execute(Context context) {
long ping = context.getJda().getGatewayPing();
context.getChannel().sendMessage("Latency: " + ping + " ms.").queue();
return Result.fromSuccess();
}
@Override
public Configuration getConfiguration() {
return Configuration.builder()
.name("ping")
.module("utility")
.description("Prints the current bot latency to the discord api")
.causesReaction(false)
.build();
}
}

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>dev.sheldan.abstracto.command</groupId>
<artifactId>command</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<artifactId>command-int</artifactId>
<dependencies>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
<artifactId>opus-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,11 @@
package dev.sheldan.abstracto.command;
import dev.sheldan.abstracto.command.execution.Configuration;
import dev.sheldan.abstracto.command.execution.Context;
import dev.sheldan.abstracto.command.execution.Result;
public interface Command<T> {
Result execute(Context context);
Configuration getConfiguration();
}

View File

@@ -0,0 +1,17 @@
package dev.sheldan.abstracto.command.execution;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter @Setter @Builder
public class Configuration {
private String name;
private String module;
private String description;
private List<Parameter> parameters;
private boolean causesReaction;
}

View File

@@ -0,0 +1,18 @@
package dev.sheldan.abstracto.command.execution;
import lombok.Builder;
import lombok.Getter;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
@Builder
@Getter
public class Context {
private TextChannel channel;
private User author;
private Message message;
private Parameters parameters;
private JDA jda;
}

View File

@@ -0,0 +1,13 @@
package dev.sheldan.abstracto.command.execution;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Getter @Setter @Builder
public class Parameter {
private String name;
private Class type;
private boolean optional;
private boolean remainder;
}

View File

@@ -0,0 +1,12 @@
package dev.sheldan.abstracto.command.execution;
import lombok.Builder;
import lombok.Getter;
import java.util.List;
@Builder
public class Parameters {
@Getter
private List<Object> parameters;
}

View File

@@ -0,0 +1,20 @@
package dev.sheldan.abstracto.command.execution;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Getter @Setter @Builder
public class Result {
private ResultState result;
private String message;
public static Result fromSuccess() {
return Result.builder().result(ResultState.SUCCESSFUL).build();
}
public static Result fromError(String message){
return Result.builder().result(ResultState.ERROR).message(message).build();
}
}

View File

@@ -0,0 +1,5 @@
package dev.sheldan.abstracto.command.execution;
public enum ResultState {
ERROR, SUCCESSFUL, IGNORED
}

View File

@@ -0,0 +1,14 @@
package dev.sheldan.abstracto.command.meta;
import dev.sheldan.abstracto.command.Command;
import net.dv8tion.jda.api.entities.Message;
import java.util.List;
public interface CommandRegistry {
Command findCommandByParameters(String name, UnParsedCommandParameter context);
Command findCommand(String message);
List<Command> getAllCommands();
boolean isCommand(Message message);
}

View File

@@ -0,0 +1,11 @@
package dev.sheldan.abstracto.command.meta;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter @Setter
public class UnParsedCommandParameter {
private List<String> parameters;
}

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>dev.sheldan.abstracto.command</groupId>
<artifactId>command</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>command-management</artifactId>
<dependencies>
<dependency>
<groupId>dev.sheldan.abstracto.command</groupId>
<artifactId>command-int</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.sheldan.abstracto.core</groupId>
<artifactId>core-interface</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,6 @@
package dev.sheldan.abstracto.commands.management;
import dev.sheldan.abstracto.command.Command;
public abstract class AbstractCommand implements Command {
}

View File

@@ -0,0 +1,61 @@
package dev.sheldan.abstracto.commands.management;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.execution.Configuration;
import dev.sheldan.abstracto.command.execution.Parameter;
import dev.sheldan.abstracto.command.meta.CommandRegistry;
import dev.sheldan.abstracto.command.meta.UnParsedCommandParameter;
import net.dv8tion.jda.api.entities.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class CommandManager implements CommandRegistry {
@Autowired
private List<Command> commands;
@Override
public Command findCommandByParameters(String name, UnParsedCommandParameter unParsedCommandParameter) {
Optional<Command> commandOptional = commands.stream().filter((Command o )-> {
Configuration configuration = o.getConfiguration();
boolean parameterFit;
if(configuration.getParameters() != null){
boolean paramCountFits = unParsedCommandParameter.getParameters().size() - 1 == configuration.getParameters().size();
boolean hasRemainderParameter = configuration.getParameters().stream().anyMatch(Parameter::isRemainder);
parameterFit = paramCountFits || hasRemainderParameter;
} else {
parameterFit = unParsedCommandParameter.getParameters().size() == 0;
}
return configuration.getName().equals(name) && parameterFit;
}).findFirst();
if(commandOptional.isPresent()){
return commandOptional.get();
}
throw new CommandNotFoundException();
}
public Command findCommand(String name) {
Optional<Command> commandOptional = commands.stream().filter((Command o )-> {
Configuration configuration = o.getConfiguration();
return configuration.getName().equals(name);
}).findFirst();
if(commandOptional.isPresent()){
return commandOptional.get();
}
throw new CommandNotFoundException();
}
@Override
public List<Command> getAllCommands() {
return commands;
}
@Override
public boolean isCommand(Message message) {
return message.getContentRaw().startsWith("!");
}
}

View File

@@ -0,0 +1,4 @@
package dev.sheldan.abstracto.commands.management;
public class CommandNotFoundException extends RuntimeException {
}

View File

@@ -0,0 +1,60 @@
package dev.sheldan.abstracto.commands.management;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.execution.Context;
import dev.sheldan.abstracto.command.execution.Parameter;
import dev.sheldan.abstracto.command.execution.Parameters;
import dev.sheldan.abstracto.command.meta.UnParsedCommandParameter;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class CommandReceivedHandler extends ListenerAdapter {
@Autowired
private CommandManager manager;
@Override
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
if(!manager.isCommand(event.getMessage())) {
return;
}
List<String> parameters = Arrays.asList(event.getMessage().getContentStripped().split(" "));
UnParsedCommandParameter unparsedParameter = new UnParsedCommandParameter();
unparsedParameter.setParameters(parameters.subList(1, parameters.size()));
String withoutPrefix = parameters.get(0).substring(1);
Command foundCommand = manager.findCommandByParameters(withoutPrefix, unparsedParameter);
Parameters parsedParameters = getParsedParameters(unparsedParameter, foundCommand);
Context context = Context.builder()
.author(event.getAuthor())
.channel(event.getTextChannel())
.message(event.getMessage())
.parameters(parsedParameters)
.jda(event.getJDA())
.build();
foundCommand.execute(context);
}
public Parameters getParsedParameters(UnParsedCommandParameter unParsedCommandParameter, Command command){
List<Object> parsedParameters = new ArrayList<>();
for (int i = 0; i < unParsedCommandParameter.getParameters().size(); i++) {
Parameter param = command.getConfiguration().getParameters().get(i);
String value = unParsedCommandParameter.getParameters().get(i);
if(param.getType().equals(Integer.class)){
parsedParameters.add(Integer.parseInt(value));
} else if(param.getType().equals(Double.class)){
parsedParameters.add(Double.parseDouble(value));
} else {
parsedParameters.add(value);
}
}
return Parameters.builder().parameters(parsedParameters).build();
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>dev.sheldan.abstracto</groupId>
<artifactId>abstracto-application</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>dev.sheldan.abstracto.command</groupId>
<artifactId>command</artifactId>
<packaging>pom</packaging>
<modules>
<module>command-int</module>
<module>command-base</module>
<module>command-management</module>
</modules>
</project>