mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-03 00:20:12 +00:00
added template support to echo
replaced HashMaps for templates with concrete models, so the interface is easier known added rendering method for objects directly added default parameters available for commands split up service beans for server/channel into management and service bean added server reference in AChannel refactored join/leave listener
This commit is contained in:
@@ -2,11 +2,11 @@ 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.CommandContext;
|
||||
import dev.sheldan.abstracto.command.execution.Parameter;
|
||||
import dev.sheldan.abstracto.command.execution.Result;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.core.service.PostTargetService;
|
||||
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;
|
||||
@@ -19,17 +19,17 @@ import java.util.List;
|
||||
public class SetPostTargetCommand implements Command {
|
||||
|
||||
@Autowired
|
||||
private PostTargetService postTargetService;
|
||||
private PostTargetManagement postTargetManagement;
|
||||
|
||||
@Autowired
|
||||
private ChannelService channelService;
|
||||
private ChannelManagementService channelManagementService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Result execute(Context context) {
|
||||
GuildChannel channel = (GuildChannel) context.getParameters().getParameters().get(1);
|
||||
String targetName = (String) context.getParameters().getParameters().get(0);
|
||||
postTargetService.createOrUpdate(targetName, channel.getIdLong(), channel.getGuild().getIdLong());
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -26,22 +26,4 @@
|
||||
</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>
|
||||
@@ -11,8 +11,6 @@ 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
|
||||
@@ -23,10 +21,10 @@ public class Help implements Command {
|
||||
private ModuleRegistry registry;
|
||||
|
||||
@Override
|
||||
public Result execute(Context context) {
|
||||
public Result execute(CommandContext commandContext) {
|
||||
CommandHierarchy commandStructure = registry.getDetailedModules();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if(context.getParameters().getParameters().isEmpty()){
|
||||
if(commandContext.getParameters().getParameters().isEmpty()){
|
||||
sb.append("Help | Module overview \n");
|
||||
sb.append("```");
|
||||
commandStructure.getRootModules().forEach(packedModule -> {
|
||||
@@ -35,7 +33,7 @@ public class Help implements Command {
|
||||
});
|
||||
sb.append("```");
|
||||
} else {
|
||||
String parameterValue = context.getParameters().getParameters().get(0).toString();
|
||||
String parameterValue = commandContext.getParameters().getParameters().get(0).toString();
|
||||
PackedModule module = commandStructure.getModuleWithName(parameterValue);
|
||||
if(module != null){
|
||||
sb.append("Help | Module overview \n");
|
||||
@@ -53,7 +51,7 @@ public class Help implements Command {
|
||||
}
|
||||
}
|
||||
|
||||
context.getChannel().sendMessage(sb.toString()).queue();
|
||||
commandContext.getChannel().sendMessage(sb.toString()).queue();
|
||||
return Result.fromSuccess();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,10 +3,14 @@ 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.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;
|
||||
@@ -14,13 +18,20 @@ import java.util.List;
|
||||
@Service
|
||||
public class Echo implements Command {
|
||||
|
||||
private static final String TEMPLATE_NAME = "echo";
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Override
|
||||
public Result execute(Context context) {
|
||||
@Transactional
|
||||
public Result execute(CommandContext commandContext) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
context.getParameters().getParameters().forEach(o -> {
|
||||
commandContext.getParameters().getParameters().forEach(o -> {
|
||||
sb.append(o.toString());
|
||||
});
|
||||
context.getChannel().sendMessage(sb.toString()).queue();
|
||||
EchoModel model = EchoModel.parentBuilder().parent(commandContext.getCommandTemplateContext()).text(sb.toString()).build();
|
||||
commandContext.getChannel().sendMessage(templateService.renderTemplate(TEMPLATE_NAME, model)).queue();
|
||||
return Result.fromSuccess();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,15 +2,14 @@ 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.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;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@Service
|
||||
public class Ping implements Command {
|
||||
|
||||
@@ -21,12 +20,11 @@ public class Ping implements Command {
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Result execute(Context context) {
|
||||
long ping = context.getJda().getGatewayPing();
|
||||
HashMap<String, Object> parameters = new HashMap<>();
|
||||
parameters.put("latency", ping);
|
||||
String text = templateService.renderTemplate(PING_TEMPLATE, parameters);
|
||||
context.getChannel().sendMessage(text).queue();
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
${text}
|
||||
@@ -35,6 +35,12 @@
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>dev.sheldan.abstracto.core</groupId>
|
||||
<artifactId>core-interface</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +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.CommandContext;
|
||||
import dev.sheldan.abstracto.command.execution.Result;
|
||||
|
||||
public interface Command<T> {
|
||||
|
||||
Result execute(Context context);
|
||||
Result execute(CommandContext commandContext);
|
||||
Configuration getConfiguration();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package dev.sheldan.abstracto.command;
|
||||
|
||||
import dev.sheldan.abstracto.command.execution.Context;
|
||||
import dev.sheldan.abstracto.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.command.execution.Result;
|
||||
|
||||
public interface PostCommandExecution {
|
||||
void execute(Context context, Result result, Command command);
|
||||
void execute(CommandContext commandContext, Result result, Command command);
|
||||
}
|
||||
|
||||
@@ -10,11 +10,12 @@ import net.dv8tion.jda.api.entities.User;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class Context {
|
||||
public class CommandContext {
|
||||
private TextChannel channel;
|
||||
private Guild guild;
|
||||
private User author;
|
||||
private Message message;
|
||||
private CommandTemplateContext commandTemplateContext;
|
||||
private Parameters parameters;
|
||||
private JDA jda;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package dev.sheldan.abstracto.command.execution;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.AServer;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter @Builder @AllArgsConstructor
|
||||
public class CommandTemplateContext {
|
||||
private AChannel channel;
|
||||
private AServer server;
|
||||
|
||||
public CommandTemplateContext(CommandTemplateContext commandTemplateContext) {
|
||||
this.channel = commandTemplateContext.channel;
|
||||
this.server = commandTemplateContext.server;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ 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 dev.sheldan.abstracto.commands.management.exception.CommandNotFoundException;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -2,12 +2,12 @@ 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.execution.*;
|
||||
import dev.sheldan.abstracto.command.meta.UnParsedCommandParameter;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import dev.sheldan.abstracto.core.management.ChannelManagementService;
|
||||
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.Message;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
@@ -29,6 +29,12 @@ public class CommandReceivedHandler extends ListenerAdapter {
|
||||
@Autowired
|
||||
private PostCommandExecution execution;
|
||||
|
||||
@Autowired
|
||||
private ServerManagementService serverManagementService;
|
||||
|
||||
@Autowired
|
||||
private ChannelManagementService channelManagementService;
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
|
||||
if(!manager.isCommand(event.getMessage())) {
|
||||
@@ -40,16 +46,23 @@ public class CommandReceivedHandler extends ListenerAdapter {
|
||||
String withoutPrefix = parameters.get(0).substring(1);
|
||||
Command foundCommand = manager.findCommandByParameters(withoutPrefix, unparsedParameter);
|
||||
Parameters parsedParameters = getParsedParameters(unparsedParameter, foundCommand, event.getMessage());
|
||||
Context context = Context.builder()
|
||||
CommandContext commandContext = CommandContext.builder()
|
||||
.author(event.getAuthor())
|
||||
.guild(event.getGuild())
|
||||
.channel(event.getTextChannel())
|
||||
.message(event.getMessage())
|
||||
.parameters(parsedParameters)
|
||||
.jda(event.getJDA())
|
||||
.commandTemplateContext(buildTemplateParameter(event))
|
||||
.build();
|
||||
Result result = foundCommand.execute(context);
|
||||
execution.execute(context, result, foundCommand);
|
||||
Result result = foundCommand.execute(commandContext);
|
||||
execution.execute(commandContext, result, foundCommand);
|
||||
}
|
||||
|
||||
private CommandTemplateContext buildTemplateParameter(MessageReceivedEvent event) {
|
||||
AChannel channel = channelManagementService.loadChannel(event.getChannel().getIdLong());
|
||||
AServer server = serverManagementService.loadServer(event.getGuild().getIdLong());
|
||||
return CommandTemplateContext.builder().channel(channel).server(server).build();
|
||||
}
|
||||
|
||||
public Parameters getParsedParameters(UnParsedCommandParameter unParsedCommandParameter, Command command, Message message){
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package dev.sheldan.abstracto.commands.management;
|
||||
|
||||
public class PostTargetException extends RuntimeException {
|
||||
public PostTargetException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,16 @@ 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.CommandContext;
|
||||
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) {
|
||||
public void execute(CommandContext commandContext, Result result, Command command) {
|
||||
if(command.getConfiguration().isCausesReaction()){
|
||||
context.getMessage().addReaction("⭐").queue();
|
||||
commandContext.getMessage().addReaction("⭐").queue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package dev.sheldan.abstracto.commands.management;
|
||||
package dev.sheldan.abstracto.commands.management.exception;
|
||||
|
||||
public class CommandNotFoundException extends RuntimeException {
|
||||
}
|
||||
Reference in New Issue
Block a user