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

@@ -0,0 +1,53 @@
package dev.sheldan.abstracto.utility.command;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.HelpInfo;
import dev.sheldan.abstracto.command.execution.*;
import dev.sheldan.abstracto.templating.TemplateService;
import dev.sheldan.abstracto.utility.Utility;
import dev.sheldan.abstracto.utility.models.template.ShowEmoteLog;
import net.dv8tion.jda.api.entities.Emote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class ShowEmote implements Command {
private static final String SHOW_EMOTE_RESPONSE_TEMPLATE = "showEmote_response";
@Autowired
private TemplateService templateService;
@Override
public Result execute(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
Object emoteParameter = parameters.get(0);
if(!(emoteParameter instanceof Emote)) {
return Result.fromError("No custom emote found.");
}
Emote emote = (Emote) emoteParameter;
ShowEmoteLog emoteLog = (ShowEmoteLog) ContextConverter.fromCommandContext(commandContext, ShowEmoteLog.class);
emoteLog.setEmote(emote);
String message = templateService.renderTemplate(SHOW_EMOTE_RESPONSE_TEMPLATE, emoteLog);
commandContext.getChannel().sendMessage(message).queue();
return Result.fromSuccess();
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("emote").type(Emote.class).optional(false).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("showEmote")
.module(Utility.UTILITY)
.templated(true)
.causesReaction(false)
.parameters(parameters)
.help(helpInfo)
.build();
}
}

View File

@@ -0,0 +1 @@
**Name**: ${emote.name} **Link**: ${emote.imageUrl}

View File

@@ -0,0 +1,14 @@
package dev.sheldan.abstracto.utility.models.template;
import dev.sheldan.abstracto.core.models.UserInitiatedServerContext;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import net.dv8tion.jda.api.entities.Emote;
@Getter
@Setter
@SuperBuilder
public class ShowEmoteLog extends UserInitiatedServerContext {
private Emote emote;
}

View File

@@ -7,7 +7,9 @@ import lombok.Setter;
import lombok.experimental.SuperBuilder;
import net.dv8tion.jda.api.entities.Member;
@Getter @Setter @SuperBuilder
@Getter
@Setter
@SuperBuilder
public class SuggestionLog extends UserInitiatedServerContext {
private Suggestion suggestion;
private Member suggester;

View File

@@ -13,6 +13,9 @@ public class ReactionPostExecution implements PostCommandExecution {
public void execute(CommandContext commandContext, Result result, Command command) {
if(result.getResult().equals(ResultState.ERROR)) {
commandContext.getMessage().addReaction("⚠️").queue();
if(result.getMessage() != null && result.getThrowable() == null){
commandContext.getChannel().sendMessage(result.getMessage()).queue();
}
} else {
if(command.getConfiguration().isCausesReaction()){
commandContext.getMessage().addReaction("").queue();

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,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(", ")}

View File

@@ -1,8 +1,7 @@
package dev.sheldan.abstracto.core.commands.utility.model;
package dev.sheldan.abstracto.core.models.command;
import dev.sheldan.abstracto.core.models.UserInitiatedServerContext;
import lombok.Builder;
import lombok.Getter;
import lombok.experimental.SuperBuilder;

View File

@@ -1,7 +1,6 @@
package dev.sheldan.abstracto.core.commands.utility.model;
package dev.sheldan.abstracto.core.models.command;
import dev.sheldan.abstracto.core.models.UserInitiatedServerContext;
import lombok.Builder;
import lombok.Getter;
import lombok.experimental.SuperBuilder;

View File

@@ -0,0 +1,15 @@
package dev.sheldan.abstracto.core.models.command;
import dev.sheldan.abstracto.core.models.UserInitiatedServerContext;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import java.util.List;
@Setter
@Getter
@SuperBuilder
public class PostTargetErrorModel extends UserInitiatedServerContext {
private List<String> validPostTargets;
}

View File

@@ -4,6 +4,7 @@ import dev.sheldan.abstracto.core.models.database.PostTarget;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public interface PostTargetService {
@@ -11,4 +12,6 @@ public interface PostTargetService {
CompletableFuture<Message> sendEmbedInPostTarget(MessageEmbed embed, PostTarget target);
CompletableFuture<Message> sendTextInPostTarget(String text, String postTargetName, Long serverId);
CompletableFuture<Message> sendEmbedInPostTarget(MessageEmbed embed, String postTargetName, Long serverId);
boolean validPostTarget(String name);
List<String> getAvailablePostTargets();
}