From 0ff0f26811577b9fd098deb62b03144276b3955b Mon Sep 17 00:00:00 2001 From: sciwhiz12 Date: Mon, 30 Nov 2020 11:23:25 +0800 Subject: [PATCH] Allow arrays and objects in certain text fields in messages --- .../messages/json/DeserializerUtil.java | 31 +++++++++++++++++++ .../json/ListingMessageDeserializer.java | 9 ++++-- .../json/RegularMessageDeserializer.java | 9 ++++-- .../resources/messages/general/about.json | 6 +++- .../general/error/ambiguous_member.json | 5 ++- .../error/insufficient_permissions.json | 5 ++- 6 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 core/src/main/java/sciwhiz12/janitor/messages/json/DeserializerUtil.java diff --git a/core/src/main/java/sciwhiz12/janitor/messages/json/DeserializerUtil.java b/core/src/main/java/sciwhiz12/janitor/messages/json/DeserializerUtil.java new file mode 100644 index 0000000..bb5d178 --- /dev/null +++ b/core/src/main/java/sciwhiz12/janitor/messages/json/DeserializerUtil.java @@ -0,0 +1,31 @@ +package sciwhiz12.janitor.messages.json; + +import com.fasterxml.jackson.databind.JsonNode; + +import java.util.stream.StreamSupport; +import javax.annotation.Nullable; + +import static java.util.stream.Collectors.joining; + +public final class DeserializerUtil { + public static final String NEWLINE = "\n"; + + private DeserializerUtil() {} + + @Nullable + public static String readText(JsonNode node) { + if (node.isTextual() || node.isValueNode()) { + return node.asText(); + } else if (node.isArray()) { + return StreamSupport.stream(node.spliterator(), false) + .map(JsonNode::asText) + .collect(joining(NEWLINE)); + } else if (node.isObject()) { + final String joiner = node.path("joiner").asText(NEWLINE); + return StreamSupport.stream(node.path("text").spliterator(), false) + .map(JsonNode::asText) + .collect(joining(NEWLINE)); + } + return null; + } +} diff --git a/core/src/main/java/sciwhiz12/janitor/messages/json/ListingMessageDeserializer.java b/core/src/main/java/sciwhiz12/janitor/messages/json/ListingMessageDeserializer.java index e94d538..88dac31 100644 --- a/core/src/main/java/sciwhiz12/janitor/messages/json/ListingMessageDeserializer.java +++ b/core/src/main/java/sciwhiz12/janitor/messages/json/ListingMessageDeserializer.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.google.common.base.Joiner; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.MessageEmbed; import sciwhiz12.janitor.api.messages.ListingMessage; @@ -15,6 +16,8 @@ import java.util.List; import javax.annotation.Nullable; public class ListingMessageDeserializer extends StdDeserializer { + public static final Joiner NEWLINE = Joiner.on('\n'); + public ListingMessageDeserializer() { super(ListingMessage.class); } @@ -27,7 +30,7 @@ public class ListingMessageDeserializer extends StdDeserializer String title = null; String url = null; - String description = root.path("description").asText(null); + String description = DeserializerUtil.readText(root.path("description")); String color = root.path("color").asText(null); String authorName = null; String authorUrl = null; @@ -106,10 +109,10 @@ public class ListingMessageDeserializer extends StdDeserializer @Nullable public static MessageEmbed.Field readField(JsonNode fieldNode) { - if (fieldNode.path("name").isTextual() && fieldNode.path("value").isTextual()) { + if (fieldNode.path("name").isTextual()) { return new MessageEmbed.Field( fieldNode.path("name").asText(), - fieldNode.path("value").asText(), + DeserializerUtil.readText(fieldNode.path("value")), fieldNode.path("inline").asBoolean(false) ); } diff --git a/core/src/main/java/sciwhiz12/janitor/messages/json/RegularMessageDeserializer.java b/core/src/main/java/sciwhiz12/janitor/messages/json/RegularMessageDeserializer.java index 536de5e..359dfb2 100644 --- a/core/src/main/java/sciwhiz12/janitor/messages/json/RegularMessageDeserializer.java +++ b/core/src/main/java/sciwhiz12/janitor/messages/json/RegularMessageDeserializer.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.google.common.base.Joiner; import net.dv8tion.jda.api.entities.MessageEmbed; import sciwhiz12.janitor.api.messages.RegularMessage; @@ -14,6 +15,8 @@ import java.util.List; import javax.annotation.Nullable; public class RegularMessageDeserializer extends StdDeserializer { + public static final Joiner NEWLINE = Joiner.on('\n'); + public RegularMessageDeserializer() { super(RegularMessage.class); } @@ -26,7 +29,7 @@ public class RegularMessageDeserializer extends StdDeserializer String title = null; String url = null; - String description = node.path("description").asText(null); + String description = DeserializerUtil.readText(node.path("description")); String color = node.path("color").asText(null); String authorName = null; String authorUrl = null; @@ -82,10 +85,10 @@ public class RegularMessageDeserializer extends StdDeserializer @Nullable public static MessageEmbed.Field readField(JsonNode fieldNode) { - if (fieldNode.path("name").isTextual() && fieldNode.path("value").isTextual()) { + if (fieldNode.path("name").isTextual()) { return new MessageEmbed.Field( fieldNode.path("name").asText(), - fieldNode.path("value").asText(), + DeserializerUtil.readText(fieldNode.path("value")), fieldNode.path("inline").asBoolean(false) ); } diff --git a/core/src/main/resources/messages/general/about.json b/core/src/main/resources/messages/general/about.json index 35e88b4..5e4df7e 100644 --- a/core/src/main/resources/messages/general/about.json +++ b/core/src/main/resources/messages/general/about.json @@ -1,7 +1,11 @@ { "title": "${bot.member.name}", "thumbnail": "${bot.member.avatar}", - "description": "A general and extensible housekeeping bot. Created by SciWhiz12.\n_Use `${guild.command_prefix}commands` to get the list of enabled commands in this guild._\n\nRunning **${bot.name}**, version **${bot.version}**. Serving **${bot.guild_count}** guilds.", + "description": [ + "A general and extensible housekeeping bot. Created by SciWhiz12.", + "_Use `${guild.command_prefix}commands` to get the list of enabled commands in this guild._", + "\nRunning **${bot.name}**, version **${bot.version}**. Serving **${bot.guild_count}** guilds." + ], "fields": [ { "name": "Active Modules (${bot.modules.count})", diff --git a/core/src/main/resources/messages/general/error/ambiguous_member.json b/core/src/main/resources/messages/general/error/ambiguous_member.json index fd9db2f..716ee14 100644 --- a/core/src/main/resources/messages/general/error/ambiguous_member.json +++ b/core/src/main/resources/messages/general/error/ambiguous_member.json @@ -1,5 +1,8 @@ { "color": "${general.error.color}", "title": "Ambiguous member argument!", - "description": "The name you have specified is too ambiguous (leads to more than 1 member)!\nPlease narrow down the specified name until it can uniquely identify a member of this guild." + "description": [ + "The name you have specified is too ambiguous (leads to more than 1 member)!", + "Please narrow down the specified name until it can uniquely identify a member of this guild." + ] } \ No newline at end of file diff --git a/core/src/main/resources/messages/general/error/insufficient_permissions.json b/core/src/main/resources/messages/general/error/insufficient_permissions.json index 7c8ffb0..4d3dd72 100644 --- a/core/src/main/resources/messages/general/error/insufficient_permissions.json +++ b/core/src/main/resources/messages/general/error/insufficient_permissions.json @@ -1,6 +1,9 @@ { "color": "${general.error.color}", - "description": "I do not have sufficient permissions to carry out this action.\nPlease contact your server administrators if you believe this is in error.", + "description": [ + "I do not have sufficient permissions to carry out this action.", + "Please contact your server administrators if you believe this is in error." + ], "fields": [ { "name": "Required permissions",