mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-17 04:29:13 +00:00
upgraded spring boot starter version
added gson dependency added logging the exception in the exception post executor added configuration exception in case the configuration for posttargets is not correct added support to send embeds to posttargets changed interface to be a warnlog instead of a server context for warning users, because the template changed freemarker Incompatible Improvements to support default methods in interfaces added ability to define embed configurations to be used, in order for a template to only be configurable as a template changed template content size to 4000 characters
This commit is contained in:
@@ -2,9 +2,7 @@ package dev.sheldan.abstracto.moderation.command;
|
|||||||
|
|
||||||
import dev.sheldan.abstracto.command.execution.*;
|
import dev.sheldan.abstracto.command.execution.*;
|
||||||
import dev.sheldan.abstracto.moderation.Moderation;
|
import dev.sheldan.abstracto.moderation.Moderation;
|
||||||
import dev.sheldan.abstracto.moderation.models.template.BanIdLog;
|
|
||||||
import dev.sheldan.abstracto.moderation.models.template.WarnLog;
|
import dev.sheldan.abstracto.moderation.models.template.WarnLog;
|
||||||
import dev.sheldan.abstracto.moderation.models.Warning;
|
|
||||||
import dev.sheldan.abstracto.moderation.service.WarnService;
|
import dev.sheldan.abstracto.moderation.service.WarnService;
|
||||||
import dev.sheldan.abstracto.command.Command;
|
import dev.sheldan.abstracto.command.Command;
|
||||||
import dev.sheldan.abstracto.command.HelpInfo;
|
import dev.sheldan.abstracto.command.HelpInfo;
|
||||||
@@ -41,6 +39,7 @@ public class Warn implements Command {
|
|||||||
warnLogModel.setWarnedUser(member);
|
warnLogModel.setWarnedUser(member);
|
||||||
warnLogModel.setReason(reason);
|
warnLogModel.setReason(reason);
|
||||||
warnLogModel.setWarningUser(commandContext.getAuthor());
|
warnLogModel.setWarningUser(commandContext.getAuthor());
|
||||||
|
warnLogModel.setMessageLink(commandContext.getMessage().getJumpUrl());
|
||||||
warnService.warnUser(member, commandContext.getAuthor(), reason, warnLogModel);
|
warnService.warnUser(member, commandContext.getAuthor(), reason, warnLogModel);
|
||||||
return Result.fromSuccess();
|
return Result.fromSuccess();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -49,7 +50,7 @@ public class WarnServiceBean implements WarnService {
|
|||||||
private static final String WARN_NOTIFICATION_TEMPLATE = "warn_notification";
|
private static final String WARN_NOTIFICATION_TEMPLATE = "warn_notification";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void warnUser(AUserInAServer warnedAUserInAServer, AUserInAServer warningAUserInAServer, String reason, ServerContext warnLog) {
|
public void warnUser(AUserInAServer warnedAUserInAServer, AUserInAServer warningAUserInAServer, String reason, WarnLog warnLog) {
|
||||||
AUser warningAUser = warningAUserInAServer.getUserReference();
|
AUser warningAUser = warningAUserInAServer.getUserReference();
|
||||||
AUser warnedAUser = warnedAUserInAServer.getUserReference();
|
AUser warnedAUser = warnedAUserInAServer.getUserReference();
|
||||||
AServer serverOfWarning = warnedAUserInAServer.getServerReference();
|
AServer serverOfWarning = warnedAUserInAServer.getServerReference();
|
||||||
@@ -72,18 +73,21 @@ public class WarnServiceBean implements WarnService {
|
|||||||
} else {
|
} else {
|
||||||
log.warn("Unable to find user {} in guild {} to warn.", warnedAUser.getId(), serverOfWarning.getId());
|
log.warn("Unable to find user {} in guild {} to warn.", warnedAUser.getId(), serverOfWarning.getId());
|
||||||
}
|
}
|
||||||
|
warnLog.setWarning(warning);
|
||||||
this.sendWarnLog(warnLog);
|
this.sendWarnLog(warnLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void warnUser(Member warnedMember, Member warningMember, String reason, ServerContext warnLog) {
|
public void warnUser(Member warnedMember, Member warningMember, String reason, WarnLog warnLog) {
|
||||||
AUserInAServer warnedAUser = userManagementService.loadUser(warnedMember);
|
AUserInAServer warnedAUser = userManagementService.loadUser(warnedMember);
|
||||||
AUserInAServer warningAUser = userManagementService.loadUser(warningMember);
|
AUserInAServer warningAUser = userManagementService.loadUser(warningMember);
|
||||||
this.warnUser(warnedAUser, warningAUser, reason, warnLog);
|
this.warnUser(warnedAUser, warningAUser, reason, warnLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendWarnLog(ServerContext warnLogModel) {
|
private void sendWarnLog(ServerContext warnLogModel) {
|
||||||
String warnLogMessage = templateService.renderContextAwareTemplate(WARN_LOG_TEMPLATE, warnLogModel);
|
String warnLogMessage = templateService.renderContextAwareTemplate(WARN_LOG_TEMPLATE, warnLogModel);
|
||||||
postTargetService.sendTextInPostTarget(warnLogMessage, PostTarget.WARN_LOG, warnLogModel.getServer().getId());
|
postTargetService.sendTextInPostTarget(warnLogMessage, PostTarget.WARN_LOG, warnLogModel.getServer().getId());
|
||||||
|
MessageEmbed embed = templateService.renderEmbedTemplate("warn_log", warnLogModel);
|
||||||
|
postTargetService.sendEmbedInPostTarget(embed, PostTarget.WARN_LOG, warnLogModel.getServer().getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"author": {
|
||||||
|
"name": "${warnedUser.effectiveName}",
|
||||||
|
"avatar": "${warnedUser.user.effectiveAvatarUrl}"
|
||||||
|
},
|
||||||
|
"title": {
|
||||||
|
"title": "User has been warned"
|
||||||
|
},
|
||||||
|
"color" : {
|
||||||
|
"r": 200,
|
||||||
|
"g": 0,
|
||||||
|
"b": 255
|
||||||
|
},
|
||||||
|
"fields": [
|
||||||
|
{
|
||||||
|
"name": "Warned User",
|
||||||
|
"value": "${warnedUser.effectiveName} ${warnedUser.asMention} (${warnedUser.idLong?c})"
|
||||||
|
},
|
||||||
|
<#if warningUser?has_content>
|
||||||
|
{
|
||||||
|
"name": "Warned by",
|
||||||
|
"value": "${warningUser.effectiveName} ${warningUser.asMention} (${warningUser.idLong?c})"
|
||||||
|
},
|
||||||
|
</#if>
|
||||||
|
<#if warning?has_content>
|
||||||
|
{
|
||||||
|
"name": "Location of the incident",
|
||||||
|
"value": "[${textChannel.name}](${messageLink})"
|
||||||
|
},
|
||||||
|
</#if>
|
||||||
|
{
|
||||||
|
"name": "Reason",
|
||||||
|
"value": "${reason}"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"footer": {
|
||||||
|
<#if warningUser?has_content>
|
||||||
|
"text": "Warning #${warning.id}"
|
||||||
|
</#if>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,5 +14,6 @@ public class WarnLog extends UserInitiatedServerContext {
|
|||||||
private String reason;
|
private String reason;
|
||||||
private Member warnedUser;
|
private Member warnedUser;
|
||||||
private Member warningUser;
|
private Member warningUser;
|
||||||
|
private String messageLink;
|
||||||
|
private Warning warning;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,6 @@ import net.dv8tion.jda.api.entities.Member;
|
|||||||
|
|
||||||
|
|
||||||
public interface WarnService {
|
public interface WarnService {
|
||||||
void warnUser(AUserInAServer warnedAUser, AUserInAServer warningAUser, String reason, ServerContext warnLog);
|
void warnUser(AUserInAServer warnedAUser, AUserInAServer warningAUser, String reason, WarnLog warnLog);
|
||||||
void warnUser(Member warnedUser, Member warningUser, String reason, ServerContext warnLog);
|
void warnUser(Member warnedUser, Member warningUser, String reason, WarnLog warnLog);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,12 @@ import dev.sheldan.abstracto.command.execution.CommandContext;
|
|||||||
import dev.sheldan.abstracto.command.execution.Result;
|
import dev.sheldan.abstracto.command.execution.Result;
|
||||||
import dev.sheldan.abstracto.command.execution.ResultState;
|
import dev.sheldan.abstracto.command.execution.ResultState;
|
||||||
import dev.sheldan.abstracto.templating.TemplateService;
|
import dev.sheldan.abstracto.templating.TemplateService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@Slf4j
|
||||||
public class ExceptionPostExecution implements PostCommandExecution {
|
public class ExceptionPostExecution implements PostCommandExecution {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -20,6 +22,7 @@ public class ExceptionPostExecution implements PostCommandExecution {
|
|||||||
public void execute(CommandContext commandContext, Result result, Command command) {
|
public void execute(CommandContext commandContext, Result result, Command command) {
|
||||||
if(result.getResult().equals(ResultState.ERROR)) {
|
if(result.getResult().equals(ResultState.ERROR)) {
|
||||||
if(result.getThrowable() != null) {
|
if(result.getThrowable() != null) {
|
||||||
|
log.warn("Exception", result.getThrowable());
|
||||||
if(result.getThrowable() instanceof TemplatedException) {
|
if(result.getThrowable() instanceof TemplatedException) {
|
||||||
TemplatedException exception = (TemplatedException) result.getThrowable();
|
TemplatedException exception = (TemplatedException) result.getThrowable();
|
||||||
String text = templateService.renderTemplate(exception.getTemplateName(), exception.getTemplateModel());
|
String text = templateService.renderTemplate(exception.getTemplateName(), exception.getTemplateModel());
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package dev.sheldan.abstracto.core.exception;
|
||||||
|
|
||||||
|
public class ConfigurationException extends RuntimeException {
|
||||||
|
public ConfigurationException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
package dev.sheldan.abstracto.core.service;
|
package dev.sheldan.abstracto.core.service;
|
||||||
|
|
||||||
|
import dev.sheldan.abstracto.core.exception.ConfigurationException;
|
||||||
import dev.sheldan.abstracto.core.management.PostTargetManagement;
|
import dev.sheldan.abstracto.core.management.PostTargetManagement;
|
||||||
import dev.sheldan.abstracto.core.management.ServerManagementService;
|
import dev.sheldan.abstracto.core.management.ServerManagementService;
|
||||||
import dev.sheldan.abstracto.core.models.database.PostTarget;
|
import dev.sheldan.abstracto.core.models.database.PostTarget;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
import net.dv8tion.jda.api.entities.TextChannel;
|
import net.dv8tion.jda.api.entities.TextChannel;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -25,11 +27,22 @@ public class PostTargetServiceBean implements PostTargetService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendTextInPostTarget(String text, PostTarget target) {
|
public void sendTextInPostTarget(String text, PostTarget target) {
|
||||||
|
TextChannel textChannelForPostTarget = getTextChannelForPostTarget(target);
|
||||||
|
textChannelForPostTarget.sendMessage(text).queue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendEmbedInPostTarget(MessageEmbed embed, PostTarget target) {
|
||||||
|
TextChannel textChannelForPostTarget = getTextChannelForPostTarget(target);
|
||||||
|
textChannelForPostTarget.sendMessage(embed).queue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private TextChannel getTextChannelForPostTarget(PostTarget target) {
|
||||||
Guild guild = botService.getInstance().getGuildById(target.getServerReference().getId());
|
Guild guild = botService.getInstance().getGuildById(target.getServerReference().getId());
|
||||||
if(guild != null) {
|
if(guild != null) {
|
||||||
TextChannel textChannelById = guild.getTextChannelById(target.getChannelReference().getId());
|
TextChannel textChannelById = guild.getTextChannelById(target.getChannelReference().getId());
|
||||||
if(textChannelById != null) {
|
if(textChannelById != null) {
|
||||||
textChannelById.sendMessage(text).queue();
|
return textChannelById;
|
||||||
} else {
|
} else {
|
||||||
log.warn("Incorrect post target configuration: {} points to {} on server {}", target.getName(),
|
log.warn("Incorrect post target configuration: {} points to {} on server {}", target.getName(),
|
||||||
target.getChannelReference().getId(), target.getServerReference().getId());
|
target.getChannelReference().getId(), target.getServerReference().getId());
|
||||||
@@ -37,15 +50,28 @@ public class PostTargetServiceBean implements PostTargetService {
|
|||||||
} else {
|
} else {
|
||||||
log.warn("Incorrect post target configuration: Guild id {} was not found.", target.getServerReference().getId());
|
log.warn("Incorrect post target configuration: Guild id {} was not found.", target.getServerReference().getId());
|
||||||
}
|
}
|
||||||
|
throw new ConfigurationException("Incorrect post target configuration.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private PostTarget getPostTarget(String postTargetName, Long serverId) {
|
||||||
|
PostTarget postTarget = postTargetManagement.getPostTarget(postTargetName, serverId);
|
||||||
|
if(postTarget != null) {
|
||||||
|
return postTarget;
|
||||||
|
} else {
|
||||||
|
log.warn("PostTarget {} in server {} was not found!", postTargetName, serverId);
|
||||||
|
throw new ConfigurationException(String.format("Incorrect post target configuration: Post target %s was not found.", postTargetName));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendTextInPostTarget(String text, String postTargetName, Long serverId) {
|
public void sendTextInPostTarget(String text, String postTargetName, Long serverId) {
|
||||||
PostTarget postTarget = postTargetManagement.getPostTarget(postTargetName, serverId);
|
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);
|
||||||
if(postTarget != null) {
|
|
||||||
this.sendTextInPostTarget(text, postTarget);
|
this.sendTextInPostTarget(text, postTarget);
|
||||||
} else {
|
|
||||||
log.warn("PostTarget {} in server {} was not found!", postTargetName, serverId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendEmbedInPostTarget(MessageEmbed embed, String postTargetName, Long serverId) {
|
||||||
|
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);
|
||||||
|
this.sendEmbedInPostTarget(embed, postTarget);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package dev.sheldan.abstracto.core.service;
|
package dev.sheldan.abstracto.core.service;
|
||||||
|
|
||||||
import dev.sheldan.abstracto.core.models.database.PostTarget;
|
import dev.sheldan.abstracto.core.models.database.PostTarget;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
|
|
||||||
public interface PostTargetService {
|
public interface PostTargetService {
|
||||||
void sendTextInPostTarget(String text, PostTarget target);
|
void sendTextInPostTarget(String text, PostTarget target);
|
||||||
|
void sendEmbedInPostTarget(MessageEmbed embed, PostTarget target);
|
||||||
void sendTextInPostTarget(String text, String postTargetName, Long serverId);
|
void sendTextInPostTarget(String text, String postTargetName, Long serverId);
|
||||||
|
void sendEmbedInPostTarget(MessageEmbed embed, String postTargetName, Long serverId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,13 @@
|
|||||||
<version>4.3</version>
|
<version>4.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.8.6</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|||||||
@@ -22,10 +22,17 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -19,6 +19,9 @@ public class FreemarkerConfiguration {
|
|||||||
public Configuration freeMarkerConfiguration() throws IOException, TemplateException {
|
public Configuration freeMarkerConfiguration() throws IOException, TemplateException {
|
||||||
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
|
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
|
||||||
factory.setPreTemplateLoaders(templateLoader);
|
factory.setPreTemplateLoaders(templateLoader);
|
||||||
return factory.createConfiguration();
|
Configuration configuration = factory.createConfiguration();
|
||||||
|
// needed to support default methods in interfaces
|
||||||
|
configuration.setIncompatibleImprovements(Configuration.VERSION_2_3_29);
|
||||||
|
return configuration;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package dev.sheldan.abstracto.templating.embed;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter @Setter
|
||||||
|
public class EmbedAuthor {
|
||||||
|
private String name;
|
||||||
|
private String url;
|
||||||
|
private String avatar;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package dev.sheldan.abstracto.templating.embed;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Setter @Getter
|
||||||
|
public class EmbedColor {
|
||||||
|
private Integer r;
|
||||||
|
private Integer g;
|
||||||
|
private Integer b;
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package dev.sheldan.abstracto.templating.embed;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Getter @Setter
|
||||||
|
public class EmbedConfiguration {
|
||||||
|
private EmbedAuthor author;
|
||||||
|
private EmbedTitle title;
|
||||||
|
private EmbedColor color;
|
||||||
|
private String description;
|
||||||
|
private String thumbnail;
|
||||||
|
private List<EmbedField> fields;
|
||||||
|
private EmbedFooter footer;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package dev.sheldan.abstracto.templating.embed;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter @Setter
|
||||||
|
public class EmbedField {
|
||||||
|
private String name;
|
||||||
|
private String value;
|
||||||
|
private Boolean inline;
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package dev.sheldan.abstracto.templating.embed;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter @Setter
|
||||||
|
public class EmbedFooter {
|
||||||
|
private String text;
|
||||||
|
private String icon;
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package dev.sheldan.abstracto.templating.embed;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter @Setter
|
||||||
|
public class EmbedTitle {
|
||||||
|
private String title;
|
||||||
|
private String url;
|
||||||
|
}
|
||||||
@@ -1,18 +1,24 @@
|
|||||||
package dev.sheldan.abstracto.templating.loading;
|
package dev.sheldan.abstracto.templating.loading;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
import dev.sheldan.abstracto.core.models.ContextAware;
|
import dev.sheldan.abstracto.core.models.ContextAware;
|
||||||
import dev.sheldan.abstracto.core.models.ServerContext;
|
import dev.sheldan.abstracto.core.models.ServerContext;
|
||||||
import dev.sheldan.abstracto.templating.TemplateDto;
|
import dev.sheldan.abstracto.templating.TemplateDto;
|
||||||
import dev.sheldan.abstracto.templating.TemplateService;
|
import dev.sheldan.abstracto.templating.TemplateService;
|
||||||
|
import dev.sheldan.abstracto.templating.embed.*;
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
|
import freemarker.template.Template;
|
||||||
import freemarker.template.TemplateException;
|
import freemarker.template.TemplateException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
@@ -26,8 +32,10 @@ public class TemplateServiceBean implements TemplateService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private Configuration configuration;
|
private Configuration configuration;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Gson gson;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable("template")
|
|
||||||
public TemplateDto getTemplateByKey(String key) {
|
public TemplateDto getTemplateByKey(String key) {
|
||||||
return repository.getOne(key);
|
return repository.getOne(key);
|
||||||
}
|
}
|
||||||
@@ -42,6 +50,58 @@ public class TemplateServiceBean implements TemplateService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MessageEmbed renderEmbedTemplate(String key, Object model) {
|
||||||
|
String embedConfig = this.renderTemplate(key + "_embed", model);
|
||||||
|
EmbedBuilder builder = new EmbedBuilder();
|
||||||
|
EmbedConfiguration configuration = gson.fromJson(embedConfig, EmbedConfiguration.class);
|
||||||
|
String description = configuration.getDescription();
|
||||||
|
if(description != null) {
|
||||||
|
builder.setDescription(description);
|
||||||
|
}
|
||||||
|
EmbedAuthor author = configuration.getAuthor();
|
||||||
|
if(author != null) {
|
||||||
|
builder.setAuthor(author.getName(), author.getUrl(), author.getAvatar());
|
||||||
|
}
|
||||||
|
|
||||||
|
String thumbnail = configuration.getThumbnail();
|
||||||
|
if(thumbnail != null) {
|
||||||
|
builder.setThumbnail(thumbnail);
|
||||||
|
}
|
||||||
|
EmbedTitle title = configuration.getTitle();
|
||||||
|
if(title != null) {
|
||||||
|
builder.setTitle(title.getTitle(), title.getUrl());
|
||||||
|
}
|
||||||
|
EmbedFooter footer = configuration.getFooter();
|
||||||
|
if(footer != null) {
|
||||||
|
builder.setFooter(footer.getText(), footer.getIcon());
|
||||||
|
}
|
||||||
|
configuration.getFields().forEach(embedField -> {
|
||||||
|
Boolean inline = embedField.getInline() != null ? embedField.getInline() : Boolean.FALSE;
|
||||||
|
builder.addField(embedField.getName(), embedField.getValue(), inline);
|
||||||
|
});
|
||||||
|
|
||||||
|
EmbedColor color = configuration.getColor();
|
||||||
|
if(color != null) {
|
||||||
|
builder.setColor(new Color(color.getR(), color.getG(), color.getB()).getRGB());
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String impromptu(String templateStr, Object model) {
|
||||||
|
try {
|
||||||
|
Template t = new Template("name", new StringReader(templateStr),
|
||||||
|
new Configuration(Configuration.VERSION_2_3_29));
|
||||||
|
return FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (TemplateException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String renderTemplate(String key, HashMap<String, Object> parameters) {
|
public String renderTemplate(String key, HashMap<String, Object> parameters) {
|
||||||
try {
|
try {
|
||||||
@@ -58,8 +118,8 @@ public class TemplateServiceBean implements TemplateService {
|
|||||||
return FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(key), model);
|
return FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(key), model);
|
||||||
} catch (IOException | TemplateException e) {
|
} catch (IOException | TemplateException e) {
|
||||||
log.warn("Failed to render template: {}", e.getMessage());
|
log.warn("Failed to render template: {}", e.getMessage());
|
||||||
|
throw new RuntimeException("Failed to render template", e);
|
||||||
}
|
}
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.sheldan.abstracto.templating.seeding;
|
package dev.sheldan.abstracto.templating.seeding;
|
||||||
|
|
||||||
import dev.sheldan.abstracto.templating.TemplateService;
|
import dev.sheldan.abstracto.templating.TemplateService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -16,6 +17,7 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@Slf4j
|
||||||
public class TemplateSeedDataLoader {
|
public class TemplateSeedDataLoader {
|
||||||
|
|
||||||
@Value("classpath*:**/templates/**/*.ftl")
|
@Value("classpath*:**/templates/**/*.ftl")
|
||||||
@@ -31,6 +33,7 @@ public class TemplateSeedDataLoader {
|
|||||||
try {
|
try {
|
||||||
String templateKey = FilenameUtils.getBaseName(resource.getFilename());
|
String templateKey = FilenameUtils.getBaseName(resource.getFilename());
|
||||||
String templateContent = IOUtils.toString(resource.getURI(), Charset.defaultCharset());
|
String templateContent = IOUtils.toString(resource.getURI(), Charset.defaultCharset());
|
||||||
|
log.debug("Creating template {}", templateKey);
|
||||||
service.createTemplate(templateKey, templateContent);
|
service.createTemplate(templateKey, templateContent);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import lombok.Builder;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
@@ -22,6 +23,7 @@ public class TemplateDto {
|
|||||||
private String key;
|
private String key;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@Column(length = 4000)
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.sheldan.abstracto.templating;
|
package dev.sheldan.abstracto.templating;
|
||||||
|
|
||||||
import dev.sheldan.abstracto.core.models.ServerContext;
|
import dev.sheldan.abstracto.core.models.ServerContext;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
@@ -8,6 +9,7 @@ public interface TemplateService {
|
|||||||
TemplateDto getTemplateByKey(String key);
|
TemplateDto getTemplateByKey(String key);
|
||||||
boolean templateExists(String key);
|
boolean templateExists(String key);
|
||||||
String renderTemplate(TemplateDto templateDto);
|
String renderTemplate(TemplateDto templateDto);
|
||||||
|
MessageEmbed renderEmbedTemplate(String key, Object model);
|
||||||
String renderTemplate(String key, HashMap<String, Object> parameters);
|
String renderTemplate(String key, HashMap<String, Object> parameters);
|
||||||
String renderTemplate(String key, Object model);
|
String renderTemplate(String key, Object model);
|
||||||
String renderContextAwareTemplate(String key, ServerContext serverContext);
|
String renderContextAwareTemplate(String key, ServerContext serverContext);
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -8,7 +8,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>2.2.2.RELEASE</version>
|
<version>2.2.5.RELEASE</version>
|
||||||
<relativePath/>
|
<relativePath/>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user