mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-26 13:46:19 +00:00
added overview to existing post targets if no parameter is provided for the post target command
This commit is contained in:
@@ -7,7 +7,12 @@ import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||
import dev.sheldan.abstracto.core.command.execution.*;
|
||||
import dev.sheldan.abstracto.core.config.FeatureEnum;
|
||||
import dev.sheldan.abstracto.core.command.config.features.CoreFeatures;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.models.database.PostTarget;
|
||||
import dev.sheldan.abstracto.core.models.template.commands.PostTargetDisplayModel;
|
||||
import dev.sheldan.abstracto.core.models.template.commands.PostTargetErrorModel;
|
||||
import dev.sheldan.abstracto.core.models.template.commands.PostTargetModelEntry;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.core.service.PostTargetService;
|
||||
import dev.sheldan.abstracto.core.service.management.PostTargetManagement;
|
||||
import dev.sheldan.abstracto.templating.service.TemplateService;
|
||||
@@ -18,14 +23,16 @@ import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class PostTarget extends AbstractConditionableCommand {
|
||||
public class PostTargetCommand extends AbstractConditionableCommand {
|
||||
|
||||
public static final String POST_TARGET_NO_TARGET_TEMPLATE = "posttarget_no_target";
|
||||
public static final String POST_TARGET_SHOW_TARGETS = "posttarget_show_targets";
|
||||
public static final String POST_TARGET_INVALID_TARGET_TEMPLATE = "posttarget_invalid_target";
|
||||
|
||||
@Autowired
|
||||
@@ -37,13 +44,32 @@ public class PostTarget extends AbstractConditionableCommand {
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Autowired
|
||||
private ChannelService channelService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
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 CommandResult.fromError(errorMessage);
|
||||
PostTargetDisplayModel posttargetDisplayModel = (PostTargetDisplayModel) ContextConverter.fromCommandContext(commandContext, PostTargetDisplayModel.class);
|
||||
AServer server = commandContext.getUserInitiatedContext().getServer();
|
||||
List<PostTarget> postTargets = postTargetService.getPostTargets(server);
|
||||
posttargetDisplayModel.setPostTargets(new ArrayList<>());
|
||||
List<PostTargetModelEntry> postTargetEntries = posttargetDisplayModel.getPostTargets();
|
||||
postTargets.forEach(target -> {
|
||||
Optional<TextChannel> channelFromAChannel = channelService.getChannelFromAChannel(target.getChannelReference());
|
||||
PostTargetModelEntry targetEntry = PostTargetModelEntry.builder().channel(channelFromAChannel.orElse(null)).postTarget(target).build();
|
||||
postTargetEntries.add(targetEntry);
|
||||
});
|
||||
List<String> postTargetConfigs = postTargetService.getPostTargetsOfEnabledFeatures(server);
|
||||
postTargetConfigs.forEach(postTargetName -> {
|
||||
if(postTargetEntries.stream().noneMatch(postTargetModelEntry -> postTargetModelEntry.getPostTarget().getName().equalsIgnoreCase(postTargetName))) {
|
||||
PostTarget fakeEntry = PostTarget.builder().name(postTargetName).build();
|
||||
PostTargetModelEntry postTargetEntry = PostTargetModelEntry.builder().postTarget(fakeEntry).build();
|
||||
postTargetEntries.add(postTargetEntry);
|
||||
}
|
||||
});
|
||||
channelService.sendEmbedTemplateInChannel(POST_TARGET_SHOW_TARGETS, posttargetDisplayModel, commandContext.getChannel());
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
String targetName = (String) commandContext.getParameters().getParameters().get(0);
|
||||
if(!postTargetService.validPostTarget(targetName)) {
|
||||
@@ -7,6 +7,7 @@ import org.springframework.data.jpa.repository.QueryHints;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.QueryHint;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface PostTargetRepository extends JpaRepository<PostTarget, Long> {
|
||||
@@ -16,4 +17,6 @@ public interface PostTargetRepository extends JpaRepository<PostTarget, Long> {
|
||||
|
||||
boolean existsByNameAndServerReference(String name, AServer server);
|
||||
|
||||
List<PostTarget> findByServerReference(AServer server);
|
||||
|
||||
}
|
||||
|
||||
@@ -200,4 +200,9 @@ public class ChannelServiceBean implements ChannelService {
|
||||
}
|
||||
throw new GuildException(server.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<TextChannel> getChannelFromAChannel(AChannel channel) {
|
||||
return botService.getTextChannelFromServer(channel.getServer().getId(), channel.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.config.DynamicKeyLoader;
|
||||
import dev.sheldan.abstracto.core.config.FeatureConfig;
|
||||
import dev.sheldan.abstracto.core.config.PostTargetEnum;
|
||||
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
|
||||
import dev.sheldan.abstracto.core.exception.GuildException;
|
||||
import dev.sheldan.abstracto.core.exception.PostTargetNotFoundException;
|
||||
import dev.sheldan.abstracto.core.exception.PostTargetNotValidException;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.service.management.PostTargetManagement;
|
||||
import dev.sheldan.abstracto.core.models.database.PostTarget;
|
||||
import dev.sheldan.abstracto.templating.model.MessageToSend;
|
||||
@@ -18,6 +20,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -39,6 +42,12 @@ public class PostTargetServiceBean implements PostTargetService {
|
||||
@Autowired
|
||||
private ChannelService channelService;
|
||||
|
||||
@Autowired
|
||||
private FeatureFlagService featureFlagService;
|
||||
|
||||
@Autowired
|
||||
private FeatureConfigService featureConfigService;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Message> sendTextInPostTarget(String text, PostTarget target) {
|
||||
return channelService.sendTextToAChannel(text, target.getChannelReference());
|
||||
@@ -189,8 +198,25 @@ public class PostTargetServiceBean implements PostTargetService {
|
||||
return possiblePostTargets.contains(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PostTarget> getPostTargets(AServer server) {
|
||||
return postTargetManagement.getPostTargetsInServer(server);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAvailablePostTargets() {
|
||||
return dynamicKeyLoader.getPostTargetsAsList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getPostTargetsOfEnabledFeatures(AServer server) {
|
||||
List<String> postTargets = new ArrayList<>();
|
||||
List<FeatureConfig> allFeatureConfigs = featureConfigService.getAllFeatureConfigs();
|
||||
allFeatureConfigs.forEach(featureConfig -> {
|
||||
if(featureFlagService.isFeatureEnabled(featureConfig, server)) {
|
||||
featureConfig.getRequiredPostTargets().forEach(postTargetEnum -> postTargets.add(postTargetEnum.getKey()));
|
||||
}
|
||||
});
|
||||
return postTargets;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@@ -97,4 +98,9 @@ public class PostTargetManagementBean implements PostTargetManagement {
|
||||
return target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PostTarget> getPostTargetsInServer(AServer server) {
|
||||
return postTargetRepository.findByServerReference(server);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
<#include "posttarget_available_post_targets">: ${validPostTargets?join(", ")}.
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"title": {
|
||||
"title": "<#include "posttarget_embed_title">""
|
||||
},
|
||||
"color" : {
|
||||
"r": 200,
|
||||
"g": 0,
|
||||
"b": 255
|
||||
},
|
||||
"description": "
|
||||
<#list postTargets as postTarget>
|
||||
<#assign postTargetName>${postTarget.postTarget.name}</#assign>
|
||||
<#assign channelMention><#if postTarget.channel?has_content>${postTarget.channel.asMention}<#else><#include "posttarget_no_channel"></#if></#assign>
|
||||
<#include "posttarget_post_target_text">
|
||||
<#else>
|
||||
<#include "posttarget_no_post_targets_found">
|
||||
</#list>
|
||||
"
|
||||
}
|
||||
Reference in New Issue
Block a user