1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-09-19 23:24:03 +00:00

Allow arrays and objects in certain text fields in messages

This commit is contained in:
sciwhiz12 2020-11-30 11:23:25 +08:00
parent eebb101603
commit 0ff0f26811
Signed by: sciwhiz12
GPG Key ID: 622CF446534317E1
6 changed files with 56 additions and 9 deletions

View File

@ -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;
}
}

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; 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.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import sciwhiz12.janitor.api.messages.ListingMessage; import sciwhiz12.janitor.api.messages.ListingMessage;
@ -15,6 +16,8 @@ import java.util.List;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class ListingMessageDeserializer extends StdDeserializer<ListingMessage> { public class ListingMessageDeserializer extends StdDeserializer<ListingMessage> {
public static final Joiner NEWLINE = Joiner.on('\n');
public ListingMessageDeserializer() { public ListingMessageDeserializer() {
super(ListingMessage.class); super(ListingMessage.class);
} }
@ -27,7 +30,7 @@ public class ListingMessageDeserializer extends StdDeserializer<ListingMessage>
String title = null; String title = null;
String url = 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 color = root.path("color").asText(null);
String authorName = null; String authorName = null;
String authorUrl = null; String authorUrl = null;
@ -106,10 +109,10 @@ public class ListingMessageDeserializer extends StdDeserializer<ListingMessage>
@Nullable @Nullable
public static MessageEmbed.Field readField(JsonNode fieldNode) { 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( return new MessageEmbed.Field(
fieldNode.path("name").asText(), fieldNode.path("name").asText(),
fieldNode.path("value").asText(), DeserializerUtil.readText(fieldNode.path("value")),
fieldNode.path("inline").asBoolean(false) fieldNode.path("inline").asBoolean(false)
); );
} }

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.google.common.base.Joiner;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import sciwhiz12.janitor.api.messages.RegularMessage; import sciwhiz12.janitor.api.messages.RegularMessage;
@ -14,6 +15,8 @@ import java.util.List;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class RegularMessageDeserializer extends StdDeserializer<RegularMessage> { public class RegularMessageDeserializer extends StdDeserializer<RegularMessage> {
public static final Joiner NEWLINE = Joiner.on('\n');
public RegularMessageDeserializer() { public RegularMessageDeserializer() {
super(RegularMessage.class); super(RegularMessage.class);
} }
@ -26,7 +29,7 @@ public class RegularMessageDeserializer extends StdDeserializer<RegularMessage>
String title = null; String title = null;
String url = 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 color = node.path("color").asText(null);
String authorName = null; String authorName = null;
String authorUrl = null; String authorUrl = null;
@ -82,10 +85,10 @@ public class RegularMessageDeserializer extends StdDeserializer<RegularMessage>
@Nullable @Nullable
public static MessageEmbed.Field readField(JsonNode fieldNode) { 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( return new MessageEmbed.Field(
fieldNode.path("name").asText(), fieldNode.path("name").asText(),
fieldNode.path("value").asText(), DeserializerUtil.readText(fieldNode.path("value")),
fieldNode.path("inline").asBoolean(false) fieldNode.path("inline").asBoolean(false)
); );
} }

View File

@ -1,7 +1,11 @@
{ {
"title": "${bot.member.name}", "title": "${bot.member.name}",
"thumbnail": "${bot.member.avatar}", "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": [ "fields": [
{ {
"name": "Active Modules (${bot.modules.count})", "name": "Active Modules (${bot.modules.count})",

View File

@ -1,5 +1,8 @@
{ {
"color": "${general.error.color}", "color": "${general.error.color}",
"title": "Ambiguous member argument!", "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."
]
} }

View File

@ -1,6 +1,9 @@
{ {
"color": "${general.error.color}", "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": [ "fields": [
{ {
"name": "Required permissions", "name": "Required permissions",