added error message logging to reaction post execution

added validation to post target command
added templates for post target command
moved ping and echo model to interface
added show emote command
This commit is contained in:
Sheldan
2020-03-22 12:34:52 +01:00
parent d95382b589
commit 8cd9068cfc
32 changed files with 154 additions and 17 deletions

View File

@@ -1,12 +1,12 @@
package dev.sheldan.abstracto.core.commands.channels;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.execution.CommandConfiguration;
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.execution.*;
import dev.sheldan.abstracto.core.management.ChannelManagementService;
import dev.sheldan.abstracto.core.management.PostTargetManagement;
import dev.sheldan.abstracto.core.models.command.PostTargetErrorModel;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.templating.TemplateService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.GuildChannel;
@@ -21,16 +21,36 @@ import java.util.List;
@Slf4j
public class PostTargetCommand implements Command {
public static final String POST_TARGET_NO_TARGET_TEMPLATE = "posttarget_no_target";
public static final String POST_TARGET_INVALID_TARGET_TEMPLATE = "posttarget_invalid_target";
@Autowired
private PostTargetManagement postTargetManagement;
@Autowired
private PostTargetService postTargetService;
@Autowired
private ChannelManagementService channelManagementService;
@Autowired
private TemplateService templateService;
@Override
public Result execute(CommandContext commandContext) {
GuildChannel channel = (GuildChannel) commandContext.getParameters().getParameters().get(1);
if(commandContext.getParameters().getParameters().isEmpty()) {
PostTargetErrorModel postTargetErrorModel = (PostTargetErrorModel) ContextConverter.fromCommandContext(commandContext, PostTargetErrorModel.class);
postTargetErrorModel.setValidPostTargets(postTargetService.getAvailablePostTargets());
String errorMessage = templateService.renderTemplate(POST_TARGET_NO_TARGET_TEMPLATE, postTargetErrorModel);
return Result.fromError(errorMessage);
}
String targetName = (String) commandContext.getParameters().getParameters().get(0);
if(!postTargetService.validPostTarget(targetName)) {
PostTargetErrorModel postTargetErrorModel = (PostTargetErrorModel) ContextConverter.fromCommandContext(commandContext, PostTargetErrorModel.class);
postTargetErrorModel.setValidPostTargets(postTargetService.getAvailablePostTargets());
String errorMessage = templateService.renderTemplate(POST_TARGET_INVALID_TARGET_TEMPLATE, postTargetErrorModel);
return Result.fromError(errorMessage);
}
GuildChannel channel = (GuildChannel) commandContext.getParameters().getParameters().get(1);
Guild guild = channel.getGuild();
postTargetManagement.createOrUpdate(targetName, guild.getIdLong(), channel.getIdLong());
log.info("Setting posttarget {} in {} to {}", targetName, guild.getIdLong(), channel.getId());
@@ -39,8 +59,8 @@ public class PostTargetCommand implements Command {
@Override
public CommandConfiguration getConfiguration() {
Parameter channel = Parameter.builder().name("channel").type(TextChannel.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();
Parameter channel = Parameter.builder().name("channel").type(TextChannel.class).optional(true).description("The channel to post towards").build();
Parameter postTargetName = Parameter.builder().name("name").type(String.class).optional(true).description("The name of the post target to redirect").build();
List<Parameter> parameters = Arrays.asList(postTargetName, channel);
return CommandConfiguration.builder()
.name("posttarget")

View File

@@ -6,7 +6,7 @@ import dev.sheldan.abstracto.command.execution.CommandConfiguration;
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.commands.utility.model.EchoModel;
import dev.sheldan.abstracto.core.models.command.EchoModel;
import dev.sheldan.abstracto.templating.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

View File

@@ -4,7 +4,7 @@ import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.execution.CommandConfiguration;
import dev.sheldan.abstracto.command.execution.CommandContext;
import dev.sheldan.abstracto.command.execution.Result;
import dev.sheldan.abstracto.core.commands.utility.model.PingModel;
import dev.sheldan.abstracto.core.models.command.PingModel;
import dev.sheldan.abstracto.templating.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

View File

@@ -1,12 +0,0 @@
package dev.sheldan.abstracto.core.commands.utility.model;
import dev.sheldan.abstracto.core.models.UserInitiatedServerContext;
import lombok.Builder;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
@Getter @SuperBuilder
public class EchoModel extends UserInitiatedServerContext {
private String text;
}

View File

@@ -1,11 +0,0 @@
package dev.sheldan.abstracto.core.commands.utility.model;
import dev.sheldan.abstracto.core.models.UserInitiatedServerContext;
import lombok.Builder;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
@Getter @SuperBuilder
public class PingModel extends UserInitiatedServerContext {
private Long latency;
}

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.DynamicKeyLoader;
import dev.sheldan.abstracto.core.exception.ConfigurationException;
import dev.sheldan.abstracto.core.management.PostTargetManagement;
import dev.sheldan.abstracto.core.management.ServerManagementService;
@@ -12,6 +13,7 @@ import net.dv8tion.jda.api.entities.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@@ -28,6 +30,9 @@ public class PostTargetServiceBean implements PostTargetService {
@Autowired
private Bot botService;
@Autowired
private DynamicKeyLoader dynamicKeyLoader;
@Override
public CompletableFuture<Message> sendTextInPostTarget(String text, PostTarget target) {
TextChannel textChannelForPostTarget = getTextChannelForPostTarget(target);
@@ -77,4 +82,15 @@ public class PostTargetServiceBean implements PostTargetService {
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);
return this.sendEmbedInPostTarget(embed, postTarget);
}
@Override
public boolean validPostTarget(String name) {
List<String> possiblePostTargets = dynamicKeyLoader.getPostTargetsAsList();
return possiblePostTargets.contains(name);
}
@Override
public List<String> getAvailablePostTargets() {
return dynamicKeyLoader.getPostTargetsAsList();
}
}

View File

@@ -8,6 +8,7 @@ import dev.sheldan.abstracto.core.models.database.PostTarget;
import dev.sheldan.abstracto.core.management.ChannelManagementService;
import dev.sheldan.abstracto.core.management.PostTargetManagement;
import dev.sheldan.abstracto.core.management.ServerManagementService;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.repository.PostTargetRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,11 +32,13 @@ public class PostTargetManagementBean implements PostTargetManagement {
@Autowired
private DynamicKeyLoader dynamicKeyLoader;
@Autowired
private PostTargetService postTargetService;
@Override
public void createPostTarget(String name, AServer server, AChannel targetChannel) {
List<String> possiblePostTargets = dynamicKeyLoader.getPostTargetsAsList();
if(!possiblePostTargets.contains(name)) {
throw new ConfigurationException("PostTarget not found. Possible values are: " + String.join(", ", possiblePostTargets));
if(!postTargetService.validPostTarget(name)) {
throw new ConfigurationException("PostTarget not found. Possible values are: " + String.join(", ", dynamicKeyLoader.getPostTargetsAsList()));
}
log.info("Creating post target {} pointing towards {}", name, targetChannel);
postTargetRepository.save(PostTarget.builder().name(name).channelReference(targetChannel).serverReference(server).build());

View File

@@ -0,0 +1 @@
Sets the target of the given posts to a channel.

View File

@@ -0,0 +1 @@
The provided post target is invalid. <#include "posttarget_no_target">

View File

@@ -0,0 +1,2 @@
Sets the target of the given posts to a channel. You have to mention the channel with #.
The available post targets will be printed when executed with no parameters.

View File

@@ -0,0 +1 @@
The available post targets are: ${validPostTargets?join(", ")}