JDA:如何从 JSON 文件创建 MessageEmbed?

JDA: How to create MessageEmbed from JSON file?

我想在一条消息中发送两个嵌入。这应该可以用 MessageChannel#sendMessageEmbeds(java.util.Collection).
另外,我已经像这样嵌入 JSON file

{
    "author": {
      "name": "I use this as title because why not",
      "icon_url": "url for icon"
    },
    "description": "description that goes top of the embed",
    "color": 65535,
    "fields": [
      {
        "name": "field name 01",
        "value": "field value 01 which *can* ||have|| __some__ markdown stuff",
        "inline": false
      },
      {
        "name": "field name 02",
        "value": "yes, thie fileds can have more than one filed",
        "inline": false
      },
      {
        "name": "field name 03",
        "value": "blahblah some texts",
        "inline": true
      },
    ],
    "thumbnail": {
      "url": "url for thumbnail"
    },
    "footer": {
      "text": "footer text because it is cool",
      "icon_url": "this would be same as author icon_url but this may vary sometimes"
    }
  }

问题是,我想不出'how to convert JSON file to MessageEmbed object'。我在 JDA discord 服务器中询问,搜索了 Google 一些提示,但仍然想不出正确的方法。

长话短说,我有 JSON 文件用于嵌入,我想 convert JSON 文件到 MessageEmbed 对象,这样我可以发到Discord频道。

也许有一种将 JSON 转换为 MessageEmbeds 的内置方法,如果是这样,那我就浪费了时间,我自己找不到。

下面的方法采用 JsonObject 并将其有效成员转换为 MessageEmbed,其中包含您在 Json 和 title 中列出的所有类别,因为我注意到你没有添加它。 如果您想添加更多内容,欢迎使用它。

注意:我使用了 Google 的 GSON 库来执行此操作,如果您想要使用我过去使用的方法,则必须将其添加到您的项目中。要了解更多信息,请参阅 here

方法代码:

/**
 * Converts a {@link JsonObject} to {@link MessageEmbed}.
 * Supported Fields: Title, Author, Description, Color, Fields, Thumbnail, Footer.
 * 
 * @param json The JsonObject
 * @return The Embed
 */
public static MessageEmbed jsonToEmbed(JsonObject json){
    EmbedBuilder embedBuilder = new EmbedBuilder();

    JsonPrimitive titleObj = json.getAsJsonPrimitive("title");
    if (titleObj != null){ // Make sure the object is not null before adding it onto the embed.
        embedBuilder.setTitle(titleObj.getAsString());
    }

    JsonObject authorObj = json.getAsJsonObject("author");
    if (authorObj != null) {
        String authorName = authorObj.get("name").getAsString();
        String authorIconUrl = authorObj.get("icon_url").getAsString();
        if (authorIconUrl != null) // Make sure the icon_url is not null before adding it onto the embed. If its null then add just the author's name.
            embedBuilder.setAuthor(authorName, authorIconUrl);
        else
            embedBuilder.setAuthor(authorName);
    }

    JsonPrimitive descObj = json.getAsJsonPrimitive("description");
    if (descObj != null){
        embedBuilder.setDescription(descObj.getAsString());
    }

    JsonPrimitive colorObj = json.getAsJsonPrimitive("color");
    if (colorObj != null){
        Color color = new Color(colorObj.getAsInt());
        embedBuilder.setColor(color);
    }

    JsonArray fieldsArray = json.getAsJsonArray("fields");
    if (fieldsArray != null) {
        // Loop over the fields array and add each one by order to the embed.
        fieldsArray.forEach(ele -> {
            String name = ele.getAsJsonObject().get("name").getAsString();
            String content = ele.getAsJsonObject().get("value").getAsString();
            boolean inline = ele.getAsJsonObject().get("inline").getAsBoolean();
            embedBuilder.addField(name, content, inline);
        });
    }

    JsonPrimitive thumbnailObj = json.getAsJsonPrimitive("thumbnail");
    if (thumbnailObj != null){
        embedBuilder.setThumbnail(thumbnailObj.getAsString());
    }

    JsonObject footerObj = json.getAsJsonObject("footer");
    if (footerObj != null){
        String content = footerObj.get("text").getAsString();
        String footerIconUrl = footerObj.get("icon_url").getAsString();

        if (footerIconUrl != null)
            embedBuilder.setFooter(content, footerIconUrl);
        else
            embedBuilder.setFooter(content);
    }

    return embedBuilder.build();
}

使用方法:

textChannel.sendMessageEmbeds(jsonToEmbed((JsonObject) JsonParser.parseReader(new FileReader("jsonfile.json")))).queue();

它使用我的测试打印的内容 JSON: