1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-09-19 23:24:03 +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.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;
import sciwhiz12.janitor.utils.DeserializerUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
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 static final Joiner NEWLINE = Joiner.on('\n');
public ListingMessageDeserializer() {
super(ListingMessage.class);
}
@ -30,7 +29,7 @@ public class ListingMessageDeserializer extends StdDeserializer<ListingMessage>
String title = 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 authorName = null;
String authorUrl = null;
@ -88,34 +87,8 @@ public class ListingMessageDeserializer extends StdDeserializer<ListingMessage>
case "description": {
return new ListingMessage.DescriptionEntry(
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.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;
import sciwhiz12.janitor.utils.DeserializerUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
public class RegularMessageDeserializer extends StdDeserializer<RegularMessage> {
public static final Joiner NEWLINE = Joiner.on('\n');
public RegularMessageDeserializer() {
super(RegularMessage.class);
}
@ -38,7 +33,7 @@ public class RegularMessageDeserializer extends StdDeserializer<RegularMessage>
String footerIconUrl = null;
String imageUrl = node.path("image").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
if (node.path("title").isTextual()) {
@ -68,30 +63,4 @@ public class RegularMessageDeserializer extends StdDeserializer<RegularMessage>
return new RegularMessage(title, url, description, color, authorName, authorUrl,
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._",
"\nRunning **${bot.name}**, version **${bot.version}**. Serving **${bot.guild_count}** guilds."
],
"fields": [
{
"name": "Active Modules (${bot.modules.count})",
"value": "${bot.modules.names}",
"inline": true
"fields": {
"Active Modules (${bot.modules.count})": "${bot.modules.names}"
}
]
}

View File

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

View File

@ -4,11 +4,7 @@
"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",
"value": "${required_permissions}",
"inline": true
"fields": {
"Required permissions": "${required_permissions}"
}
]
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,11 @@
},
"entry": {
"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._**"
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,11 @@
},
"entry": {
"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._**"
}