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:
Sheldan
2020-05-22 20:13:24 +02:00
parent 992357b2cb
commit 3714fd2582
37 changed files with 535 additions and 69 deletions

View File

@@ -0,0 +1,26 @@
package dev.sheldan.abstracto.experience.config;
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
import org.springframework.beans.factory.annotation.Autowired;
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 ExperienceDefaultConfigListener {
@Autowired
private DefaultConfigManagementService defaultConfigManagementService;
@Autowired
private ExperienceConfig experienceConfig;
@EventListener
@Transactional
public void handleContextRefreshEvent(ContextRefreshedEvent ctxStartEvt) {
defaultConfigManagementService.createDefaultConfig("minExp", experienceConfig.getMinExp().longValue());
defaultConfigManagementService.createDefaultConfig("maxExp", experienceConfig.getMaxExp().longValue());
defaultConfigManagementService.createDefaultConfig("expMultiplier", experienceConfig.getExpMultiplier().doubleValue());
}
}

View File

@@ -0,0 +1,25 @@
package dev.sheldan.abstracto.moderation.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 ModerationDefaultConfigListener {
@Autowired
private DefaultConfigManagementService defaultConfigManagementService;
@Value("${abstracto.warnings.warnDecay.days}")
private Long decayDays;
@EventListener
@Transactional
public void handleContextRefreshEvent(ContextRefreshedEvent ctxStartEvt) {
defaultConfigManagementService.createDefaultConfig("decayDays", decayDays);
}
}

View File

@@ -0,0 +1,29 @@
package dev.sheldan.abstracto.modmail.listener;
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
import dev.sheldan.abstracto.templating.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import static dev.sheldan.abstracto.modmail.service.ModMailThreadServiceBean.MODMAIL_CLOSING_MESSAGE_TEXT;
@Component
public class ModMailDefaultConfigListener {
@Autowired
private DefaultConfigManagementService defaultConfigManagementService;
@Autowired
private TemplateService templateService;
@EventListener
@Transactional
public void handleContextRefreshEvent(ContextRefreshedEvent ctxStartEvt) {
String text = templateService.renderSimpleTemplate("modmail_closing_user_message_description");
defaultConfigManagementService.createDefaultConfig(MODMAIL_CLOSING_MESSAGE_TEXT, text);
}
}

View File

@@ -0,0 +1,27 @@
package dev.sheldan.abstracto.utility.config;
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
import org.springframework.beans.factory.annotation.Autowired;
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 StarboardDefaultConfigListener {
@Autowired
private DefaultConfigManagementService defaultConfigManagementService;
@Autowired
private StarboardConfig starboardConfig;
@EventListener
@Transactional
public void handleContextRefreshEvent(ContextRefreshedEvent ctxStartEvt) {
for (int i = 0; i < starboardConfig.getLvl().size(); i++) {
Integer value = starboardConfig.getLvl().get(i);
defaultConfigManagementService.createDefaultConfig("starLvl" + ( i + 1 ), Long.valueOf(value));
}
}
}

View File

@@ -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");
}
}

View File

@@ -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;

View File

@@ -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()));
}

View File

@@ -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 {

View File

@@ -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();
}
}

View File

@@ -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());
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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");
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1 @@
<#assign featureName><#include "${featureConfig.feature.key}_feature"></#assign><#include "setup_cancellation_message">

View File

@@ -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">

View File

@@ -0,0 +1 @@
<#assign featureName><#include "${featureConfig.feature.key}_feature"></#assign><#include "setup_initial_message_display">

View File

@@ -0,0 +1 @@
<#assign defaultValue=defaultConfig.valueAsString><#include "setup_config_${configKey}">

View File

@@ -3,9 +3,8 @@ package dev.sheldan.abstracto.core.interactive;
import dev.sheldan.abstracto.core.models.AServerChannelUserId;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public interface SetupStep {
CompletableFuture<List<DelayedActionConfig>> execute(AServerChannelUserId aUserInAServer, SetupStepParameter parameter);
CompletableFuture<SetupStepResult> execute(AServerChannelUserId aUserInAServer, SetupStepParameter parameter);
}

View File

@@ -0,0 +1,19 @@
package dev.sheldan.abstracto.core.interactive;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
@Builder
public class SetupStepResult {
private List<DelayedActionConfig> delayedActionConfigList;
private SetupStepResultType result;
public static SetupStepResult fromCancelled() {
return SetupStepResult.builder().result(SetupStepResultType.CANCELLED).build();
}
}

View File

@@ -0,0 +1,5 @@
package dev.sheldan.abstracto.core.interactive;
public enum SetupStepResultType {
SUCCESS, CANCELLED
}

View File

@@ -58,6 +58,17 @@ public class AConfig {
this.updated = Instant.now();
}
public String getValueAsString() {
if(getLongValue() != null) {
return getLongValue().toString();
} else if(getDoubleValue() != null) {
return getDoubleValue().toString();
} else if(getStringValue() != null) {
return getStringValue();
}
return null;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@@ -0,0 +1,60 @@
package dev.sheldan.abstracto.core.models.database;
import lombok.*;
import javax.persistence.*;
import java.time.Instant;
@Entity
@Table(name="default_configs")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class ADefaultConfig {
@javax.persistence.Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer Id;
@Column
private String name;
@Column
@Setter
private String stringValue;
@Column
@Setter
private Double doubleValue;
@Column
@Setter
private Long longValue;
@Column(name = "created")
private Instant created;
@PrePersist
private void onInsert() {
this.created = Instant.now();
}
@Column(name = "updated")
private Instant updated;
@PreUpdate
private void onUpdate() {
this.updated = Instant.now();
}
public String getValueAsString() {
if(getLongValue() != null) {
return getLongValue().toString();
} else if(getDoubleValue() != null) {
return getDoubleValue().toString();
} else if(getStringValue() != null) {
return getStringValue();
}
return null;
}
}

View File

@@ -9,5 +9,5 @@ import lombok.Setter;
@Setter
@Builder
public class SetupCompletedNotificationModel {
private FeatureConfig completedFeature;
private FeatureConfig featureConfig;
}

View File

@@ -0,0 +1,13 @@
package dev.sheldan.abstracto.core.models.template.commands;
import dev.sheldan.abstracto.core.config.FeatureConfig;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Builder
public class SetupInitialMessageModel {
private FeatureConfig featureConfig;
}

View File

@@ -0,0 +1,14 @@
package dev.sheldan.abstracto.core.models.template.commands;
import dev.sheldan.abstracto.core.models.database.ADefaultConfig;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Builder
public class SetupSystemConfigMessageModel {
private String configKey;
private ADefaultConfig defaultConfig;
}

View File

@@ -1,5 +1,7 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.models.database.AConfig;
public interface ConfigService {
Double getDoubleValue(String name, Long serverId);
Long getLongValue(String name, Long serverId);
@@ -9,8 +11,9 @@ public interface ConfigService {
void setDoubleValue(String name, Long serverId, Double value);
void setLongValue(String name, Long serverId, Long value);
void setConfigValue(String name, Long serverId, String value);
void setConfigValue(String name, Long serverId, AConfig value);
void setStringValue(String name, Long serverId, String value);
boolean configIsFitting(String name, Long serverId, String value);
void validateConfig(String name, Long serverId, String value);
AConfig getFakeConfigForValue(String name, Long serverId, String value);
}

View File

@@ -0,0 +1,10 @@
package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.models.database.ADefaultConfig;
public interface DefaultConfigManagementService {
void createDefaultConfig(String key, String value);
void createDefaultConfig(String key, Long value);
void createDefaultConfig(String key, Double value);
ADefaultConfig getDefaultConfig(String key);
}

View File

@@ -0,0 +1 @@
Setup for ${featureName} has been cancelled.

View File

@@ -1 +1 @@
${featureName} has been successfully setup.
Setup for ${featureName} has been completed successfully.

View File

@@ -0,0 +1 @@
Starting setup of feature ${featureName}. Type 'exit' to stop or 'default' to keep the proposed default value.

View File

@@ -1 +1 @@
The value each gained experience is multiplied by. Default 1.0
The value each gained experience is multiplied by. Default: ${defaultValue}

View File

@@ -1 +1 @@
The maximum of experience given randomly. Default: 25
The maximum of experience given randomly. Default: ${defaultValue}

View File

@@ -1 +1 @@
The minimum experience given randomly. Default: 10
The minimum experience given randomly. Default: ${defaultValue}

View File

@@ -1 +1 @@
The amount of days after which a warning is decayed.
The amount of days after which a warning is decayed. Default: ${defaultValue}

View File

@@ -1 +1 @@
The text to close mod mail threads with.
The text to close mod mail threads with. Default: `${defaultValue}`