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";
}
}