mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-28 08:38:52 +00:00
added serverinfo command
added freemarker extension in order to split up elements in fields at a point which makes sense (necessary for emotes to render) added additional check to split fields if they are indeed over the max length
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package dev.sheldan.abstracto.utility.commands;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.UtilityModuleInterface;
|
||||
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
|
||||
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
|
||||
import dev.sheldan.abstracto.core.command.config.HelpInfo;
|
||||
import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
|
||||
import dev.sheldan.abstracto.core.config.FeatureEnum;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.utility.config.features.UtilityFeature;
|
||||
import dev.sheldan.abstracto.utility.models.template.commands.serverinfo.ServerInfoModel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class ServerInfo extends AbstractConditionableCommand {
|
||||
|
||||
@Autowired
|
||||
private ChannelService channelService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
ServerInfoModel model = (ServerInfoModel) ContextConverter.fromCommandContext(commandContext, ServerInfoModel.class);
|
||||
model.setGuild(commandContext.getGuild());
|
||||
channelService.sendEmbedTemplateInChannel("serverinfo_response", model, commandContext.getChannel());
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandConfiguration getConfiguration() {
|
||||
List<Parameter> parameters = new ArrayList<>();
|
||||
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
|
||||
return CommandConfiguration.builder()
|
||||
.name("serverInfo")
|
||||
.module(UtilityModuleInterface.UTILITY)
|
||||
.templated(true)
|
||||
.causesReaction(false)
|
||||
.parameters(parameters)
|
||||
.help(helpInfo)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FeatureEnum getFeature() {
|
||||
return UtilityFeature.UTILITY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<#assign additionalInfo><#if count gt 1>${count}<#else><#assign count>${list?size}</#assign><#include "serverinfo_embed_emotes_title_total_emotes"></#if></#assign><#include "serverinfo_embed_emotes_field_title_message">
|
||||
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"title": {
|
||||
"title": "<#assign name=guild.name><#include "serverinfo_embed_title">"
|
||||
},
|
||||
"color" : {
|
||||
"r": 200,
|
||||
"g": 0,
|
||||
"b": 255
|
||||
},
|
||||
"fields": [
|
||||
{
|
||||
"name": "<#include "serverinfo_embed_id_field_title">",
|
||||
"value": "${guild.id}",
|
||||
"inline": "true"
|
||||
},
|
||||
{
|
||||
"name": "<#include "serverinfo_embed_owner_field_title">",
|
||||
"value": "${guild.owner.effectiveName}#${guild.owner.user.discriminator}",
|
||||
"inline": "true"
|
||||
},
|
||||
{
|
||||
"name": "<#include "serverinfo_embed_members_field_title">",
|
||||
"value": "${guild.memberCount}",
|
||||
"inline": "true"
|
||||
},
|
||||
{
|
||||
"name": "<#include "serverinfo_embed_role_count_field_title">",
|
||||
"value": "${guild.roles?size}",
|
||||
"inline": "true"
|
||||
},
|
||||
{
|
||||
"name": "<#include "serverinfo_embed_created_field_title">",
|
||||
"value": "${guild.timeCreated}",
|
||||
"inline": "true"
|
||||
},
|
||||
${safeFieldLength(guild.emotes, 'emote_mention', 'serverinfo_embed_emotes_field_title', 'false')},
|
||||
{
|
||||
"name": "<#include "serverinfo_embed_features_field_title">",
|
||||
"value": "<#list guild.features as feature>${feature}<#else>No features</#list>",
|
||||
"inline": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package dev.sheldan.abstracto.utility.models.template.commands.serverinfo;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.context.UserInitiatedServerContext;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import net.dv8tion.jda.api.entities.Emote;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@SuperBuilder
|
||||
public class ServerInfoModel extends UserInitiatedServerContext {
|
||||
private Guild guild;
|
||||
private List<Emote> emotes;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Created at
|
||||
@@ -0,0 +1 @@
|
||||
Emotes ${additionalInfo}
|
||||
@@ -0,0 +1 @@
|
||||
(Total emotes ${count})
|
||||
@@ -0,0 +1 @@
|
||||
Features
|
||||
@@ -0,0 +1 @@
|
||||
Server ID
|
||||
@@ -0,0 +1 @@
|
||||
Member count
|
||||
@@ -0,0 +1 @@
|
||||
Owner
|
||||
@@ -0,0 +1 @@
|
||||
Role count
|
||||
@@ -0,0 +1 @@
|
||||
Server info of ${name}
|
||||
@@ -3,6 +3,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.SafeFieldIterations;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.TemplateException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -27,6 +28,9 @@ public class FreemarkerConfiguration {
|
||||
@Autowired
|
||||
private InstantMethod instantMethod;
|
||||
|
||||
@Autowired
|
||||
private SafeFieldIterations safeFieldIterations;
|
||||
|
||||
/**
|
||||
* Creates a {@link Configuration} bean with the appropriate configuration which includes:
|
||||
* The correct compatibility version and the provided formatter methods to be used in the templates.
|
||||
@@ -40,6 +44,7 @@ public class FreemarkerConfiguration {
|
||||
Configuration configuration = factory.createConfiguration();
|
||||
configuration.setSharedVariable("fmtDuration", durationMethod);
|
||||
configuration.setSharedVariable("formatInstant", instantMethod);
|
||||
configuration.setSharedVariable("safeFieldLength", safeFieldIterations);
|
||||
configuration.setEncoding(Locale.getDefault(), "utf-8");
|
||||
// needed to support default methods in interfaces
|
||||
configuration.setIncompatibleImprovements(Configuration.VERSION_2_3_29);
|
||||
|
||||
@@ -39,7 +39,6 @@ public class DurationMethod implements TemplateMethodModelEx {
|
||||
throw new TemplateModelException("Passed argument was not a duration object");
|
||||
}
|
||||
Duration duration = (Duration) wrappedObject;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
// upgrading to java 9 makes this nicer
|
||||
HashMap<String, Object> parameters = new HashMap<>();
|
||||
long days = duration.toDays();
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package dev.sheldan.abstracto.templating.methods;
|
||||
|
||||
import dev.sheldan.abstracto.templating.service.TemplateService;
|
||||
import freemarker.template.DefaultListAdapter;
|
||||
import freemarker.template.SimpleScalar;
|
||||
import freemarker.template.TemplateMethodModelEx;
|
||||
import freemarker.template.TemplateModelException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class SafeFieldIterations implements TemplateMethodModelEx {
|
||||
|
||||
@Autowired
|
||||
private TemplateService service;
|
||||
|
||||
@Override
|
||||
public Object exec(List arguments) throws TemplateModelException {
|
||||
List wrappedObject = (List) ((DefaultListAdapter) arguments.get(0)).getWrappedObject();
|
||||
String appliedTemplate = ((SimpleScalar) arguments.get(1)).getAsString();
|
||||
String nameTemplate = ((SimpleScalar) arguments.get(2)).getAsString();
|
||||
String inline = ((SimpleScalar) arguments.get(3)).getAsString();
|
||||
|
||||
|
||||
List<StringBuilder> result = new ArrayList<>();
|
||||
StringBuilder currentBuilder = new StringBuilder();
|
||||
String firstEmbedTitle = service.renderTemplateWithMap(nameTemplate, getEmbedCountParameters(1, wrappedObject));
|
||||
currentBuilder.append(newFieldHeader(firstEmbedTitle, inline));
|
||||
String finalClosingString = "\"}";
|
||||
String closingString = finalClosingString + ",";
|
||||
int splitFieldCounts = 1;
|
||||
for (Object ob: wrappedObject) {
|
||||
HashMap<String, Object> parameters = new HashMap<>();
|
||||
parameters.put("object", ob);
|
||||
String s = service.renderTemplateWithMap(appliedTemplate, parameters);
|
||||
if((currentBuilder.toString().length() + s.length() > 1024)) {
|
||||
currentBuilder.append(closingString);
|
||||
result.add(currentBuilder);
|
||||
currentBuilder = new StringBuilder();
|
||||
splitFieldCounts += 1;
|
||||
String renderedName = service.renderTemplateWithMap(nameTemplate, getEmbedCountParameters(splitFieldCounts, wrappedObject));
|
||||
currentBuilder.append(newFieldHeader(renderedName, inline));
|
||||
}
|
||||
currentBuilder.append(s);
|
||||
|
||||
}
|
||||
currentBuilder.append(finalClosingString);
|
||||
result.add(currentBuilder);
|
||||
StringBuilder bigBuilder = new StringBuilder();
|
||||
for (StringBuilder innerBuilder: result) {
|
||||
bigBuilder.append(innerBuilder.toString());
|
||||
}
|
||||
return bigBuilder;
|
||||
}
|
||||
|
||||
private String newFieldHeader(String name, String inline) {
|
||||
return String.format("{ \"name\": \"%s\", \"inline\": \"%s\", \"value\": \"", name, inline);
|
||||
}
|
||||
|
||||
private HashMap<String, Object> getEmbedCountParameters(Integer count, List objects) {
|
||||
HashMap<String, Object> parameters = new HashMap<>();
|
||||
parameters.put("count", count);
|
||||
parameters.put("list", objects);
|
||||
return parameters;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.templating.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@@ -8,6 +9,7 @@ import lombok.Setter;
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public class EmbedField {
|
||||
/**
|
||||
* The name of the field to be set
|
||||
|
||||
@@ -90,6 +90,15 @@ public class TemplateServiceBean implements TemplateService {
|
||||
firstBuilder.setFooter(footer.getText(), footer.getIcon());
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
double neededIndex = Math.ceil(configuration.getFields().size() / 25D) - 1;
|
||||
extendIfNecessary(embedBuilders, neededIndex);
|
||||
for (int i = 0; i < configuration.getFields().size(); i++) {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
${object.asMention}
|
||||
Reference in New Issue
Block a user