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

@@ -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();
}
}
}