Beginning of serializing Discord messages in the config

This commit is contained in:
Vankka 2021-07-31 03:11:19 +03:00
parent 208d124fea
commit 2c3031dc75
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
6 changed files with 144 additions and 19 deletions

View File

@ -84,7 +84,11 @@ public interface DiscordSRVApi {
* @return the JDA instance, if available
*
* <p>
* JDA is an external API, using DiscordSRV's APIs where possible is recommended. Please see <a href="https://github.com/DV8FromTheWorld/JDA#deprecation-policy">JDA's deprecation policy</a>.
* JDA is an external API, using DiscordSRV's APIs where possible is recommended.
* Please see <a href="https://github.com/DV8FromTheWorld/JDA#deprecation-policy">JDA's deprecation policy</a>,
* additionally DiscordSRV may update the major version of JDA, which will have breaking changes.
*
* @see #discordAPI()
* @see #isReady()
* @see #discordConnectionDetails()
*/

View File

@ -153,6 +153,10 @@ public class DiscordMessageEmbed {
return footerImageUrl;
}
public Builder toBuilder() {
return new Builder(this);
}
public MessageEmbed toJDA() {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(color);
@ -208,11 +212,11 @@ public class DiscordMessageEmbed {
private OffsetDateTime timestamp;
private String footer, footerImageUrl;
public Builder() {
protected Builder() {
this.fields = new ArrayList<>();
}
public Builder(int color, String authorName, String authorUrl, String authorImageUrl, String title,
protected Builder(int color, String authorName, String authorUrl, String authorImageUrl, String title,
String titleUrl, String description, List<Field> fields, String thumbnailUrl, String imageUrl,
OffsetDateTime timestamp, String footer, String footerImageUrl) {
this.color = color;
@ -230,6 +234,22 @@ public class DiscordMessageEmbed {
this.footerImageUrl = footerImageUrl;
}
protected Builder(DiscordMessageEmbed embed) {
this.color = embed.getColor();
this.authorName = embed.getAuthorName();
this.authorUrl = embed.getAuthorUrl();
this.authorImageUrl = embed.getAuthorImageUrl();
this.title = embed.getTitle();
this.titleUrl = embed.getTitleUrl();
this.description = embed.getDescription();
this.fields = embed.getFields();
this.thumbnailUrl = embed.getThumbnailUrl();
this.imageUrl = embed.getImageUrl();
this.timestamp = embed.getTimestamp();
this.footer = embed.getFooter();
this.footerImageUrl = embed.getFooterImageUrl();
}
public int getColor() {
return color;
}
@ -390,7 +410,7 @@ public class DiscordMessageEmbed {
@SuppressWarnings({"MethodDoesntCallSuperMethod", "CloneDoesntDeclareCloneNotSupportedException"})
@Override
protected Builder clone() {
public Builder clone() {
return new Builder(color, authorName, authorUrl, authorImageUrl, title, titleUrl, description,
fields, thumbnailUrl, imageUrl, timestamp, footer, footerImageUrl);
}

View File

@ -18,11 +18,15 @@
package com.discordsrv.common.config.manager;
import com.discordsrv.api.discord.api.entity.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.api.entity.message.SendableDiscordMessage;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.main.channels.ChannelConfigHolder;
import com.discordsrv.common.config.manager.loader.YamlConfigLoaderProvider;
import com.discordsrv.common.config.manager.manager.TranslatedConfigManager;
import com.discordsrv.common.config.serializer.DiscordMessageEmbedSerializer;
import com.discordsrv.common.config.serializer.SendableDiscordMessageSerializer;
import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
@ -40,16 +44,14 @@ public abstract class MainConfigManager<C extends MainConfig>
return MainConfig.FILE_NAME;
}
@Override
public ObjectMapper.Factory.Builder objectMapper() {
return super.objectMapper();
}
@Override
public ConfigurationOptions defaultOptions() {
ChannelConfigHolder.Serializer channelConfigSerializer = new ChannelConfigHolder.Serializer(objectMapper().build());
return YamlConfigLoaderProvider.super.defaultOptions()
.serializers(builder ->
builder.register(ChannelConfigHolder.class, channelConfigSerializer));
.serializers(builder -> {
ObjectMapper.Factory objectMapper = defaultObjectMapper();
builder.register(ChannelConfigHolder.class, new ChannelConfigHolder.Serializer(objectMapper));
builder.register(DiscordMessageEmbed.Builder.class, new DiscordMessageEmbedSerializer());
builder.register(SendableDiscordMessage.Builder.class, new SendableDiscordMessageSerializer());
});
}
}

View File

@ -44,6 +44,8 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
protected final DiscordSRV discordSRV;
private final Path filePath;
private final LT loader;
private final ObjectMapper.Factory configObjectMapper;
private final ObjectMapper.Factory defaultObjectMapper;
protected T configuration;
@ -51,6 +53,8 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
this.discordSRV = discordSRV;
this.filePath = new File(discordSRV.dataDirectory().toFile(), fileName()).toPath();
this.loader = createLoader(filePath, configNodeOptions());
this.configObjectMapper = configObjectMapperBuilder().build();
this.defaultObjectMapper = defaultObjectMapperBuilder().build();
}
public Path filePath() {
@ -73,7 +77,7 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
.shouldCopyDefaults(false);
}
public ObjectMapper.Factory.Builder objectMapper() {
protected ObjectMapper.Factory.Builder objectMapperBuilder() {
return ObjectMapper.factoryBuilder();
}
@ -81,16 +85,20 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
return defaultOptions();
}
public ObjectMapper.Factory.Builder configObjectMapper() {
return objectMapper();
protected ObjectMapper.Factory.Builder configObjectMapperBuilder() {
return objectMapperBuilder();
}
public ObjectMapper.Factory configObjectMapper() {
return configObjectMapper;
}
public ConfigurationOptions defaultNodeOptions() {
return defaultOptions();
}
public ObjectMapper.Factory.Builder defaultObjectMapper() {
return configObjectMapper()
protected ObjectMapper.Factory.Builder defaultObjectMapperBuilder() {
return configObjectMapperBuilder()
.addProcessor(DefaultOnly.class, (data, value) -> (value1, destination) -> {
String[] children = data.value();
boolean whitelist = data.whitelist();
@ -124,9 +132,13 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
});
}
public ObjectMapper.Factory defaultObjectMapper() {
return defaultObjectMapper;
}
private CommentedConfigurationNode getDefault(T defaultConfig, boolean cleanMapper) throws SerializationException {
CommentedConfigurationNode node = CommentedConfigurationNode.root(defaultNodeOptions());
(cleanMapper ? defaultObjectMapper() : configObjectMapper()).build()
(cleanMapper ? defaultObjectMapper() : configObjectMapper())
.get(defaultConfig.getClass()).load(node);
return node;
}
@ -164,7 +176,7 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
node = getDefault(defaultConfig, false);
}
this.configuration = configObjectMapper().build()
this.configuration = configObjectMapper()
.get((Class<T>) defaultConfig.getClass())
.load(node);
} catch (ConfigurateException e) {

View File

@ -0,0 +1,23 @@
package com.discordsrv.common.config.serializer;
import com.discordsrv.api.discord.api.entity.message.DiscordMessageEmbed;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import java.lang.reflect.Type;
public class DiscordMessageEmbedSerializer implements TypeSerializer<DiscordMessageEmbed.Builder> {
@Override
public DiscordMessageEmbed.Builder deserialize(Type type, ConfigurationNode node) throws SerializationException {
// TODO
return null;
}
@Override
public void serialize(Type type, DiscordMessageEmbed.@Nullable Builder obj, ConfigurationNode node) throws SerializationException {
// TODO
}
}

View File

@ -0,0 +1,64 @@
package com.discordsrv.common.config.serializer;
import com.discordsrv.api.discord.api.entity.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.api.entity.message.SendableDiscordMessage;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class SendableDiscordMessageSerializer implements TypeSerializer<SendableDiscordMessage.Builder> {
@Override
public SendableDiscordMessage.Builder deserialize(Type type, ConfigurationNode node) throws SerializationException {
String contentOnly = node.getString();
if (contentOnly != null) {
return SendableDiscordMessage.builder()
.setContent(contentOnly);
}
SendableDiscordMessage.Builder builder = SendableDiscordMessage.builder();
ConfigurationNode webhook = node.node("Webhook");
if (webhook.node("Enabled").getBoolean(false)) {
builder.setWebhookUsername(webhook.node("Username").getString());
builder.setWebhookAvatarUrl(webhook.node("AvatarUrl").getString());
}
List<DiscordMessageEmbed.Builder> embeds = node.node("Embeds").getList(DiscordMessageEmbed.Builder.class);
if (embeds != null) {
for (DiscordMessageEmbed.Builder embed : embeds) {
builder.addEmbed(embed.build());
}
}
builder.setContent(node.node("Content").getString());
return builder;
}
@Override
public void serialize(Type type, SendableDiscordMessage.@Nullable Builder obj, ConfigurationNode node) throws SerializationException {
if (obj == null) {
node.set(null);
return;
}
String webhookUsername = obj.getWebhookUsername();
if (webhookUsername != null) {
ConfigurationNode webhook = node.node("Webhook");
webhook.node("Username").set(webhookUsername);
webhook.node("AvatarUrl").set(Optional.ofNullable(obj.getWebhookAvatarUrl()).orElse(""));
}
List<DiscordMessageEmbed.Builder> embedBuilders = new ArrayList<>();
obj.getEmbeds().forEach(embed -> embedBuilders.add(embed.toBuilder()));
node.setList(DiscordMessageEmbed.Builder.class, embedBuilders);
node.node("Content").set(obj.getContent());
}
}