mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-06 16:26:31 +00:00
added serverInfo help
added userInfo command added offsetDateTime to the custom date formatter
This commit is contained in:
@@ -2,7 +2,7 @@ package dev.sheldan.abstracto.templating.config;
|
||||
|
||||
import dev.sheldan.abstracto.templating.loading.DatabaseTemplateLoader;
|
||||
import dev.sheldan.abstracto.templating.methods.DurationMethod;
|
||||
import dev.sheldan.abstracto.templating.methods.InstantMethod;
|
||||
import dev.sheldan.abstracto.templating.methods.DateMethod;
|
||||
import dev.sheldan.abstracto.templating.methods.SafeFieldIterations;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.TemplateException;
|
||||
@@ -26,7 +26,7 @@ public class FreemarkerConfiguration {
|
||||
private DurationMethod durationMethod;
|
||||
|
||||
@Autowired
|
||||
private InstantMethod instantMethod;
|
||||
private DateMethod instantMethod;
|
||||
|
||||
@Autowired
|
||||
private SafeFieldIterations safeFieldIterations;
|
||||
@@ -43,7 +43,7 @@ public class FreemarkerConfiguration {
|
||||
factory.setPreTemplateLoaders(templateLoader);
|
||||
Configuration configuration = factory.createConfiguration();
|
||||
configuration.setSharedVariable("fmtDuration", durationMethod);
|
||||
configuration.setSharedVariable("formatInstant", instantMethod);
|
||||
configuration.setSharedVariable("formatDate", instantMethod);
|
||||
configuration.setSharedVariable("safeFieldLength", safeFieldIterations);
|
||||
configuration.setEncoding(Locale.getDefault(), "utf-8");
|
||||
// needed to support default methods in interfaces
|
||||
|
||||
@@ -9,24 +9,25 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Formats the passed {@link Instant} object with the given Formatter. The format will be directly passed to {@link DateTimeFormatter}.
|
||||
* Formats the passed {@link Instant} or {@link OffsetDateTime} object with the given Formatter. The format will be directly passed to {@link DateTimeFormatter}.
|
||||
*/
|
||||
@Component
|
||||
public class InstantMethod implements TemplateMethodModelEx {
|
||||
public class DateMethod implements TemplateMethodModelEx {
|
||||
|
||||
@Autowired
|
||||
private TemplateService service;
|
||||
|
||||
/**
|
||||
* Renders the given {@link Instant} object with the given String. Internally {@link DateTimeFormatter} will be used.
|
||||
* @param arguments The list of arguments, first element must be an {@link Instant} and the second one must be a {@link String}.
|
||||
* @param arguments The list of arguments, first element must be an {@link Instant} or {@link OffsetDateTime} and the second one must be a {@link String}.
|
||||
* @return The formatted {@link Instant} as a string.
|
||||
* @throws TemplateModelException If there are less or more arguments in the list and if the first element is not a {@link Instant}.
|
||||
* @throws TemplateModelException If there are less or more arguments in the list and if the first element is not a {@link Instant} of {@link OffsetDateTime}
|
||||
*/
|
||||
@Override
|
||||
public Object exec(List arguments) throws TemplateModelException {
|
||||
@@ -34,14 +35,21 @@ public class InstantMethod implements TemplateMethodModelEx {
|
||||
throw new TemplateModelException("Incorrect parameters passed.");
|
||||
}
|
||||
Object wrappedObject = ((StringModel) arguments.get(0)).getWrappedObject();
|
||||
if(!(wrappedObject instanceof Instant)) {
|
||||
boolean isOffsetDateTime = wrappedObject instanceof OffsetDateTime;
|
||||
boolean isInstant = wrappedObject instanceof Instant;
|
||||
if(!isInstant && !isOffsetDateTime) {
|
||||
throw new TemplateModelException("Passed argument was not a instant object");
|
||||
}
|
||||
|
||||
String formatString = ((SimpleScalar) arguments.get(1)).getAsString();
|
||||
Instant timeStamp = (Instant) wrappedObject;
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatString)
|
||||
.withZone(ZoneId.systemDefault());
|
||||
return formatter.format(timeStamp);
|
||||
if(isInstant) {
|
||||
Instant timeStamp = (Instant) wrappedObject;
|
||||
return formatter.format(timeStamp);
|
||||
} else {
|
||||
OffsetDateTime offsetDateTime = (OffsetDateTime) wrappedObject;
|
||||
return formatter.format(offsetDateTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,11 +92,15 @@ public class TemplateServiceBean implements TemplateService {
|
||||
if(configuration.getFields() != null) {
|
||||
for (int i = 0; i < configuration.getFields().size(); i++) {
|
||||
EmbedField field = configuration.getFields().get(i);
|
||||
if(field.getValue().length() > 1024) {
|
||||
String substring = field.getValue().substring(1024);
|
||||
field.setValue(field.getValue().substring(0, 1024));
|
||||
EmbedField secondPart = EmbedField.builder().inline(field.getInline()).name(field.getName() + " 2").value(substring).build();
|
||||
configuration.getFields().add(i + 1, secondPart);
|
||||
if(field != null && field.getValue() != null) {
|
||||
if(field.getValue().length() > MessageEmbed.VALUE_MAX_LENGTH) {
|
||||
String substring = field.getValue().substring(MessageEmbed.VALUE_MAX_LENGTH);
|
||||
field.setValue(field.getValue().substring(0, MessageEmbed.VALUE_MAX_LENGTH));
|
||||
EmbedField secondPart = EmbedField.builder().inline(field.getInline()).name(field.getName() + " 2").value(substring).build();
|
||||
configuration.getFields().add(i + 1, secondPart);
|
||||
}
|
||||
} else {
|
||||
log.warn("Field {} in template {} is null.", i, key);
|
||||
}
|
||||
}
|
||||
double neededIndex = Math.ceil(configuration.getFields().size() / 25D) - 1;
|
||||
|
||||
Reference in New Issue
Block a user