1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-11-10 02:21:25 +00:00

Complicate and simplify syntax for fields

This commit is contained in:
sciwhiz12 2020-11-30 12:53:27 +08:00
parent 0ff0f26811
commit cce35cea6e
Signed by: sciwhiz12
GPG Key ID: 622CF446534317E1
25 changed files with 179 additions and 382 deletions

View File

@ -1,31 +0,0 @@
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,20 +4,19 @@ 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;
import sciwhiz12.janitor.utils.DeserializerUtil;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import javax.annotation.Nullable;
import static java.util.Objects.requireNonNullElse;
import static sciwhiz12.janitor.utils.DeserializerUtil.readFields;
import static sciwhiz12.janitor.utils.DeserializerUtil.readText;
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);
} }
@ -30,7 +29,7 @@ public class ListingMessageDeserializer extends StdDeserializer<ListingMessage>
String title = null; String title = null;
String url = null; String url = null;
String description = DeserializerUtil.readText(root.path("description")); String description = 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;
@ -88,34 +87,8 @@ public class ListingMessageDeserializer extends StdDeserializer<ListingMessage>
case "description": { case "description": {
return new ListingMessage.DescriptionEntry( return new ListingMessage.DescriptionEntry(
root.path("joiner").asText(null), root.path("joiner").asText(null),
root.path("text").asText()); requireNonNullElse(DeserializerUtil.readText(root.path("text")), ""));
} }
} }
} }
public static List<MessageEmbed.Field> readFields(JsonNode node) {
if (node.isArray()) {
final ArrayList<MessageEmbed.Field> fields = new ArrayList<>();
for (int i = 0; i < node.size(); i++) {
final MessageEmbed.Field field = readField(node.path(i));
if (field != null) {
fields.add(field);
}
}
return fields;
}
return Collections.emptyList();
}
@Nullable
public static MessageEmbed.Field readField(JsonNode fieldNode) {
if (fieldNode.path("name").isTextual()) {
return new MessageEmbed.Field(
fieldNode.path("name").asText(),
DeserializerUtil.readText(fieldNode.path("value")),
fieldNode.path("inline").asBoolean(false)
);
}
return null;
}
} }

View File

@ -4,19 +4,14 @@ 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;
import sciwhiz12.janitor.utils.DeserializerUtil;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
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);
} }
@ -38,7 +33,7 @@ public class RegularMessageDeserializer extends StdDeserializer<RegularMessage>
String footerIconUrl = null; String footerIconUrl = null;
String imageUrl = node.path("image").asText(null); String imageUrl = node.path("image").asText(null);
String thumbnailUrl = node.path("thumbnail").asText(null); String thumbnailUrl = node.path("thumbnail").asText(null);
List<MessageEmbed.Field> fields = readFields(node); List<MessageEmbed.Field> fields = DeserializerUtil.readFields(node.path("fields"));
// Title // Title
if (node.path("title").isTextual()) { if (node.path("title").isTextual()) {
@ -68,30 +63,4 @@ public class RegularMessageDeserializer extends StdDeserializer<RegularMessage>
return new RegularMessage(title, url, description, color, authorName, authorUrl, return new RegularMessage(title, url, description, color, authorName, authorUrl,
authorIconUrl, footerText, footerIconUrl, imageUrl, thumbnailUrl, fields); authorIconUrl, footerText, footerIconUrl, imageUrl, thumbnailUrl, fields);
} }
public static List<MessageEmbed.Field> readFields(JsonNode node) {
if (node.path("fields").isArray()) {
final ArrayList<MessageEmbed.Field> fields = new ArrayList<>();
for (int i = 0; i < node.path("fields").size(); i++) {
final MessageEmbed.Field field = readField(node.path("fields").path(i));
if (field != null) {
fields.add(field);
}
}
return fields;
}
return Collections.emptyList();
}
@Nullable
public static MessageEmbed.Field readField(JsonNode fieldNode) {
if (fieldNode.path("name").isTextual()) {
return new MessageEmbed.Field(
fieldNode.path("name").asText(),
DeserializerUtil.readText(fieldNode.path("value")),
fieldNode.path("inline").asBoolean(false)
);
}
return null;
}
} }

View File

@ -0,0 +1,69 @@
package sciwhiz12.janitor.utils;
import com.fasterxml.jackson.databind.JsonNode;
import net.dv8tion.jda.api.entities.MessageEmbed;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import static java.util.stream.Collectors.joining;
import static net.dv8tion.jda.api.EmbedBuilder.ZERO_WIDTH_SPACE;
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;
}
public static List<MessageEmbed.Field> readFields(JsonNode root) {
List<MessageEmbed.Field> fields = new ArrayList<>();
if (root.isArray()) {
for (JsonNode node : root) {
if (node.isNull()) {
fields.add(new MessageEmbed.Field(ZERO_WIDTH_SPACE, ZERO_WIDTH_SPACE, false));
} else if (node.isObject()) {
fields.add(new MessageEmbed.Field(
node.path("name").asText(ZERO_WIDTH_SPACE),
node.path("value").asText(ZERO_WIDTH_SPACE),
node.path("inline").asBoolean(true)
));
}
}
} else if (root.isObject()) {
for (Iterator<String> names = root.fieldNames(); names.hasNext(); ) {
final String name = names.next();
final JsonNode node = root.get(name);
final String fieldName = node.path("name").asText(name);
String value = ZERO_WIDTH_SPACE;
if (node.isValueNode()) {
value = node.asText();
}
final String fieldValue = node.path("value").asText(value);
final boolean inline = node.path("inline").asBoolean(true);
fields.add(new MessageEmbed.Field(fieldName, fieldValue, inline));
}
}
return fields;
}
}

View File

@ -6,11 +6,7 @@
"_Use `${guild.command_prefix}commands` to get the list of enabled commands in this guild._", "_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." "\nRunning **${bot.name}**, version **${bot.version}**. Serving **${bot.guild_count}** guilds."
], ],
"fields": [ "fields": {
{ "Active Modules (${bot.modules.count})": "${bot.modules.names}"
"name": "Active Modules (${bot.modules.count})", }
"value": "${bot.modules.names}",
"inline": true
}
]
} }

View File

@ -1,11 +1,7 @@
{ {
"color": "${general.error.color}", "color": "${general.error.color}",
"description": "Cannot perform action on the given member, likely due to me being lower in the role hierarchy.", "description": "Cannot perform action on the given member, likely due to me being lower in the role hierarchy.",
"fields": [ "fields": {
{ "Target": "${target.mention}"
"name": "Target", }
"value": "${target.mention}",
"inline": true
}
]
} }

View File

@ -4,11 +4,7 @@
"I do not have sufficient permissions to carry out this action.", "I do not have sufficient permissions to carry out this action.",
"Please contact your server administrators if you believe this is in error." "Please contact your server administrators if you believe this is in error."
], ],
"fields": [ "fields": {
{ "Required permissions": "${required_permissions}"
"name": "Required permissions", }
"value": "${required_permissions}",
"inline": true
}
]
} }

View File

@ -5,16 +5,8 @@
"icon_url": "${performer.guild.icon_url}" "icon_url": "${performer.guild.icon_url}"
}, },
"title": "You were banned from this server.", "title": "You were banned from this server.",
"fields": [ "fields": {
{ "Moderator": "${performer.mention}",
"name": "Moderator", "Reason": "${nullcheck;reason;_No reason specified_}"
"value": "${performer.mention}", }
"inline": true
},
{
"name": "Reason",
"value": "${nullcheck;reason;_No reason specified._}",
"inline": true
}
]
} }

View File

@ -4,31 +4,14 @@
"name": "Banned user from server.", "name": "Banned user from server.",
"icon_url": "${moderation.icon_url}" "icon_url": "${moderation.icon_url}"
}, },
"fields": [ "fields": {
{ "Performer": "${performer.mention}",
"name": "Performer", "Target": "${target.mention}",
"value": "${performer.mention}", "Sent DM": "${private_message}",
"inline": true "Message Deletion": "${delete_duration} day(s)",
}, "Reason": {
{
"name": "Target",
"value": "${target.mention}",
"inline": true
},
{
"name": "Sent DM",
"value": "${private_message}",
"inline": true
},
{
"name": "Message Deletion",
"value": "${delete_duration} day(s)",
"inline": true
},
{
"name": "Reason",
"value": "${nullcheck;reason;_No reason specified._}", "value": "${nullcheck;reason;_No reason specified._}",
"inline": false "inline": false
} }
] }
} }

View File

@ -1,11 +1,7 @@
{ {
"color": "${general.error.color}", "color": "${general.error.color}",
"description": "The performer of this command cannot moderate the target user, likely due to being lower in the role hierarchy.", "description": "The performer of this command cannot moderate the target user, likely due to being lower in the role hierarchy.",
"fields": [ "fields": {
{ "Target": "${target.mention}"
"name": "Target", }
"value": "${target.mention}",
"inline": true
}
]
} }

View File

@ -1,11 +1,7 @@
{ {
"color": "${general.error.color}", "color": "${general.error.color}",
"description": "The performer of this command has insufficient permissions to use this command.", "description": "The performer of this command has insufficient permissions to use this command.",
"fields": [ "fields": {
{ "Required permissions": "${required_permissions}"
"name": "Required permissions", }
"value": "${required_permissions}",
"inline": true
}
]
} }

View File

@ -1,16 +1,8 @@
{ {
"color": "${general.error.color}", "color": "${general.error.color}",
"description": "The performer has reached the maximum amount of notes for the target user.", "description": "The performer has reached the maximum amount of notes for the target user.",
"fields": [ "fields": {
{ "Target": "${target.mention}",
"name": "Target", "(Max.) Amount": "${notes_amount}"
"value": "${target.mention}", }
"inline": true
},
{
"name": "(Max.) Amount",
"value": "${notes_amount}",
"inline": true
}
]
} }

View File

@ -1,16 +1,8 @@
{ {
"color": "${general.error.color}", "color": "${general.error.color}",
"title": "Cannot remove warning issued by higher-ranked moderator.", "title": "Cannot remove warning issued by higher-ranked moderator.",
"fields": [ "fields": {
{ "Performer": "${performer.mention}",
"name": "Performer", "Issuer": "${warning_entry.performer.mention}"
"value": "${performer.mention}", }
"inline": true
},
{
"name": "Issuer",
"value": "${warning_entry.performer.mention}",
"inline": true
}
]
} }

View File

@ -1,16 +1,8 @@
{ {
"color": "${general.error.color}", "color": "${general.error.color}",
"title": "Cannot remove warning from self.", "title": "Cannot remove warning from self.",
"fields": [ "fields": {
{ "Performer/Target": "${performer.mention}",
"name": "Performer/Target", "Issuer": "${warning_entry.performer.mention}"
"value": "${performer.mention}", }
"inline": true
},
{
"name": "Issuer",
"value": "${warning_entry.performer.mention}",
"inline": true
}
]
} }

View File

@ -1,11 +1,7 @@
{ {
"color": "${general.error.color}", "color": "${general.error.color}",
"title": "Cannot warn other moderators.", "title": "Cannot warn other moderators.",
"fields": [ "fields": {
{ "Target": "${target.mention}"
"name": "Target", }
"value": "${target.mention}",
"inline": true
}
]
} }

View File

@ -5,16 +5,8 @@
"icon_url": "${performer.guild.icon_url}" "icon_url": "${performer.guild.icon_url}"
}, },
"title": "You were kicked from this server.", "title": "You were kicked from this server.",
"fields": [ "fields": {
{ "Moderator": "${performer.mention}",
"name": "Moderator", "Reason": "${nullcheck;reason;_No reason specified._}"
"value": "${performer.mention}", }
"inline": true
},
{
"name": "Reason",
"value": "${nullcheck;reason;_No reason specified._}",
"inline": true
}
]
} }

View File

@ -4,26 +4,13 @@
"name": "Kicked user from server.", "name": "Kicked user from server.",
"icon_url": "${moderation.icon_url}" "icon_url": "${moderation.icon_url}"
}, },
"fields": [ "fields": {
{ "Performer": "${performer.mention}",
"name": "Performer", "Target": "${target.mention}",
"value": "${performer.mention}", "Sent DM": "${private_message}",
"inline": true "Reason": {
},
{
"name": "Target",
"value": "${target.mention}",
"inline": true
},
{
"name": "Sent DM",
"value": "${private_message}",
"inline": true
},
{
"name": "Reason",
"value": "${nullcheck;reason;_No reason specified._}", "value": "${nullcheck;reason;_No reason specified._}",
"inline": false "inline": false
} }
] }
} }

View File

@ -4,31 +4,14 @@
"name": "Recorded note for user.", "name": "Recorded note for user.",
"icon_url": "${moderation.icon_url}" "icon_url": "${moderation.icon_url}"
}, },
"fields": [ "fields": {
{ "Performer": "${note_entry.performer.mention}"
"name": "Performer", "Target": "${note_entry.target.mention}"
"value": "${note_entry.performer.mention}", "Note ID": "${note_entry.note_id}"
"inline": true "Date & Time": "${note_entry.date_time}",
}, "Text": {
{
"name": "Target",
"value": "${note_entry.target.mention}",
"inline": true
},
{
"name": "Note ID",
"value": "${note_entry.note_id}",
"inline": true
},
{
"name": "Date & Time",
"value": "${note_entry.date_time}",
"inline": true
},
{
"name": "Text",
"value": "${note_entry.contents}", "value": "${note_entry.contents}",
"inline": false "inline": false
} }
] }
} }

View File

@ -7,7 +7,11 @@
}, },
"entry": { "entry": {
"type": "description", "type": "description",
"text": "**#${note_entry.note_id}**: ${note_entry.target.mention} by ${note_entry.performer.mention}\n - _${note_entry.date_time}_\n${note_entry.contents}" "text": [
"**#${note_entry.note_id}**: ${note_entry.target.mention} by ${note_entry.performer.mention}",
" - _${note_entry.date_time}_",
"${note_entry.contents}"
]
}, },
"empty": "**_No recorded notes matching your query._**" "empty": "**_No recorded notes matching your query._**"
} }

View File

@ -4,36 +4,15 @@
"name": "Removed note.", "name": "Removed note.",
"icon_url": "${moderation.icon_url}" "icon_url": "${moderation.icon_url}"
}, },
"fields": [ "fields": {
{ "Performer": "${performer.mention}",
"name": "Performer", "Original Performer": "${note_entry.performer.mention}",
"value": "${performer.mention}", "Original Target": "${note_entry.target.mention}",
"inline": true "Note ID": "${note_entry.note_id}",
}, "Date & Time": "${note_entry.date_time}",
{ "Text": {
"name": "Original Performer",
"value": "${note_entry.performer.mention}",
"inline": true
},
{
"name": "Original Target",
"value": "${note_entry.target.mention}",
"inline": true
},
{
"name": "Note ID",
"value": "${note_entry.note_id}",
"inline": true
},
{
"name": "Date & Time",
"value": "${note_entry.date_time}",
"inline": true
},
{
"name": "Text",
"value": "${note_entry.contents}", "value": "${note_entry.contents}",
"inline": false "inline": false
} }
] }
} }

View File

@ -4,16 +4,8 @@
"name": "Unbanned user from server.", "name": "Unbanned user from server.",
"icon_url": "${moderation.icon_url}" "icon_url": "${moderation.icon_url}"
}, },
"fields": [ "fields": {
{ "Performer": "${performer.mention}",
"name": "Performer", "Target": "${target.mention}"
"value": "${performer.mention}", }
"inline": true
},
{
"name": "Target",
"value": "${target.mention}",
"inline": true
}
]
} }

View File

@ -4,36 +4,15 @@
"name": "Removed warning from user.", "name": "Removed warning from user.",
"icon_url": "${moderation.icon_url}" "icon_url": "${moderation.icon_url}"
}, },
"fields": [ "fields": {
{ "Performer": "${performer.mention}",
"name": "Performer", "Original Target": "${warning_entry.target.mention}",
"value": "${performer.mention}", "Original Performer": "${warning_entry.performer.mention}",
"inline": true "Case ID": "${warning_entry.case_id}",
}, "Date & Time": "${warning_entry.date_time}",
{ "Reason": {
"name": "Original Target",
"value": "${warning_entry.target.mention}",
"inline": true
},
{
"name": "Original Performer",
"value": "${warning_entry.performer.mention}",
"inline": true
},
{
"name": "Case ID",
"value": "${warning_entry.case_id}",
"inline": true
},
{
"name": "Date & Time",
"value": "${warning_entry.date_time}",
"inline": true
},
{
"name": "Reason",
"value": "${nullcheck;warning_entry.reason;_No reason specified._}", "value": "${nullcheck;warning_entry.reason;_No reason specified._}",
"inline": false "inline": false
} }
] }
} }

View File

@ -5,21 +5,12 @@
"icon_url": "${performer.guild.icon_url}" "icon_url": "${performer.guild.icon_url}"
}, },
"title": "You were warned by a moderator.", "title": "You were warned by a moderator.",
"fields": [ "fields": {
{ "Moderator": "${warning_entry.performer.mention}",
"name": "Moderator", "Date & Time": "${warning_entry.date_time}",
"value": "${warning_entry.performer.mention}", "Reason": {
"inline": true
},
{
"name": "Date & Time",
"value": "${warning_entry.date_time}",
"inline": true
},
{
"name": "Reason",
"value": "${nullcheck;warning_entry.reason;_No reason specified._}", "value": "${nullcheck;warning_entry.reason;_No reason specified._}",
"inline": false "inline": false
} }
] }
} }

View File

@ -4,36 +4,15 @@
"name": "Warned user.", "name": "Warned user.",
"icon_url": "${moderation.icon_url}" "icon_url": "${moderation.icon_url}"
}, },
"fields": [ "fields": {
{ "Performer": "${warning_entry.performer.mention}",
"name": "Performer", "Target": "${warning_entry.target.mention}",
"value": "${warning_entry.performer.mention}", "Case ID": "${warning_entry.case_id}",
"inline": true "Sent DM": "${private_message}",
}, "Date & Time": "${warning_entry.date_time}",
{ "Reason": {
"name": "Target",
"value": "${warning_entry.target.mention}",
"inline": true
},
{
"name": "Case ID",
"value": "${warning_entry.case_id}",
"inline": true
},
{
"name": "Sent DM",
"value": "${private_message}",
"inline": true
},
{
"name": "Date & Time",
"value": "${warning_entry.date_time}",
"inline": true
},
{
"name": "Reason",
"value": "${nullcheck;warning_entry.reason;_No reason specified._}", "value": "${nullcheck;warning_entry.reason;_No reason specified._}",
"inline": false "inline": false
} }
] }
} }

View File

@ -7,7 +7,11 @@
}, },
"entry": { "entry": {
"type": "description", "type": "description",
"text": "**Case #${warning_entry.case_id}**: Warned ${warning_entry.target.mention} by ${warning_entry.performer.mention} \n - _Date & Time:_ ${warning_entry.date_time} \n - _Reason:_ ${warning_entry.reason}" "text": [
"**Case #${warning_entry.case_id}**: Warned ${warning_entry.target.mention} by ${warning_entry.performer.mention}",
" - _Date & Time:_ ${warning_entry.date_time}",
" - _Reason:_ ${warning_entry.reason}"
]
}, },
"empty": "**_No warnings logged matching your query._**" "empty": "**_No warnings logged matching your query._**"
} }