mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2025-01-13 20:11:24 +01:00
Beginning of serializing Discord messages in the config
This commit is contained in:
parent
208d124fea
commit
2c3031dc75
@ -84,7 +84,11 @@ public interface DiscordSRVApi {
|
|||||||
* @return the JDA instance, if available
|
* @return the JDA instance, if available
|
||||||
*
|
*
|
||||||
* <p>
|
* <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 #isReady()
|
||||||
* @see #discordConnectionDetails()
|
* @see #discordConnectionDetails()
|
||||||
*/
|
*/
|
||||||
|
@ -153,6 +153,10 @@ public class DiscordMessageEmbed {
|
|||||||
return footerImageUrl;
|
return footerImageUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder toBuilder() {
|
||||||
|
return new Builder(this);
|
||||||
|
}
|
||||||
|
|
||||||
public MessageEmbed toJDA() {
|
public MessageEmbed toJDA() {
|
||||||
EmbedBuilder embedBuilder = new EmbedBuilder();
|
EmbedBuilder embedBuilder = new EmbedBuilder();
|
||||||
embedBuilder.setColor(color);
|
embedBuilder.setColor(color);
|
||||||
@ -208,11 +212,11 @@ public class DiscordMessageEmbed {
|
|||||||
private OffsetDateTime timestamp;
|
private OffsetDateTime timestamp;
|
||||||
private String footer, footerImageUrl;
|
private String footer, footerImageUrl;
|
||||||
|
|
||||||
public Builder() {
|
protected Builder() {
|
||||||
this.fields = new ArrayList<>();
|
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,
|
String titleUrl, String description, List<Field> fields, String thumbnailUrl, String imageUrl,
|
||||||
OffsetDateTime timestamp, String footer, String footerImageUrl) {
|
OffsetDateTime timestamp, String footer, String footerImageUrl) {
|
||||||
this.color = color;
|
this.color = color;
|
||||||
@ -230,6 +234,22 @@ public class DiscordMessageEmbed {
|
|||||||
this.footerImageUrl = footerImageUrl;
|
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() {
|
public int getColor() {
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
@ -390,7 +410,7 @@ public class DiscordMessageEmbed {
|
|||||||
|
|
||||||
@SuppressWarnings({"MethodDoesntCallSuperMethod", "CloneDoesntDeclareCloneNotSupportedException"})
|
@SuppressWarnings({"MethodDoesntCallSuperMethod", "CloneDoesntDeclareCloneNotSupportedException"})
|
||||||
@Override
|
@Override
|
||||||
protected Builder clone() {
|
public Builder clone() {
|
||||||
return new Builder(color, authorName, authorUrl, authorImageUrl, title, titleUrl, description,
|
return new Builder(color, authorName, authorUrl, authorImageUrl, title, titleUrl, description,
|
||||||
fields, thumbnailUrl, imageUrl, timestamp, footer, footerImageUrl);
|
fields, thumbnailUrl, imageUrl, timestamp, footer, footerImageUrl);
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,15 @@
|
|||||||
|
|
||||||
package com.discordsrv.common.config.manager;
|
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.DiscordSRV;
|
||||||
import com.discordsrv.common.config.main.MainConfig;
|
import com.discordsrv.common.config.main.MainConfig;
|
||||||
import com.discordsrv.common.config.main.channels.ChannelConfigHolder;
|
import com.discordsrv.common.config.main.channels.ChannelConfigHolder;
|
||||||
import com.discordsrv.common.config.manager.loader.YamlConfigLoaderProvider;
|
import com.discordsrv.common.config.manager.loader.YamlConfigLoaderProvider;
|
||||||
import com.discordsrv.common.config.manager.manager.TranslatedConfigManager;
|
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.ConfigurationOptions;
|
||||||
import org.spongepowered.configurate.objectmapping.ObjectMapper;
|
import org.spongepowered.configurate.objectmapping.ObjectMapper;
|
||||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||||
@ -40,16 +44,14 @@ public abstract class MainConfigManager<C extends MainConfig>
|
|||||||
return MainConfig.FILE_NAME;
|
return MainConfig.FILE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ObjectMapper.Factory.Builder objectMapper() {
|
|
||||||
return super.objectMapper();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConfigurationOptions defaultOptions() {
|
public ConfigurationOptions defaultOptions() {
|
||||||
ChannelConfigHolder.Serializer channelConfigSerializer = new ChannelConfigHolder.Serializer(objectMapper().build());
|
|
||||||
return YamlConfigLoaderProvider.super.defaultOptions()
|
return YamlConfigLoaderProvider.super.defaultOptions()
|
||||||
.serializers(builder ->
|
.serializers(builder -> {
|
||||||
builder.register(ChannelConfigHolder.class, channelConfigSerializer));
|
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());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,8 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
|
|||||||
protected final DiscordSRV discordSRV;
|
protected final DiscordSRV discordSRV;
|
||||||
private final Path filePath;
|
private final Path filePath;
|
||||||
private final LT loader;
|
private final LT loader;
|
||||||
|
private final ObjectMapper.Factory configObjectMapper;
|
||||||
|
private final ObjectMapper.Factory defaultObjectMapper;
|
||||||
|
|
||||||
protected T configuration;
|
protected T configuration;
|
||||||
|
|
||||||
@ -51,6 +53,8 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
|
|||||||
this.discordSRV = discordSRV;
|
this.discordSRV = discordSRV;
|
||||||
this.filePath = new File(discordSRV.dataDirectory().toFile(), fileName()).toPath();
|
this.filePath = new File(discordSRV.dataDirectory().toFile(), fileName()).toPath();
|
||||||
this.loader = createLoader(filePath, configNodeOptions());
|
this.loader = createLoader(filePath, configNodeOptions());
|
||||||
|
this.configObjectMapper = configObjectMapperBuilder().build();
|
||||||
|
this.defaultObjectMapper = defaultObjectMapperBuilder().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Path filePath() {
|
public Path filePath() {
|
||||||
@ -73,7 +77,7 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
|
|||||||
.shouldCopyDefaults(false);
|
.shouldCopyDefaults(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObjectMapper.Factory.Builder objectMapper() {
|
protected ObjectMapper.Factory.Builder objectMapperBuilder() {
|
||||||
return ObjectMapper.factoryBuilder();
|
return ObjectMapper.factoryBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,16 +85,20 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
|
|||||||
return defaultOptions();
|
return defaultOptions();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObjectMapper.Factory.Builder configObjectMapper() {
|
protected ObjectMapper.Factory.Builder configObjectMapperBuilder() {
|
||||||
return objectMapper();
|
return objectMapperBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectMapper.Factory configObjectMapper() {
|
||||||
|
return configObjectMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConfigurationOptions defaultNodeOptions() {
|
public ConfigurationOptions defaultNodeOptions() {
|
||||||
return defaultOptions();
|
return defaultOptions();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObjectMapper.Factory.Builder defaultObjectMapper() {
|
protected ObjectMapper.Factory.Builder defaultObjectMapperBuilder() {
|
||||||
return configObjectMapper()
|
return configObjectMapperBuilder()
|
||||||
.addProcessor(DefaultOnly.class, (data, value) -> (value1, destination) -> {
|
.addProcessor(DefaultOnly.class, (data, value) -> (value1, destination) -> {
|
||||||
String[] children = data.value();
|
String[] children = data.value();
|
||||||
boolean whitelist = data.whitelist();
|
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 {
|
private CommentedConfigurationNode getDefault(T defaultConfig, boolean cleanMapper) throws SerializationException {
|
||||||
CommentedConfigurationNode node = CommentedConfigurationNode.root(defaultNodeOptions());
|
CommentedConfigurationNode node = CommentedConfigurationNode.root(defaultNodeOptions());
|
||||||
(cleanMapper ? defaultObjectMapper() : configObjectMapper()).build()
|
(cleanMapper ? defaultObjectMapper() : configObjectMapper())
|
||||||
.get(defaultConfig.getClass()).load(node);
|
.get(defaultConfig.getClass()).load(node);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -164,7 +176,7 @@ public abstract class ConfigurateConfigManager<T, LT extends AbstractConfigurati
|
|||||||
node = getDefault(defaultConfig, false);
|
node = getDefault(defaultConfig, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.configuration = configObjectMapper().build()
|
this.configuration = configObjectMapper()
|
||||||
.get((Class<T>) defaultConfig.getClass())
|
.get((Class<T>) defaultConfig.getClass())
|
||||||
.load(node);
|
.load(node);
|
||||||
} catch (ConfigurateException e) {
|
} catch (ConfigurateException e) {
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user