mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-24 13:44:33 +00:00
added default value handling to setup wizard
added table to store the default values of the whole system, to not require the property files added ability to cancel setup wizard
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
package dev.sheldan.abstracto.core.interactive;
|
||||
|
||||
import dev.sheldan.abstracto.core.exception.SetupStepException;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.templating.service.TemplateService;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@@ -35,4 +31,12 @@ public abstract class AbstractConfigSetupStep implements SetupStep {
|
||||
interactiveUtils.sendTimeoutMessage(serverId, channelId);
|
||||
};
|
||||
}
|
||||
|
||||
protected boolean checkForExit(Message message) {
|
||||
return message.getContentRaw().trim().equalsIgnoreCase("exit");
|
||||
}
|
||||
|
||||
protected boolean checkForKeep(Message message) {
|
||||
return message.getContentRaw().trim().equalsIgnoreCase("default");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,9 +51,8 @@ public class InteractiveServiceBean implements InteractiveService {
|
||||
private BotService botService;
|
||||
|
||||
@Override
|
||||
public void createMessageWithResponse(String templateKey, AUserInAServer responder, AChannel channel, Long messageId, Consumer<MessageReceivedEvent> action, Runnable finalAction) {
|
||||
String message = templateService.renderSimpleTemplate(templateKey);
|
||||
channelService.sendTextToAChannel(message, channel);
|
||||
public void createMessageWithResponse(String messageText, AUserInAServer responder, AChannel channel, Long messageId, Consumer<MessageReceivedEvent> action, Runnable finalAction) {
|
||||
channelService.sendTextToAChannel(messageText, channel);
|
||||
eventWaiter.waitForEvent(MessageReceivedEvent.class, event -> {
|
||||
if(event != null) {
|
||||
return event.getAuthor().getIdLong() == responder.getUserReference().getId() && event.getMessage().getIdLong() != messageId;
|
||||
|
||||
@@ -7,7 +7,9 @@ import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.service.ConfigService;
|
||||
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.templating.service.TemplateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -38,36 +40,53 @@ public class PostTargetSetupStep extends AbstractConfigSetupStep {
|
||||
@Autowired
|
||||
private PostTargetSetupStep self;
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<DelayedActionConfig>> execute(AServerChannelUserId user, SetupStepParameter parameter) {
|
||||
public CompletableFuture<SetupStepResult> execute(AServerChannelUserId user, SetupStepParameter parameter) {
|
||||
PostTargetStepParameter systemConfigStepParameter = (PostTargetStepParameter) parameter;
|
||||
String messageTemplateKey = "setup_posttarget_" + systemConfigStepParameter.getPostTargetKey();
|
||||
String messageText = templateService.renderSimpleTemplate(messageTemplateKey);
|
||||
Optional<AChannel> channel = channelManagementService.loadChannel(user.getChannelId());
|
||||
CompletableFuture<List<DelayedActionConfig>> future = new CompletableFuture<>();
|
||||
CompletableFuture<SetupStepResult> future = new CompletableFuture<>();
|
||||
AUserInAServer aUserInAServer = userInServerManagementService.loadUser(user.getGuildId(), user.getUserId());
|
||||
if(channel.isPresent()) {
|
||||
Runnable finalAction = super.getTimeoutRunnable(user.getGuildId(), user.getChannelId());
|
||||
Consumer<MessageReceivedEvent> configAction = (MessageReceivedEvent event) -> {
|
||||
try {
|
||||
if(event.getMessage().getMentionedChannels().size() == 0) {
|
||||
future.completeExceptionally(new RuntimeException());
|
||||
|
||||
SetupStepResult result;
|
||||
Message message = event.getMessage();
|
||||
if(checkForExit(message)) {
|
||||
result = SetupStepResult.fromCancelled();
|
||||
} else {
|
||||
if(message.getMentionedChannels().size() == 0) {
|
||||
future.completeExceptionally(new RuntimeException());
|
||||
}
|
||||
TextChannel textChannel = message.getMentionedChannels().get(0);
|
||||
PostTargetDelayedActionConfig build = PostTargetDelayedActionConfig
|
||||
.builder()
|
||||
.postTargetKey(systemConfigStepParameter.getPostTargetKey())
|
||||
.serverId(user.getGuildId())
|
||||
.textChannel(textChannel)
|
||||
.channelId(textChannel.getIdLong())
|
||||
.build();
|
||||
List<DelayedActionConfig> delayedSteps = Arrays.asList(build);
|
||||
result = SetupStepResult
|
||||
.builder()
|
||||
.result(SetupStepResultType.SUCCESS)
|
||||
.delayedActionConfigList(delayedSteps)
|
||||
.build();
|
||||
}
|
||||
TextChannel textChannel = event.getMessage().getMentionedChannels().get(0);
|
||||
PostTargetDelayedActionConfig build = PostTargetDelayedActionConfig
|
||||
.builder()
|
||||
.postTargetKey(systemConfigStepParameter.getPostTargetKey())
|
||||
.serverId(user.getGuildId())
|
||||
.textChannel(textChannel)
|
||||
.channelId(textChannel.getIdLong())
|
||||
.build();
|
||||
List<DelayedActionConfig> delayedSteps = Arrays.asList(build);
|
||||
future.complete(delayedSteps);
|
||||
|
||||
future.complete(result);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to handle post target step.", e);
|
||||
future.completeExceptionally(e);
|
||||
}
|
||||
};
|
||||
interactiveService.createMessageWithResponse(messageTemplateKey, aUserInAServer, channel.get(), parameter.getPreviousMessageId(), configAction, finalAction);
|
||||
interactiveService.createMessageWithResponse(messageText, aUserInAServer, channel.get(), parameter.getPreviousMessageId(), configAction, finalAction);
|
||||
} else {
|
||||
future.completeExceptionally(new ChannelNotFoundException(user.getGuildId(), user.getChannelId()));
|
||||
}
|
||||
|
||||
@@ -8,15 +8,12 @@ import dev.sheldan.abstracto.core.models.template.commands.SetupSummaryModel;
|
||||
import dev.sheldan.abstracto.core.service.DelayedActionService;
|
||||
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.templating.service.TemplateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -45,7 +42,7 @@ public class SetupSummaryStep extends AbstractConfigSetupStep {
|
||||
private SetupSummaryStep self;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<DelayedActionConfig>> execute(AServerChannelUserId user, SetupStepParameter generalParameter) {
|
||||
public CompletableFuture<SetupStepResult> execute(AServerChannelUserId user, SetupStepParameter generalParameter) {
|
||||
SetupSummaryStepParameter parameter = (SetupSummaryStepParameter) generalParameter;
|
||||
SetupSummaryModel model = SetupSummaryModel
|
||||
.builder()
|
||||
@@ -53,21 +50,29 @@ public class SetupSummaryStep extends AbstractConfigSetupStep {
|
||||
.build();
|
||||
String messageToSend = templateService.renderTemplate("setup_confirmation", model);
|
||||
Optional<AChannel> channel = channelManagementService.loadChannel(user.getChannelId());
|
||||
CompletableFuture<List<DelayedActionConfig>> future = new CompletableFuture<>();
|
||||
CompletableFuture<SetupStepResult> future = new CompletableFuture<>();
|
||||
AUserInAServer aUserInAServer = userInServerManagementService.loadUser(user.getGuildId(), user.getUserId());
|
||||
if(channel.isPresent()) {
|
||||
Runnable finalAction = super.getTimeoutRunnable(user.getGuildId(), user.getChannelId());
|
||||
Consumer<Void> confirmation = (Void none) -> {
|
||||
try {
|
||||
self.executeDelayedSteps(parameter);
|
||||
future.complete(null);
|
||||
SetupStepResult result = SetupStepResult
|
||||
.builder()
|
||||
.result(SetupStepResultType.SUCCESS)
|
||||
.build();
|
||||
future.complete(result);
|
||||
} catch (Exception e) {
|
||||
future.completeExceptionally(e);
|
||||
}
|
||||
};
|
||||
|
||||
Consumer<Void> denial = (Void none) -> {
|
||||
log.info("Stopped wizard.");
|
||||
SetupStepResult result = SetupStepResult
|
||||
.builder()
|
||||
.result(SetupStepResultType.CANCELLED)
|
||||
.build();
|
||||
future.complete(result);
|
||||
};
|
||||
interactiveService.createMessageWithConfirmation(messageToSend, aUserInAServer, channel.get(), parameter.getPreviousMessageId(), confirmation, denial, finalAction);
|
||||
} else {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.core.interactive;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.database.AConfig;
|
||||
import dev.sheldan.abstracto.core.models.template.commands.SystemConfigActionModel;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
@@ -11,7 +12,7 @@ import lombok.Setter;
|
||||
public class SystemConfigDelayedActionConfig implements DelayedActionConfig {
|
||||
private String configKey;
|
||||
private Long serverId;
|
||||
private String value;
|
||||
private AConfig value;
|
||||
|
||||
@Override
|
||||
public String getTemplateName() {
|
||||
@@ -23,7 +24,7 @@ public class SystemConfigDelayedActionConfig implements DelayedActionConfig {
|
||||
return SystemConfigActionModel
|
||||
.builder()
|
||||
.configKey(this.configKey)
|
||||
.newValue(this.value)
|
||||
.newValue(value.getValueAsString())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,17 @@ package dev.sheldan.abstracto.core.interactive;
|
||||
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
|
||||
import dev.sheldan.abstracto.core.models.AServerChannelUserId;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.database.AConfig;
|
||||
import dev.sheldan.abstracto.core.models.database.ADefaultConfig;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.models.template.commands.SetupSystemConfigMessageModel;
|
||||
import dev.sheldan.abstracto.core.service.ConfigService;
|
||||
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.templating.service.TemplateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -38,31 +44,61 @@ public class SystemConfigSetupStep extends AbstractConfigSetupStep {
|
||||
@Autowired
|
||||
private SystemConfigSetupStep self;
|
||||
|
||||
@Autowired
|
||||
private DefaultConfigManagementService defaultConfigManagementService;
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<DelayedActionConfig>> execute(AServerChannelUserId user, SetupStepParameter parameter) {
|
||||
public CompletableFuture<SetupStepResult> execute(AServerChannelUserId user, SetupStepParameter parameter) {
|
||||
SystemConfigStepParameter systemConfigStepParameter = (SystemConfigStepParameter) parameter;
|
||||
String messageTemplateKey = "setup_config_" + systemConfigStepParameter.getConfigKey();
|
||||
ADefaultConfig defaultConfig = defaultConfigManagementService.getDefaultConfig(systemConfigStepParameter.getConfigKey());
|
||||
SetupSystemConfigMessageModel model = SetupSystemConfigMessageModel
|
||||
.builder()
|
||||
.configKey(systemConfigStepParameter.getConfigKey())
|
||||
.defaultConfig(defaultConfig)
|
||||
.build();
|
||||
String messageTemplateKey = "setup_system_config_message";
|
||||
String messageText = templateService.renderTemplate(messageTemplateKey, model);
|
||||
Optional<AChannel> channel = channelManagementService.loadChannel(user.getChannelId());
|
||||
CompletableFuture<List<DelayedActionConfig>> future = new CompletableFuture<>();
|
||||
CompletableFuture<SetupStepResult> future = new CompletableFuture<>();
|
||||
AUserInAServer aUserInAServer = userInServerManagementService.loadUser(user.getGuildId(), user.getUserId());
|
||||
if(channel.isPresent()) {
|
||||
Runnable finalAction = super.getTimeoutRunnable(user.getGuildId(), user.getChannelId());
|
||||
Consumer<MessageReceivedEvent> configAction = (MessageReceivedEvent event) -> {
|
||||
try {
|
||||
self.checkValidity(user, systemConfigStepParameter, event);
|
||||
SystemConfigDelayedActionConfig build = SystemConfigDelayedActionConfig
|
||||
.builder()
|
||||
.configKey(systemConfigStepParameter.getConfigKey())
|
||||
.serverId(user.getGuildId())
|
||||
.value(event.getMessage().getContentRaw())
|
||||
.build();
|
||||
List<DelayedActionConfig> delayedSteps = Arrays.asList(build);
|
||||
future.complete(delayedSteps);
|
||||
SetupStepResult result;
|
||||
Message message = event.getMessage();
|
||||
if(checkForExit(message)) {
|
||||
result = SetupStepResult.fromCancelled();
|
||||
} else {
|
||||
AConfig config;
|
||||
if(checkForKeep(message)) {
|
||||
config = self.loadDefaultConfig(systemConfigStepParameter);
|
||||
} else {
|
||||
config = self.checkValidity(user, systemConfigStepParameter, event);
|
||||
}
|
||||
SystemConfigDelayedActionConfig build = SystemConfigDelayedActionConfig
|
||||
.builder()
|
||||
.configKey(systemConfigStepParameter.getConfigKey())
|
||||
.serverId(user.getGuildId())
|
||||
.value(config)
|
||||
.build();
|
||||
List<DelayedActionConfig> delayedSteps = Arrays.asList(build);
|
||||
result = SetupStepResult
|
||||
.builder()
|
||||
.result(SetupStepResultType.SUCCESS)
|
||||
.delayedActionConfigList(delayedSteps)
|
||||
.build();
|
||||
}
|
||||
future.complete(result);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to handle system config. Retrying..", e);
|
||||
future.completeExceptionally(e);
|
||||
}
|
||||
};
|
||||
interactiveService.createMessageWithResponse(messageTemplateKey, aUserInAServer, channel.get(), parameter.getPreviousMessageId(), configAction, finalAction);
|
||||
interactiveService.createMessageWithResponse(messageText, aUserInAServer, channel.get(), parameter.getPreviousMessageId(), configAction, finalAction);
|
||||
} else {
|
||||
future.completeExceptionally(new ChannelNotFoundException(user.getGuildId(), user.getChannelId()));
|
||||
}
|
||||
@@ -70,8 +106,22 @@ public class SystemConfigSetupStep extends AbstractConfigSetupStep {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void checkValidity(AServerChannelUserId user, SystemConfigStepParameter systemConfigStepParameter, MessageReceivedEvent event) {
|
||||
configService.validateConfig(systemConfigStepParameter.getConfigKey(), user.getGuildId(), event.getMessage().getContentRaw());
|
||||
public AConfig loadDefaultConfig(SystemConfigStepParameter systemConfigStepParameter) {
|
||||
AConfig config;
|
||||
ADefaultConfig defaultConfig = defaultConfigManagementService.getDefaultConfig(systemConfigStepParameter.getConfigKey());
|
||||
config = AConfig
|
||||
.builder()
|
||||
.name(defaultConfig.getName())
|
||||
.doubleValue(defaultConfig.getDoubleValue())
|
||||
.longValue(defaultConfig.getLongValue())
|
||||
.stringValue(defaultConfig.getStringValue())
|
||||
.build();
|
||||
return config;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public AConfig checkValidity(AServerChannelUserId user, SystemConfigStepParameter systemConfigStepParameter, MessageReceivedEvent event) {
|
||||
return configService.getFakeConfigForValue(systemConfigStepParameter.getConfigKey(), user.getGuildId(), event.getMessage().getContentRaw());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package dev.sheldan.abstracto.core.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Component
|
||||
public class CoreDefaultConfigListener {
|
||||
|
||||
@Autowired
|
||||
private DefaultConfigManagementService defaultConfigManagementService;
|
||||
|
||||
|
||||
@Value("${abstracto.prefix}")
|
||||
private String prefix;
|
||||
|
||||
@EventListener
|
||||
@Transactional
|
||||
public void handleContextRefreshEvent(ContextRefreshedEvent ctxStartEvt) {
|
||||
defaultConfigManagementService.createDefaultConfig("prefix", prefix);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package dev.sheldan.abstracto.core.repository;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.database.ADefaultConfig;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface DefaultConfigRepository extends JpaRepository<ADefaultConfig, Long> {
|
||||
ADefaultConfig findByName(String name);
|
||||
boolean existsByName(String name);
|
||||
}
|
||||
@@ -84,6 +84,17 @@ public class ConfigServiceBean implements ConfigService{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setConfigValue(String name, Long serverId, AConfig value) {
|
||||
if(value.getDoubleValue() != null) {
|
||||
setDoubleValue(name, serverId, value.getDoubleValue());
|
||||
} else if(value.getLongValue() != null) {
|
||||
setLongValue(name, serverId, value.getLongValue());
|
||||
} else {
|
||||
setStringValue(name, serverId, value.getStringValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStringValue(String name, Long serverId, String value) {
|
||||
if(configManagementService.configExists(serverId, name)) {
|
||||
@@ -96,7 +107,7 @@ public class ConfigServiceBean implements ConfigService{
|
||||
@Override
|
||||
public boolean configIsFitting(String name, Long serverId, String value) {
|
||||
try {
|
||||
validateConfig(name, serverId, value);
|
||||
getFakeConfigForValue(name, serverId, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
@@ -104,14 +115,18 @@ public class ConfigServiceBean implements ConfigService{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validateConfig(String name, Long serverId, String value) {
|
||||
public AConfig getFakeConfigForValue(String name, Long serverId, String value) {
|
||||
if(configManagementService.configExists(serverId, name)) {
|
||||
AConfig newConfig = AConfig.builder().name(value).build();
|
||||
AConfig existing = configManagementService.loadConfig(serverId, name);
|
||||
if(existing.getDoubleValue() != null) {
|
||||
Double.parseDouble(value);
|
||||
newConfig.setDoubleValue(Double.parseDouble(value));
|
||||
} else if(existing.getLongValue() != null) {
|
||||
Long.parseLong(value);
|
||||
newConfig.setLongValue(Long.parseLong(value));
|
||||
} else {
|
||||
newConfig.setStringValue(value);
|
||||
}
|
||||
return newConfig;
|
||||
} else {
|
||||
throw new ConfigurationKeyNotFoundException(name);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import dev.sheldan.abstracto.core.config.FeatureConfig;
|
||||
import dev.sheldan.abstracto.core.interactive.*;
|
||||
import dev.sheldan.abstracto.core.models.AServerChannelUserId;
|
||||
import dev.sheldan.abstracto.core.models.template.commands.SetupCompletedNotificationModel;
|
||||
import dev.sheldan.abstracto.core.models.template.commands.SetupInitialMessageModel;
|
||||
import dev.sheldan.abstracto.templating.service.TemplateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
@@ -68,7 +69,17 @@ public class SetupServiceBean implements SetupService {
|
||||
setupExecution.setNextStep(steps.get(i + 1));
|
||||
}
|
||||
}
|
||||
executeSetup(featureConfig, steps, user, new ArrayList<>());
|
||||
|
||||
SetupInitialMessageModel setupInitialMessageModel = SetupInitialMessageModel
|
||||
.builder()
|
||||
.featureConfig(featureConfig)
|
||||
.build();
|
||||
Optional<TextChannel> textChannelInGuild = channelService.getTextChannelInGuild(user.getGuildId(), user.getChannelId());
|
||||
textChannelInGuild.ifPresent(textChannel -> {
|
||||
String text = templateService.renderTemplate("setup_initial_message", setupInitialMessageModel);
|
||||
channelService.sendTextToChannel(text, textChannel);
|
||||
executeSetup(featureConfig, steps, user, new ArrayList<>());
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -79,12 +90,17 @@ public class SetupServiceBean implements SetupService {
|
||||
|
||||
private void executeStep(AServerChannelUserId aUserInAServer, SetupExecution execution, List<DelayedActionConfig> delayedActionConfigs, FeatureConfig featureConfig) {
|
||||
execution.getStep().execute(aUserInAServer, execution.getParameter()).thenAccept(aVoid -> {
|
||||
delayedActionConfigs.addAll(aVoid);
|
||||
if(execution.getNextStep() != null) {
|
||||
executeStep(aUserInAServer, execution.getNextStep(), delayedActionConfigs, featureConfig);
|
||||
if(aVoid.getResult().equals(SetupStepResultType.SUCCESS)) {
|
||||
delayedActionConfigs.addAll(aVoid.getDelayedActionConfigList());
|
||||
if(execution.getNextStep() != null) {
|
||||
executeStep(aUserInAServer, execution.getNextStep(), delayedActionConfigs, featureConfig);
|
||||
} else {
|
||||
self.executePostSetupSteps(delayedActionConfigs, aUserInAServer, execution.getParameter().getPreviousMessageId(), featureConfig);
|
||||
}
|
||||
} else {
|
||||
self.executePostSetupSteps(delayedActionConfigs, aUserInAServer, execution.getParameter().getPreviousMessageId(), featureConfig);
|
||||
self.notifyAboutCancellation(aUserInAServer, featureConfig);
|
||||
}
|
||||
|
||||
}).exceptionally(throwable -> {
|
||||
executeStep(aUserInAServer, execution, delayedActionConfigs, featureConfig);
|
||||
return null;
|
||||
@@ -105,13 +121,21 @@ public class SetupServiceBean implements SetupService {
|
||||
|
||||
@Transactional
|
||||
public void notifyAboutCompletion(AServerChannelUserId aServerChannelUserId, FeatureConfig featureConfig) {
|
||||
String templateName = "setup_completion_notification";
|
||||
notifyUserWithTemplate(aServerChannelUserId, featureConfig, "setup_completion_notification");
|
||||
}
|
||||
|
||||
private void notifyUserWithTemplate(AServerChannelUserId aServerChannelUserId, FeatureConfig featureConfig, String templateName) {
|
||||
SetupCompletedNotificationModel model = SetupCompletedNotificationModel
|
||||
.builder()
|
||||
.completedFeature(featureConfig)
|
||||
.featureConfig(featureConfig)
|
||||
.build();
|
||||
String text = templateService.renderTemplate(templateName, model);
|
||||
Optional<TextChannel> textChannel = channelService.getTextChannelInGuild(aServerChannelUserId.getGuildId(), aServerChannelUserId.getChannelId());
|
||||
textChannel.ifPresent(channel -> channelService.sendTextToChannel(text, channel));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void notifyAboutCancellation(AServerChannelUserId aServerChannelUserId, FeatureConfig featureConfig) {
|
||||
notifyUserWithTemplate(aServerChannelUserId, featureConfig, "setup_cancellation_notification");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package dev.sheldan.abstracto.core.service.management;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.database.ADefaultConfig;
|
||||
import dev.sheldan.abstracto.core.repository.DefaultConfigRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DefaultConfigManagementServiceBean implements DefaultConfigManagementService {
|
||||
|
||||
@Autowired
|
||||
private DefaultConfigRepository defaultConfigRepository;
|
||||
|
||||
@Override
|
||||
public void createDefaultConfig(String key, String value) {
|
||||
ADefaultConfig build;
|
||||
if(defaultConfigRepository.existsByName(key)) {
|
||||
build = defaultConfigRepository.findByName(key);
|
||||
build.setStringValue(value);
|
||||
} else {
|
||||
build = ADefaultConfig
|
||||
.builder()
|
||||
.name(key)
|
||||
.stringValue(value)
|
||||
.build();
|
||||
}
|
||||
defaultConfigRepository.save(build);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createDefaultConfig(String key, Long value) {
|
||||
ADefaultConfig build;
|
||||
if(defaultConfigRepository.existsByName(key)) {
|
||||
build = defaultConfigRepository.findByName(key);
|
||||
build.setLongValue(value);
|
||||
} else {
|
||||
build = ADefaultConfig
|
||||
.builder()
|
||||
.name(key)
|
||||
.longValue(value)
|
||||
.build();
|
||||
}
|
||||
defaultConfigRepository.save(build);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createDefaultConfig(String key, Double value) {
|
||||
ADefaultConfig build;
|
||||
if(defaultConfigRepository.existsByName(key)) {
|
||||
build = defaultConfigRepository.findByName(key);
|
||||
build.setDoubleValue(value);
|
||||
} else {
|
||||
build = ADefaultConfig
|
||||
.builder()
|
||||
.name(key)
|
||||
.doubleValue(value)
|
||||
.build();
|
||||
}
|
||||
defaultConfigRepository.save(build);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ADefaultConfig getDefaultConfig(String key) {
|
||||
return defaultConfigRepository.findByName(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<#assign featureName><#include "${featureConfig.feature.key}_feature"></#assign><#include "setup_cancellation_message">
|
||||
@@ -1 +1 @@
|
||||
<#assign featureName><#include "${completedFeature.feature.key}_feature"></#assign><#include "setup_completion_message">
|
||||
<#assign featureName><#include "${featureConfig.feature.key}_feature"></#assign><#include "setup_completion_message">
|
||||
@@ -0,0 +1 @@
|
||||
<#assign featureName><#include "${featureConfig.feature.key}_feature"></#assign><#include "setup_initial_message_display">
|
||||
@@ -0,0 +1 @@
|
||||
<#assign defaultValue=defaultConfig.valueAsString><#include "setup_config_${configKey}">
|
||||
Reference in New Issue
Block a user