From 2c3031dc75537eea67f05427c46437800ad1bf19 Mon Sep 17 00:00:00 2001 From: Vankka Date: Sat, 31 Jul 2021 03:11:19 +0300 Subject: [PATCH] Beginning of serializing Discord messages in the config --- .../com/discordsrv/api/DiscordSRVApi.java | 6 +- .../entity/message/DiscordMessageEmbed.java | 26 +++++++- .../config/manager/MainConfigManager.java | 18 +++--- .../manager/ConfigurateConfigManager.java | 26 ++++++-- .../DiscordMessageEmbedSerializer.java | 23 +++++++ .../SendableDiscordMessageSerializer.java | 64 +++++++++++++++++++ 6 files changed, 144 insertions(+), 19 deletions(-) create mode 100644 common/src/main/java/com/discordsrv/common/config/serializer/DiscordMessageEmbedSerializer.java create mode 100644 common/src/main/java/com/discordsrv/common/config/serializer/SendableDiscordMessageSerializer.java diff --git a/api/src/main/java/com/discordsrv/api/DiscordSRVApi.java b/api/src/main/java/com/discordsrv/api/DiscordSRVApi.java index d6b62dea..60321ba2 100644 --- a/api/src/main/java/com/discordsrv/api/DiscordSRVApi.java +++ b/api/src/main/java/com/discordsrv/api/DiscordSRVApi.java @@ -84,7 +84,11 @@ public interface DiscordSRVApi { * @return the JDA instance, if available * *

- * JDA is an external API, using DiscordSRV's APIs where possible is recommended. Please see JDA's deprecation policy. + * JDA is an external API, using DiscordSRV's APIs where possible is recommended. + * Please see JDA's deprecation policy, + * additionally DiscordSRV may update the major version of JDA, which will have breaking changes. + * + * @see #discordAPI() * @see #isReady() * @see #discordConnectionDetails() */ diff --git a/api/src/main/java/com/discordsrv/api/discord/api/entity/message/DiscordMessageEmbed.java b/api/src/main/java/com/discordsrv/api/discord/api/entity/message/DiscordMessageEmbed.java index bb349d97..9e53f65c 100644 --- a/api/src/main/java/com/discordsrv/api/discord/api/entity/message/DiscordMessageEmbed.java +++ b/api/src/main/java/com/discordsrv/api/discord/api/entity/message/DiscordMessageEmbed.java @@ -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 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); } diff --git a/common/src/main/java/com/discordsrv/common/config/manager/MainConfigManager.java b/common/src/main/java/com/discordsrv/common/config/manager/MainConfigManager.java index ae5163c6..b5d90b8c 100644 --- a/common/src/main/java/com/discordsrv/common/config/manager/MainConfigManager.java +++ b/common/src/main/java/com/discordsrv/common/config/manager/MainConfigManager.java @@ -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 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()); + }); } } diff --git a/common/src/main/java/com/discordsrv/common/config/manager/manager/ConfigurateConfigManager.java b/common/src/main/java/com/discordsrv/common/config/manager/manager/ConfigurateConfigManager.java index e7afd17b..112603c8 100644 --- a/common/src/main/java/com/discordsrv/common/config/manager/manager/ConfigurateConfigManager.java +++ b/common/src/main/java/com/discordsrv/common/config/manager/manager/ConfigurateConfigManager.java @@ -44,6 +44,8 @@ public abstract class ConfigurateConfigManager (value1, destination) -> { String[] children = data.value(); boolean whitelist = data.whitelist(); @@ -124,9 +132,13 @@ public abstract class ConfigurateConfigManager) defaultConfig.getClass()) .load(node); } catch (ConfigurateException e) { diff --git a/common/src/main/java/com/discordsrv/common/config/serializer/DiscordMessageEmbedSerializer.java b/common/src/main/java/com/discordsrv/common/config/serializer/DiscordMessageEmbedSerializer.java new file mode 100644 index 00000000..d3ead0d6 --- /dev/null +++ b/common/src/main/java/com/discordsrv/common/config/serializer/DiscordMessageEmbedSerializer.java @@ -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 { + + @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 + } +} diff --git a/common/src/main/java/com/discordsrv/common/config/serializer/SendableDiscordMessageSerializer.java b/common/src/main/java/com/discordsrv/common/config/serializer/SendableDiscordMessageSerializer.java new file mode 100644 index 00000000..edcf048a --- /dev/null +++ b/common/src/main/java/com/discordsrv/common/config/serializer/SendableDiscordMessageSerializer.java @@ -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 { + + @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 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 embedBuilders = new ArrayList<>(); + obj.getEmbeds().forEach(embed -> embedBuilders.add(embed.toBuilder())); + node.setList(DiscordMessageEmbed.Builder.class, embedBuilders); + + node.node("Content").set(obj.getContent()); + } +}