mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-08 10:02:27 +00:00
added warn and ban command
changed command context to be a member instead of user, commands are only supported within guilds added custom exception templating for the error message added error message in case the parameters for a command were too little added support for templates on command help descriptions added post execution to be executed to print exception messages refactored command handling to be able to support exceptions added user and userInServer management
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
<?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>
|
||||
@@ -1,18 +0,0 @@
|
||||
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";
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
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.CommandContext;
|
||||
import dev.sheldan.abstracto.command.execution.Parameter;
|
||||
import dev.sheldan.abstracto.command.execution.Result;
|
||||
import dev.sheldan.abstracto.core.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.management.PostTargetManagement;
|
||||
import net.dv8tion.jda.api.entities.GuildChannel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class SetPostTargetCommand implements Command {
|
||||
|
||||
@Autowired
|
||||
private PostTargetManagement postTargetManagement;
|
||||
|
||||
@Autowired
|
||||
private ChannelManagementService channelManagementService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Result execute(CommandContext commandContext) {
|
||||
GuildChannel channel = (GuildChannel) commandContext.getParameters().getParameters().get(1);
|
||||
String targetName = (String) commandContext.getParameters().getParameters().get(0);
|
||||
postTargetManagement.createOrUpdate(targetName, channel.getIdLong(), channel.getGuild().getIdLong());
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
<?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>
|
||||
<module>channels</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>dev.sheldan.abstracto.command</groupId>
|
||||
<artifactId>command-int</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,15 +0,0 @@
|
||||
<?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>
|
||||
@@ -1,109 +0,0 @@
|
||||
package dev.sheldan.abstracto.command.support;
|
||||
|
||||
import dev.sheldan.abstracto.command.Command;
|
||||
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;
|
||||
|
||||
|
||||
@Service
|
||||
public class Help implements Command {
|
||||
|
||||
|
||||
@Autowired
|
||||
private ModuleRegistry registry;
|
||||
|
||||
@Override
|
||||
public Result execute(CommandContext commandContext) {
|
||||
CommandHierarchy commandStructure = registry.getDetailedModules();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if(commandContext.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 = commandContext.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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
commandContext.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("support")
|
||||
.parameters(Collections.singletonList(moduleOrCommandName))
|
||||
.description("Prints the help")
|
||||
.causesReaction(false)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
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";
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
<?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>
|
||||
@@ -1,52 +0,0 @@
|
||||
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.CommandContext;
|
||||
import dev.sheldan.abstracto.command.execution.Parameter;
|
||||
import dev.sheldan.abstracto.command.execution.Result;
|
||||
import dev.sheldan.abstracto.command.utility.model.EchoModel;
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class Echo implements Command {
|
||||
|
||||
private static final String TEMPLATE_NAME = "echo";
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Result execute(CommandContext commandContext) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
commandContext.getParameters().getParameters().forEach(o -> {
|
||||
sb.append(o.toString());
|
||||
});
|
||||
EchoModel model = EchoModel.parentBuilder().parent(commandContext.getCommandTemplateContext()).text(sb.toString()).build();
|
||||
commandContext.getChannel().sendMessage(templateService.renderTemplate(TEMPLATE_NAME, model)).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());
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
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.CommandContext;
|
||||
import dev.sheldan.abstracto.command.execution.Result;
|
||||
import dev.sheldan.abstracto.command.utility.model.PingModel;
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class Ping implements Command {
|
||||
|
||||
public static final String PING_TEMPLATE = "ping";
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Result execute(CommandContext commandContext) {
|
||||
long ping = commandContext.getJda().getGatewayPing();
|
||||
PingModel model = PingModel.parentBuilder().parent(commandContext.getCommandTemplateContext()).latency(ping).build();
|
||||
String text = templateService.renderTemplate(PING_TEMPLATE, model);
|
||||
commandContext.getChannel().sendMessage(text).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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
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";
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package dev.sheldan.abstracto.command.utility.model;
|
||||
|
||||
|
||||
import dev.sheldan.abstracto.command.execution.CommandTemplateContext;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class EchoModel extends CommandTemplateContext {
|
||||
private String text;
|
||||
|
||||
@Builder(builderMethodName = "parentBuilder")
|
||||
private EchoModel(CommandTemplateContext parent, String text) {
|
||||
super(parent);
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package dev.sheldan.abstracto.command.utility.model;
|
||||
|
||||
import dev.sheldan.abstracto.command.execution.CommandTemplateContext;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class PingModel extends CommandTemplateContext {
|
||||
private Long latency;
|
||||
|
||||
@Builder(builderMethodName = "parentBuilder")
|
||||
private PingModel(CommandTemplateContext parent, Long latency) {
|
||||
super(parent);
|
||||
this.latency = latency;
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
${text}
|
||||
@@ -1,11 +1,11 @@
|
||||
package dev.sheldan.abstracto.command;
|
||||
|
||||
import dev.sheldan.abstracto.command.execution.Configuration;
|
||||
import dev.sheldan.abstracto.command.execution.CommandConfiguration;
|
||||
import dev.sheldan.abstracto.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.command.execution.Result;
|
||||
|
||||
public interface Command<T> {
|
||||
|
||||
Result execute(CommandContext commandContext);
|
||||
Configuration getConfiguration();
|
||||
CommandConfiguration getConfiguration();
|
||||
}
|
||||
|
||||
@@ -7,4 +7,6 @@ import lombok.Getter;
|
||||
public class HelpInfo {
|
||||
private String usage;
|
||||
private String longHelp;
|
||||
private String longHelpTemplate;
|
||||
private String usageTemplate;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package dev.sheldan.abstracto.command;
|
||||
|
||||
public interface TemplatedException {
|
||||
String getTemplateName();
|
||||
Object getTemplateModel();
|
||||
}
|
||||
@@ -8,16 +8,17 @@ import lombok.Setter;
|
||||
import java.util.List;
|
||||
|
||||
@Getter @Builder
|
||||
public class Configuration {
|
||||
public class CommandConfiguration {
|
||||
|
||||
private String name;
|
||||
private String module;
|
||||
private String description;
|
||||
private String descriptionTemplate;
|
||||
private List<Parameter> parameters;
|
||||
private boolean causesReaction;
|
||||
private HelpInfo help;
|
||||
|
||||
public long getNecessaryParameterCount(){
|
||||
return parameters.stream().filter(parameter -> !parameter.isOptional()).count();
|
||||
public int getNecessaryParameterCount(){
|
||||
return (int) parameters.stream().filter(parameter -> !parameter.isOptional()).count();
|
||||
}
|
||||
}
|
||||
@@ -3,17 +3,14 @@ package dev.sheldan.abstracto.command.execution;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class CommandContext {
|
||||
private TextChannel channel;
|
||||
private Guild guild;
|
||||
private User author;
|
||||
private Member author;
|
||||
private Message message;
|
||||
private CommandTemplateContext commandTemplateContext;
|
||||
private Parameters parameters;
|
||||
|
||||
@@ -9,6 +9,7 @@ import lombok.Setter;
|
||||
public class Result {
|
||||
private ResultState result;
|
||||
private String message;
|
||||
private Throwable throwable;
|
||||
|
||||
public static Result fromSuccess() {
|
||||
return Result.builder().result(ResultState.SUCCESSFUL).build();
|
||||
@@ -17,4 +18,8 @@ public class Result {
|
||||
public static Result fromError(String message){
|
||||
return Result.builder().result(ResultState.ERROR).message(message).build();
|
||||
}
|
||||
|
||||
public static Result fromError(String message, Throwable throwable) {
|
||||
return Result.builder().result(ResultState.ERROR).message(message).throwable(throwable).build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package dev.sheldan.abstracto.commands.management.exception;
|
||||
|
||||
public class CommandNotFoundException extends RuntimeException {
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>command-management</artifactId>
|
||||
<artifactId>command-support</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -2,11 +2,12 @@ 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.CommandConfiguration;
|
||||
import dev.sheldan.abstracto.command.execution.Parameter;
|
||||
import dev.sheldan.abstracto.command.meta.CommandRegistry;
|
||||
import dev.sheldan.abstracto.command.meta.UnParsedCommandParameter;
|
||||
import dev.sheldan.abstracto.commands.management.exception.CommandNotFoundException;
|
||||
import dev.sheldan.abstracto.commands.management.exception.InsufficientParametersException;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -24,32 +25,39 @@ public class CommandManager implements CommandRegistry {
|
||||
@Override
|
||||
public Command findCommandByParameters(String name, UnParsedCommandParameter unParsedCommandParameter) {
|
||||
Optional<Command> commandOptional = commands.stream().filter((Command o )-> {
|
||||
Configuration configuration = o.getConfiguration();
|
||||
CommandConfiguration commandConfiguration = o.getConfiguration();
|
||||
if(!commandConfiguration.getName().equals(name)) {
|
||||
return false;
|
||||
}
|
||||
boolean parameterFit;
|
||||
if(configuration.getParameters() != null){
|
||||
boolean paramCountFits = unParsedCommandParameter.getParameters().size() >= configuration.getNecessaryParameterCount();
|
||||
boolean hasRemainderParameter = configuration.getParameters().stream().anyMatch(Parameter::isRemainder);
|
||||
if(commandConfiguration.getParameters() != null){
|
||||
boolean paramCountFits = unParsedCommandParameter.getParameters().size() >= commandConfiguration.getNecessaryParameterCount();
|
||||
boolean hasRemainderParameter = commandConfiguration.getParameters().stream().anyMatch(Parameter::isRemainder);
|
||||
if(unParsedCommandParameter.getParameters().size() < commandConfiguration.getNecessaryParameterCount()) {
|
||||
String nextParameterName = commandConfiguration.getParameters().get(commandConfiguration.getNecessaryParameterCount()).getName();
|
||||
throw new InsufficientParametersException("Insufficient parameters", o, nextParameterName);
|
||||
}
|
||||
parameterFit = paramCountFits || hasRemainderParameter;
|
||||
} else {
|
||||
parameterFit = unParsedCommandParameter.getParameters().size() == 0;
|
||||
}
|
||||
return configuration.getName().equals(name) && parameterFit;
|
||||
return parameterFit;
|
||||
}).findFirst();
|
||||
if(commandOptional.isPresent()){
|
||||
return commandOptional.get();
|
||||
}
|
||||
throw new CommandNotFoundException();
|
||||
throw new CommandNotFoundException("Command not found.");
|
||||
}
|
||||
|
||||
public Command findCommand(String name) {
|
||||
Optional<Command> commandOptional = commands.stream().filter((Command o )-> {
|
||||
Configuration configuration = o.getConfiguration();
|
||||
return configuration.getName().equals(name);
|
||||
CommandConfiguration commandConfiguration = o.getConfiguration();
|
||||
return commandConfiguration.getName().equals(name);
|
||||
}).findFirst();
|
||||
if(commandOptional.isPresent()){
|
||||
return commandOptional.get();
|
||||
}
|
||||
throw new CommandNotFoundException();
|
||||
throw new CommandNotFoundException("Command not found.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -9,11 +9,13 @@ import dev.sheldan.abstracto.core.management.ServerManagementService;
|
||||
import dev.sheldan.abstracto.core.models.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.AServer;
|
||||
import net.dv8tion.jda.api.entities.GuildChannel;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
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;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
@@ -24,10 +26,10 @@ import java.util.List;
|
||||
public class CommandReceivedHandler extends ListenerAdapter {
|
||||
|
||||
@Autowired
|
||||
private CommandManager manager;
|
||||
private CommandManager commandManager;
|
||||
|
||||
@Autowired
|
||||
private PostCommandExecution execution;
|
||||
private List<PostCommandExecution> executions;
|
||||
|
||||
@Autowired
|
||||
private ServerManagementService serverManagementService;
|
||||
@@ -36,27 +38,42 @@ public class CommandReceivedHandler extends ListenerAdapter {
|
||||
private ChannelManagementService channelManagementService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
|
||||
if(!manager.isCommand(event.getMessage())) {
|
||||
if(!commandManager.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, event.getMessage());
|
||||
CommandContext commandContext = CommandContext.builder()
|
||||
.author(event.getAuthor())
|
||||
if(!event.isFromGuild()) {
|
||||
return;
|
||||
}
|
||||
CommandContext.CommandContextBuilder commandContextBuilder = CommandContext.builder()
|
||||
.author(event.getMember())
|
||||
.guild(event.getGuild())
|
||||
.channel(event.getTextChannel())
|
||||
.message(event.getMessage())
|
||||
.parameters(parsedParameters)
|
||||
.jda(event.getJDA())
|
||||
.commandTemplateContext(buildTemplateParameter(event))
|
||||
.build();
|
||||
Result result = foundCommand.execute(commandContext);
|
||||
execution.execute(commandContext, result, foundCommand);
|
||||
.commandTemplateContext(buildTemplateParameter(event));
|
||||
Command foundCommand = null;
|
||||
try {
|
||||
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);
|
||||
foundCommand = commandManager.findCommandByParameters(withoutPrefix, unparsedParameter);
|
||||
Parameters parsedParameters = getParsedParameters(unparsedParameter, foundCommand, event.getMessage());
|
||||
CommandContext commandContext = commandContextBuilder.parameters(parsedParameters).build();
|
||||
Result result = foundCommand.execute(commandContext);
|
||||
for (PostCommandExecution postCommandExecution : executions) {
|
||||
postCommandExecution.execute(commandContext, result, foundCommand);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Result result = Result.fromError(e.getMessage(), e);
|
||||
CommandContext commandContext = commandContextBuilder.build();
|
||||
for (PostCommandExecution postCommandExecution : executions) {
|
||||
postCommandExecution.execute(commandContext, result, foundCommand);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private CommandTemplateContext buildTemplateParameter(MessageReceivedEvent event) {
|
||||
@@ -68,6 +85,7 @@ public class CommandReceivedHandler extends ListenerAdapter {
|
||||
public Parameters getParsedParameters(UnParsedCommandParameter unParsedCommandParameter, Command command, Message message){
|
||||
List<Object> parsedParameters = new ArrayList<>();
|
||||
int mentionedChannelsCount = 0;
|
||||
int mentionedUserCount = 0;
|
||||
for (int i = 0; i < unParsedCommandParameter.getParameters().size(); i++) {
|
||||
Parameter param = command.getConfiguration().getParameters().get(i);
|
||||
String value = unParsedCommandParameter.getParameters().get(i);
|
||||
@@ -78,7 +96,10 @@ public class CommandReceivedHandler extends ListenerAdapter {
|
||||
} else if(param.getType().equals(GuildChannel.class)){
|
||||
parsedParameters.add(message.getMentionedChannels().get(mentionedChannelsCount));
|
||||
mentionedChannelsCount++;
|
||||
} else{
|
||||
} else if(param.getType().equals(Member.class)) {
|
||||
parsedParameters.add(message.getMentionedMembers().get(mentionedUserCount));
|
||||
mentionedUserCount++;
|
||||
} else {
|
||||
parsedParameters.add(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package dev.sheldan.abstracto.commands.management.exception;
|
||||
|
||||
import dev.sheldan.abstracto.command.TemplatedException;
|
||||
|
||||
public class CommandNotFoundException extends RuntimeException implements TemplatedException {
|
||||
|
||||
public CommandNotFoundException(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTemplateName() {
|
||||
return "command_not_found";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTemplateModel() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package dev.sheldan.abstracto.commands.management.exception;
|
||||
|
||||
import dev.sheldan.abstracto.command.TemplatedException;
|
||||
import dev.sheldan.abstracto.command.Command;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@Getter
|
||||
public class InsufficientParametersException extends RuntimeException implements TemplatedException {
|
||||
|
||||
private Command command;
|
||||
private String parameterName;
|
||||
|
||||
public InsufficientParametersException(String s, Command command, String parameterName) {
|
||||
super(s);
|
||||
this.command = command;
|
||||
this.parameterName = parameterName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTemplateName() {
|
||||
return "insufficient_parameters";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTemplateModel() {
|
||||
HashMap<String, Object> model = new HashMap<>();
|
||||
model.put("parameterName", parameterName);
|
||||
return model;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package dev.sheldan.abstracto.commands.management.post;
|
||||
|
||||
import dev.sheldan.abstracto.command.Command;
|
||||
import dev.sheldan.abstracto.command.PostCommandExecution;
|
||||
import dev.sheldan.abstracto.command.TemplatedException;
|
||||
import dev.sheldan.abstracto.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.command.execution.Result;
|
||||
import dev.sheldan.abstracto.command.execution.ResultState;
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ExceptionPostExecution implements PostCommandExecution {
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Override
|
||||
public void execute(CommandContext commandContext, Result result, Command command) {
|
||||
if(result.getResult().equals(ResultState.ERROR)) {
|
||||
if(result.getThrowable() != null) {
|
||||
if(result.getThrowable() instanceof TemplatedException) {
|
||||
TemplatedException exception = (TemplatedException) result.getThrowable();
|
||||
String text = templateService.renderTemplate(exception.getTemplateName(), exception.getTemplateModel());
|
||||
commandContext.getChannel().sendMessage(text).queue();
|
||||
} else {
|
||||
commandContext.getChannel().sendMessage(result.getMessage()).queue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,23 @@
|
||||
package dev.sheldan.abstracto.commands.management;
|
||||
package dev.sheldan.abstracto.commands.management.post;
|
||||
|
||||
import dev.sheldan.abstracto.command.Command;
|
||||
import dev.sheldan.abstracto.command.PostCommandExecution;
|
||||
import dev.sheldan.abstracto.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.command.execution.Result;
|
||||
import dev.sheldan.abstracto.command.execution.ResultState;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ReactionPostExecution implements PostCommandExecution {
|
||||
@Override
|
||||
public void execute(CommandContext commandContext, Result result, Command command) {
|
||||
if(command.getConfiguration().isCausesReaction()){
|
||||
commandContext.getMessage().addReaction("⭐").queue();
|
||||
if(result.getResult().equals(ResultState.ERROR)) {
|
||||
commandContext.getMessage().addReaction("⚠️").queue();
|
||||
} else {
|
||||
if(command.getConfiguration().isCausesReaction()){
|
||||
commandContext.getMessage().addReaction("⭐").queue();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Command was not found.
|
||||
@@ -0,0 +1 @@
|
||||
Insufficient parameters: ${parameterName} was not found.
|
||||
@@ -14,8 +14,7 @@
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>command-int</module>
|
||||
<module>command-base</module>
|
||||
<module>command-management</module>
|
||||
<module>command-support</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user