added module responsible for accumulating the dependencies and resulting in a jar

added channels module
added help info object to command configuration
added description field to parameter
added modules for commands (and packed modules), they are mapped by name
added post command execution interface
added support for optional parameters
added support for using guildchannel as parameter
added printing of modules to help command
added service beans to wrap over the operations on the repository
added synchronizing of channels/roles on startup (controlled by flag)
added builder annotations to model classes
added more model classes
This commit is contained in:
Sheldan
2019-12-12 16:47:54 +01:00
parent 42cfe33b3a
commit 5c6b7b9a78
61 changed files with 1136 additions and 138 deletions

View File

@@ -0,0 +1,23 @@
<?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>channels</artifactId>
<dependencies>
<dependency>
<groupId>dev.sheldan.abstracto.core</groupId>
<artifactId>core-interface</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,18 @@
package dev.sheldan.abstracto.command.channels;
import dev.sheldan.abstracto.command.Module;
import dev.sheldan.abstracto.command.module.ModuleInfo;
import org.springframework.stereotype.Component;
@Component
public class ChannelsModule implements Module {
@Override
public ModuleInfo getInfo() {
return ModuleInfo.builder().name("channels").description("Includes utilities to configure the channel configuration stored in the database").build();
}
@Override
public String getParentModule() {
return "default";
}
}

View File

@@ -0,0 +1,49 @@
package dev.sheldan.abstracto.command.channels;
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 dev.sheldan.abstracto.core.models.AChannel;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.PostTargetService;
import net.dv8tion.jda.api.entities.GuildChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
@Service
public class PostTarget implements Command {
@Autowired
private PostTargetService service;
@Autowired
private ChannelService channelService;
@Override
public Result execute(Context context) {
GuildChannel channel = (GuildChannel) context.getParameters().getParameters().get(1);
String targetName = (String) context.getParameters().getParameters().get(0);
AChannel dbChannel = channelService.loadChannel(channel.getIdLong());
service.createOrUpdate(targetName, dbChannel);
return Result.fromSuccess();
}
@Override
public Configuration getConfiguration() {
Parameter channel = Parameter.builder().name("channel").type(GuildChannel.class).description("The channel to post towards").build();
Parameter postTargetName = Parameter.builder().name("name").type(String.class).description("The name of the post target to redirect").build();
List<Parameter> parameters = Arrays.asList(postTargetName, channel);
return Configuration.builder()
.name("posttarget")
.module("channels")
.parameters(parameters)
.description("Sets the target of a post done by the bot")
.causesReaction(false)
.build();
}
}

View File

@@ -15,6 +15,7 @@
<modules>
<module>utility</module>
<module>support</module>
<module>channels</module>
</modules>
<dependencies>

View File

@@ -1,38 +1,111 @@
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 dev.sheldan.abstracto.command.CommandHierarchy;
import dev.sheldan.abstracto.command.ModuleRegistry;
import dev.sheldan.abstracto.command.PackedModule;
import dev.sheldan.abstracto.command.execution.*;
import dev.sheldan.abstracto.command.module.ModuleInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@Service
public class Help implements Command {
@Autowired
private CommandRegistry registry;
private ModuleRegistry 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();
CommandHierarchy commandStructure = registry.getDetailedModules();
StringBuilder sb = new StringBuilder();
if(context.getParameters().getParameters().isEmpty()){
sb.append("Help | Module overview \n");
sb.append("```");
commandStructure.getRootModules().forEach(packedModule -> {
sb.append(getModule(packedModule, 0, true));
sb.append("\n");
});
sb.append("```");
} else {
String parameterValue = context.getParameters().getParameters().get(0).toString();
PackedModule module = commandStructure.getModuleWithName(parameterValue);
if(module != null){
sb.append("Help | Module overview \n");
sb.append(getModule(module, 0, false));
module.getCommands().forEach(command -> {
sb.append(getCommand(command));
});
} else {
Command command = commandStructure.getCommandWithName(parameterValue);
if(command != null) {
sb.append("Help | Command overview");
sb.append("\n");
sb.append(getCommand(command));
}
}
}
context.getChannel().sendMessage(sb.toString()).queue();
return Result.fromSuccess();
}
private String getCommand(Command command){
StringBuilder sb = new StringBuilder();
Configuration configuration = command.getConfiguration();
sb.append(String.format("Command: **%s**", configuration.getName()));
sb.append("\n");
sb.append(String.format("Description: %s", configuration.getDescription()));
sb.append("\n");
if(configuration.getHelp() != null){
sb.append(String.format("Usage: %s", configuration.getHelp().getUsage()));
sb.append("\n");
sb.append(String.format("Detailed help: %s", configuration.getHelp().getLongHelp()));
}
return sb.toString();
}
private String getModule(PackedModule module, int depth, boolean recursive){
StringBuilder sb = new StringBuilder();
String intentation = "";
if(depth > 0){
intentation = StringUtils.repeat("-", depth) + ">";
}
ModuleInfo info = module.getModule().getInfo();
sb.append(String.format(intentation +"**%s** \n", info.getName()));
sb.append(String.format(intentation + "%s \n", info.getDescription()));
if(recursive) {
module.getSubModules().forEach(subModule -> {
sb.append(getModule(subModule, depth + 1, true));
});
}
sb.append("\n");
return sb.toString();
}
@Override
public Configuration getConfiguration() {
Parameter moduleOrCommandName = Parameter.builder()
.name("name")
.optional(true)
.description("Name of module or command")
.type(String.class)
.build();
return Configuration.builder()
.name("help")
.module("utility")
.module("support")
.parameters(Collections.singletonList(moduleOrCommandName))
.description("Prints the help")
.causesReaction(false)
.build();
}
}

View File

@@ -0,0 +1,22 @@
package dev.sheldan.abstracto.command.support;
import dev.sheldan.abstracto.command.Module;
import dev.sheldan.abstracto.command.module.AbstracatoModule;
import dev.sheldan.abstracto.command.module.ModuleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SupportModule implements Module {
@Override
public ModuleInfo getInfo() {
return ModuleInfo.builder().name("support").description("Utilities for support").build();
}
@Override
public String getParentModule() {
return "default";
}
}

View File

@@ -1,6 +1,7 @@
package dev.sheldan.abstracto.command.utility;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.HelpInfo;
import dev.sheldan.abstracto.command.execution.Configuration;
import dev.sheldan.abstracto.command.execution.Context;
import dev.sheldan.abstracto.command.execution.Parameter;
@@ -27,12 +28,14 @@ public class Echo implements Command {
public Configuration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("input").type(String.class).remainder(true).build());
HelpInfo helpInfo = HelpInfo.builder().usage("echo <text>").longHelp("Echos back the text put in").build();
return Configuration.builder()
.name("echo")
.module("utility")
.description("Echos the input back to the same channel")
.causesReaction(false)
.parameters(parameters)
.help(helpInfo)
.build();
}
}

View File

@@ -1,9 +1,11 @@
package dev.sheldan.abstracto.command.utility;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.Module;
import dev.sheldan.abstracto.command.execution.Configuration;
import dev.sheldan.abstracto.command.execution.Context;
import dev.sheldan.abstracto.command.execution.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@@ -25,4 +27,5 @@ public class Ping implements Command {
.causesReaction(false)
.build();
}
}

View File

@@ -0,0 +1,19 @@
package dev.sheldan.abstracto.command.utility;
import dev.sheldan.abstracto.command.Module;
import dev.sheldan.abstracto.command.module.ModuleInfo;
import org.springframework.stereotype.Component;
@Component
public class UtilityModule implements Module {
@Override
public ModuleInfo getInfo() {
return ModuleInfo.builder().name("utility").description("General utilities").build();
}
@Override
public String getParentModule() {
return "default";
}
}

View File

@@ -0,0 +1,61 @@
package dev.sheldan.abstracto.command;
import lombok.Builder;
import lombok.Getter;
import java.util.List;
import java.util.Optional;
@Getter @Builder
public class CommandHierarchy {
private List<PackedModule> rootModules;
public PackedModule getModuleWithName(String name){
for (PackedModule module: rootModules) {
PackedModule found = getModuleWithName(name, module);
if(found != null){
return found;
}
}
return null;
}
private PackedModule getModuleWithName(String name, PackedModule module){
if(module.getModule().getInfo().getName().equals(name)){
return module;
} else {
for (PackedModule subModule: module.getSubModules()) {
PackedModule possibleModule = getModuleWithName(name, subModule);
if(possibleModule != null){
return possibleModule;
}
}
return null;
}
}
public Command getCommandWithName(String name) {
for (PackedModule module: rootModules) {
Command command = getCommandFromModule(name, module);
if(command != null){
return command;
}
}
return null;
}
private Command getCommandFromModule(String name, PackedModule module){
Command foundCommand = module.getCommands().stream().filter(command -> command.getConfiguration().getName().equals(name)).findAny().orElse(null);
if(foundCommand == null){
for (PackedModule subModule: module.getSubModules()) {
Command command = getCommandFromModule(name, subModule);
if(command != null){
return command;
}
}
return null;
} else {
return foundCommand;
}
}
}

View File

@@ -0,0 +1,10 @@
package dev.sheldan.abstracto.command;
import lombok.Builder;
import lombok.Getter;
@Getter @Builder
public class HelpInfo {
private String usage;
private String longHelp;
}

View File

@@ -0,0 +1,8 @@
package dev.sheldan.abstracto.command;
import dev.sheldan.abstracto.command.module.ModuleInfo;
public interface Module {
ModuleInfo getInfo();
String getParentModule();
}

View File

@@ -0,0 +1,9 @@
package dev.sheldan.abstracto.command;
import java.util.List;
public interface ModuleRegistry {
CommandHierarchy getDetailedModules();
List<Module> getModules();
}

View File

@@ -0,0 +1,15 @@
package dev.sheldan.abstracto.command;
import lombok.Builder;
import lombok.Getter;
import java.util.List;
@Builder
@Getter
public class PackedModule {
private Module module;
private PackedModule parentModule;
private List<PackedModule> subModules;
private List<Command> commands;
}

View File

@@ -0,0 +1,8 @@
package dev.sheldan.abstracto.command;
import dev.sheldan.abstracto.command.execution.Context;
import dev.sheldan.abstracto.command.execution.Result;
public interface PostCommandExecution {
void execute(Context context, Result result, Command command);
}

View File

@@ -1,12 +1,13 @@
package dev.sheldan.abstracto.command.execution;
import dev.sheldan.abstracto.command.HelpInfo;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter @Setter @Builder
@Getter @Builder
public class Configuration {
private String name;
@@ -14,4 +15,9 @@ public class Configuration {
private String description;
private List<Parameter> parameters;
private boolean causesReaction;
private HelpInfo help;
public long getNecessaryParameterCount(){
return parameters.stream().filter(parameter -> !parameter.isOptional()).count();
}
}

View File

@@ -8,6 +8,7 @@ import lombok.Setter;
public class Parameter {
private String name;
private Class type;
private String description;
private boolean optional;
private boolean remainder;
}

View File

@@ -2,6 +2,7 @@ package dev.sheldan.abstracto.command.meta;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.Module;
import net.dv8tion.jda.api.entities.Message;
import java.util.List;
@@ -10,5 +11,6 @@ public interface CommandRegistry {
Command findCommandByParameters(String name, UnParsedCommandParameter context);
Command findCommand(String message);
List<Command> getAllCommands();
List<Command> getAllCommandsFromModule(Module module);
boolean isCommand(Message message);
}

View File

@@ -0,0 +1,17 @@
package dev.sheldan.abstracto.command.module;
import dev.sheldan.abstracto.command.Module;
import org.springframework.stereotype.Service;
@Service
public class AbstracatoModule implements Module {
@Override
public ModuleInfo getInfo() {
return ModuleInfo.builder().name("default").description("Default module provided by abstracto").build();
}
@Override
public String getParentModule() {
return null;
}
}

View File

@@ -0,0 +1,11 @@
package dev.sheldan.abstracto.command.module;
import lombok.Builder;
import lombok.Getter;
@Getter @Builder
public class ModuleInfo {
private String name;
private String description;
}

View File

@@ -1,6 +1,7 @@
package dev.sheldan.abstracto.commands.management;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.Module;
import dev.sheldan.abstracto.command.execution.Configuration;
import dev.sheldan.abstracto.command.execution.Parameter;
import dev.sheldan.abstracto.command.meta.CommandRegistry;
@@ -9,6 +10,7 @@ import net.dv8tion.jda.api.entities.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@@ -24,7 +26,7 @@ public class CommandManager implements CommandRegistry {
Configuration configuration = o.getConfiguration();
boolean parameterFit;
if(configuration.getParameters() != null){
boolean paramCountFits = unParsedCommandParameter.getParameters().size() - 1 == configuration.getParameters().size();
boolean paramCountFits = unParsedCommandParameter.getParameters().size() >= configuration.getNecessaryParameterCount();
boolean hasRemainderParameter = configuration.getParameters().stream().anyMatch(Parameter::isRemainder);
parameterFit = paramCountFits || hasRemainderParameter;
} else {
@@ -54,6 +56,17 @@ public class CommandManager implements CommandRegistry {
return commands;
}
@Override
public List<Command> getAllCommandsFromModule(Module module) {
List<Command> commands = new ArrayList<>();
this.getAllCommands().forEach(command -> {
if(command.getConfiguration().getModule().equals(module.getInfo().getName())){
commands.add(command);
}
});
return commands;
}
@Override
public boolean isCommand(Message message) {
return message.getContentRaw().startsWith("!");

View File

@@ -1,10 +1,15 @@
package dev.sheldan.abstracto.commands.management;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.PostCommandExecution;
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.execution.Result;
import dev.sheldan.abstracto.command.meta.UnParsedCommandParameter;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.GuildChannel;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
@@ -21,6 +26,9 @@ public class CommandReceivedHandler extends ListenerAdapter {
@Autowired
private CommandManager manager;
@Autowired
private PostCommandExecution execution;
@Override
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
if(!manager.isCommand(event.getMessage())) {
@@ -31,7 +39,7 @@ public class CommandReceivedHandler extends ListenerAdapter {
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);
Parameters parsedParameters = getParsedParameters(unparsedParameter, foundCommand, event.getMessage());
Context context = Context.builder()
.author(event.getAuthor())
.channel(event.getTextChannel())
@@ -39,11 +47,13 @@ public class CommandReceivedHandler extends ListenerAdapter {
.parameters(parsedParameters)
.jda(event.getJDA())
.build();
foundCommand.execute(context);
Result result = foundCommand.execute(context);
execution.execute(context, result, foundCommand);
}
public Parameters getParsedParameters(UnParsedCommandParameter unParsedCommandParameter, Command command){
public Parameters getParsedParameters(UnParsedCommandParameter unParsedCommandParameter, Command command, Message message){
List<Object> parsedParameters = new ArrayList<>();
int mentionedChannelsCount = 0;
for (int i = 0; i < unParsedCommandParameter.getParameters().size(); i++) {
Parameter param = command.getConfiguration().getParameters().get(i);
String value = unParsedCommandParameter.getParameters().get(i);
@@ -51,7 +61,10 @@ public class CommandReceivedHandler extends ListenerAdapter {
parsedParameters.add(Integer.parseInt(value));
} else if(param.getType().equals(Double.class)){
parsedParameters.add(Double.parseDouble(value));
} else {
} else if(param.getType().equals(GuildChannel.class)){
parsedParameters.add(message.getMentionedChannels().get(mentionedChannelsCount));
mentionedChannelsCount++;
} else{
parsedParameters.add(value);
}
}

View File

@@ -0,0 +1,88 @@
package dev.sheldan.abstracto.commands.management;
import dev.sheldan.abstracto.command.*;
import dev.sheldan.abstracto.command.Module;
import dev.sheldan.abstracto.command.meta.CommandRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class ModuleManager implements ModuleRegistry {
@Autowired
private List<Module> modules;
@Autowired
private CommandRegistry commandRegistry;
@Override
public CommandHierarchy getDetailedModules() {
List<PackedModule> modulesWithCommands = new ArrayList<>();
List<Module> currentModules = getModules();
currentModules.forEach(module -> {
List<Command> commands = commandRegistry.getAllCommandsFromModule(module);
PackedModule packed = PackedModule.builder().commands(commands).module(module).subModules(new ArrayList<>()).build();
modulesWithCommands.add(packed);
});
return getHierarchicalPacks(modulesWithCommands, currentModules);
}
private CommandHierarchy getHierarchicalPacks(List<PackedModule> modules, List<Module> currentModules){
List<PackedModule> hierarchical = modules.stream().filter(packedModule -> packedModule.getModule().getParentModule() == null).collect(Collectors.toList());
List<PackedModule> subModules = modules.stream().filter(packedModule -> packedModule.getModule().getParentModule() != null).collect(Collectors.toList());
subModules.forEach(module -> {
List<Module> path = getModulePath(module, currentModules);
Collections.reverse(path);
Module rootModule = path.get(0);
Optional<PackedModule> any = hierarchical.stream().filter(moduleInList -> moduleInList.getModule().getInfo().getName().equals(rootModule.getInfo().getName())).findAny();
if(any.isPresent()){
PackedModule currentNodeInHierarchy = any.get();
for (int i = 1; i < path.size(); i++) {
Optional<PackedModule> nextInHierarchy = currentNodeInHierarchy.getSubModules().stream().filter(module1 -> module1.getModule().equals(module.getModule())).findAny();
if(nextInHierarchy.isPresent()){
currentNodeInHierarchy = nextInHierarchy.get();
} else {
currentNodeInHierarchy.getSubModules().add(module);
currentNodeInHierarchy = module;
}
}
if(path.size() == 1){
currentNodeInHierarchy.getSubModules().add(module);
}
}
});
return CommandHierarchy.builder().rootModules(hierarchical).build();
}
private List<Module> getModulePath(PackedModule moduleToPathFor, List<Module> currentModules){
List<Module> modulesBetweenRootAndThis = new ArrayList<>();
Module current = moduleToPathFor.getModule();
modulesBetweenRootAndThis.add(current);
while(current.getParentModule() != null){
String parentModule = current.getParentModule();
Optional<Module> possibleModule = currentModules.stream().filter(module1 -> module1.getInfo().getName().equals(parentModule)).findFirst();
if(possibleModule.isPresent()){
Module foundModule = possibleModule.get();
modulesBetweenRootAndThis.add(foundModule);
current = foundModule;
} else {
break;
}
}
return modulesBetweenRootAndThis;
}
@Override
public List<Module> getModules() {
return modules;
}
}

View File

@@ -0,0 +1,7 @@
package dev.sheldan.abstracto.commands.management;
public class PostTargetException extends RuntimeException {
public PostTargetException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,17 @@
package dev.sheldan.abstracto.commands.management;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.PostCommandExecution;
import dev.sheldan.abstracto.command.execution.Context;
import dev.sheldan.abstracto.command.execution.Result;
import org.springframework.stereotype.Service;
@Service
public class ReactionPostExecution implements PostCommandExecution {
@Override
public void execute(Context context, Result result, Command command) {
if(command.getConfiguration().isCausesReaction()){
context.getMessage().addReaction("").queue();
}
}
}