added command to add/remove channels from channel groups

added storing the commands with their modules in the db
renamed command impl module
This commit is contained in:
Sheldan
2020-04-04 17:09:28 +02:00
parent bf94af66d5
commit 20b282584a
64 changed files with 637 additions and 71 deletions

View File

@@ -9,7 +9,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>command-support</artifactId>
<artifactId>command-impl</artifactId>
<dependencies>
<dependency>

View File

@@ -1,4 +1,4 @@
package dev.sheldan.abstracto.commands.management;
package dev.sheldan.abstracto.commands;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.Module;
@@ -6,8 +6,8 @@ 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.CommandNotFound;
import dev.sheldan.abstracto.commands.management.exception.InsufficientParameters;
import dev.sheldan.abstracto.commands.exception.CommandNotFound;
import dev.sheldan.abstracto.commands.exception.InsufficientParameters;
import net.dv8tion.jda.api.entities.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -26,7 +26,7 @@ public class CommandManager implements CommandRegistry {
public Command findCommandByParameters(String name, UnParsedCommandParameter unParsedCommandParameter) {
Optional<Command> commandOptional = commands.stream().filter((Command o )-> {
CommandConfiguration commandConfiguration = o.getConfiguration();
if(!commandConfiguration.getName().equals(name)) {
if(!commandConfiguration.getName().equalsIgnoreCase(name)) {
return false;
}
boolean parameterFit;

View File

@@ -1,13 +1,13 @@
package dev.sheldan.abstracto.commands.management;
package dev.sheldan.abstracto.commands;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.CommandCondition;
import dev.sheldan.abstracto.command.ConditionalCommand;
import dev.sheldan.abstracto.command.PostCommandExecution;
import dev.sheldan.abstracto.command.service.PostCommandExecution;
import dev.sheldan.abstracto.command.execution.*;
import dev.sheldan.abstracto.command.meta.UnParsedCommandParameter;
import dev.sheldan.abstracto.commands.management.exception.IncorrectParameter;
import dev.sheldan.abstracto.commands.management.exception.ParameterTooLong;
import dev.sheldan.abstracto.commands.exception.IncorrectParameter;
import dev.sheldan.abstracto.commands.exception.ParameterTooLong;
import dev.sheldan.abstracto.core.Constants;
import dev.sheldan.abstracto.core.exception.*;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
@@ -26,8 +26,10 @@ import net.dv8tion.jda.api.entities.TextChannel;
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.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Nonnull;
@@ -54,9 +56,13 @@ public class CommandReceivedHandler extends ListenerAdapter {
@Autowired
private ChannelManagementService channelManagementService;
@Autowired
@Lazy
private CommandReceivedHandler self;
@Override
@Transactional
@Async
@Transactional
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
if(!commandManager.isCommand(event.getMessage())) {
return;
@@ -85,9 +91,8 @@ public class CommandReceivedHandler extends ListenerAdapter {
if (!shouldExecute(commandContext, foundCommand, castedCommand.getConditions())) {
throw new FeatureDisabledException(String.format("Feature `%s` has been disabled. Command is not usable", foundCommand.getFeature()));
}
}
CommandResult commandResult = foundCommand.execute(commandContext);
CommandResult commandResult = self.executeCommand(foundCommand, commandContext);
for (PostCommandExecution postCommandExecution : executions) {
postCommandExecution.execute(commandContext, commandResult, foundCommand);
}
@@ -100,6 +105,11 @@ public class CommandReceivedHandler extends ListenerAdapter {
}
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public CommandResult executeCommand(Command foundCommand, CommandContext commandContext) {
return foundCommand.execute(commandContext);
}
public boolean shouldExecute(CommandContext commandContext, Command command, List<CommandCondition> conditions) {
AtomicBoolean shouldExecute = new AtomicBoolean(true);
if(conditions != null) {

View File

@@ -1,8 +1,9 @@
package dev.sheldan.abstracto.commands.management;
package dev.sheldan.abstracto.commands;
import dev.sheldan.abstracto.command.*;
import dev.sheldan.abstracto.command.Module;
import dev.sheldan.abstracto.command.meta.CommandRegistry;
import dev.sheldan.abstracto.command.service.ModuleRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

View File

@@ -1,4 +1,4 @@
package dev.sheldan.abstracto.commands.management.config;
package dev.sheldan.abstracto.commands.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

View File

@@ -0,0 +1,29 @@
package dev.sheldan.abstracto.commands.config;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.service.CommandService;
import dev.sheldan.abstracto.core.listener.ServerConfigListener;
import dev.sheldan.abstracto.core.models.database.AServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class CommandConfigListener implements ServerConfigListener {
@Autowired
private List<Command> commandList;
@Autowired
private CommandService commandService;
@Override
public void updateServerConfig(AServer server) {
commandList.forEach(command -> {
if(!commandService.doesCommandExist(command.getConfiguration().getName())) {
commandService.createCommand(command.getConfiguration().getName(), command.getConfiguration().getModule());
}
});
}
}

View File

@@ -1,4 +1,4 @@
package dev.sheldan.abstracto.commands.management.exception;
package dev.sheldan.abstracto.commands.exception;
import dev.sheldan.abstracto.command.Templatable;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;

View File

@@ -1,4 +1,4 @@
package dev.sheldan.abstracto.commands.management.exception;
package dev.sheldan.abstracto.commands.exception;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.Templatable;

View File

@@ -1,4 +1,4 @@
package dev.sheldan.abstracto.commands.management.exception;
package dev.sheldan.abstracto.commands.exception;
import dev.sheldan.abstracto.command.Templatable;
import dev.sheldan.abstracto.command.Command;

View File

@@ -1,4 +1,4 @@
package dev.sheldan.abstracto.commands.management.exception;
package dev.sheldan.abstracto.commands.exception;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.Templatable;

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.commands.management.post;
package dev.sheldan.abstracto.commands.post;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.PostCommandExecution;
import dev.sheldan.abstracto.command.service.PostCommandExecution;
import dev.sheldan.abstracto.command.Templatable;
import dev.sheldan.abstracto.command.execution.CommandContext;
import dev.sheldan.abstracto.command.execution.CommandResult;

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.commands.management.post;
package dev.sheldan.abstracto.commands.post;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.PostCommandExecution;
import dev.sheldan.abstracto.command.service.PostCommandExecution;
import dev.sheldan.abstracto.command.execution.CommandContext;
import dev.sheldan.abstracto.command.execution.CommandResult;
import dev.sheldan.abstracto.command.execution.ResultState;

View File

@@ -0,0 +1,8 @@
package dev.sheldan.abstracto.commands.repository;
import dev.sheldan.abstracto.command.models.ACommand;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CommandRepository extends JpaRepository<ACommand, Long> {
ACommand findByName(String name);
}

View File

@@ -0,0 +1,8 @@
package dev.sheldan.abstracto.commands.repository;
import dev.sheldan.abstracto.command.models.AModule;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ModuleRepository extends JpaRepository<AModule, Long> {
AModule findByName(String name);
}

View File

@@ -0,0 +1,32 @@
package dev.sheldan.abstracto.commands.service;
import dev.sheldan.abstracto.command.models.ACommand;
import dev.sheldan.abstracto.command.models.AModule;
import dev.sheldan.abstracto.command.service.CommandService;
import dev.sheldan.abstracto.command.service.management.CommandManagementService;
import dev.sheldan.abstracto.command.service.management.ModuleManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class CommandServiceBean implements CommandService {
@Autowired
private ModuleManagementService moduleManagementService;
@Autowired
private CommandManagementService commandManagementService;
@Override
public ACommand createCommand(String name, String moduleName) {
AModule module = moduleManagementService.getOrCreate(moduleName);
return commandManagementService.createCommand(name, module);
}
@Override
public Boolean doesCommandExist(String name) {
return commandManagementService.doesCommandExist(name);
}
}

View File

@@ -0,0 +1,51 @@
package dev.sheldan.abstracto.commands.service.management;
import dev.sheldan.abstracto.command.models.ACommand;
import dev.sheldan.abstracto.command.models.AModule;
import dev.sheldan.abstracto.command.service.management.CommandManagementService;
import dev.sheldan.abstracto.command.service.management.ModuleManagementService;
import dev.sheldan.abstracto.commands.repository.CommandRepository;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class CommandManagementServiceBean implements CommandManagementService {
@Autowired
private ServerManagementService serverManagementService;
@Autowired
private ModuleManagementService moduleManagementService;
@Autowired
private CommandRepository commandRepository;
@Override
public ACommand createCommand(String name, String moduleName) {
AModule module = moduleManagementService.findModuleByName(moduleName);
return createCommand(name, module);
}
@Override
public ACommand createCommand(String name, AModule module) {
ACommand command = ACommand
.builder()
.name(name)
.module(module)
.build();
commandRepository.save(command);
return command;
}
@Override
public ACommand findCommandByName(String name) {
return commandRepository.findByName(name);
}
@Override
public Boolean doesCommandExist(String name) {
return findCommandByName(name) != null;
}
}

View File

@@ -0,0 +1,43 @@
package dev.sheldan.abstracto.commands.service.management;
import dev.sheldan.abstracto.command.models.AModule;
import dev.sheldan.abstracto.command.service.management.ModuleManagementService;
import dev.sheldan.abstracto.commands.repository.ModuleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ModuleManagementServiceBean implements ModuleManagementService {
@Autowired
private ModuleRepository moduleRepository;
@Override
public AModule createModule(String name) {
AModule module = AModule.
builder()
.name(name)
.build();
moduleRepository.save(module);
return module;
}
@Override
public AModule getOrCreate(String name) {
AModule module = findModuleByName(name);
if(module == null) {
return createModule(name);
}
return module;
}
@Override
public AModule findModuleByName(String name) {
return moduleRepository.findByName(name);
}
@Override
public Boolean doesModuleExist(String name) {
return findModuleByName(name) != null;
}
}

View File

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

View File

@@ -0,0 +1,27 @@
package dev.sheldan.abstracto.command.models;
import lombok.*;
import javax.persistence.*;
@Entity
@Table(name = "command")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class ACommand {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@Getter
@Setter
@JoinColumn(name = "module_id")
private AModule module;
}

View File

@@ -0,0 +1,34 @@
package dev.sheldan.abstracto.command.models;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "module")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class AModule {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true)
@Builder.Default
@JoinColumn(name = "module_id")
private List<ACommand> commands = new ArrayList<>();
}

View File

@@ -0,0 +1,8 @@
package dev.sheldan.abstracto.command.service;
import dev.sheldan.abstracto.command.models.ACommand;
public interface CommandService {
ACommand createCommand(String name, String moduleName);
Boolean doesCommandExist(String name);
}

View File

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

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.command;
package dev.sheldan.abstracto.command.service;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.execution.CommandContext;
import dev.sheldan.abstracto.command.execution.CommandResult;

View File

@@ -0,0 +1,11 @@
package dev.sheldan.abstracto.command.service.management;
import dev.sheldan.abstracto.command.models.ACommand;
import dev.sheldan.abstracto.command.models.AModule;
public interface CommandManagementService {
ACommand createCommand(String name, String moduleName);
ACommand createCommand(String name, AModule moduleName);
ACommand findCommandByName(String name);
Boolean doesCommandExist(String name);
}

View File

@@ -0,0 +1,10 @@
package dev.sheldan.abstracto.command.service.management;
import dev.sheldan.abstracto.command.models.AModule;
public interface ModuleManagementService {
AModule createModule(String name);
AModule getOrCreate(String name);
AModule findModuleByName(String name);
Boolean doesModuleExist(String name);
}

View File

@@ -14,7 +14,7 @@
<packaging>pom</packaging>
<modules>
<module>command-int</module>
<module>command-support</module>
<module>command-impl</module>
</modules>