【问题标题】:JDA: How to create MessageEmbed from JSON file?JDA:如何从 JSON 文件创建 MessageEmbed?
【发布时间】:2021-10-18 08:11:49
【问题描述】:

我想在一封邮件中发送两个嵌入。 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"
    }
  }

问题是,我想不出'如何convert JSON 文件到MessageEmbed 对象'。我在 JDA discord 服务器中询问,在 Google 上搜索了一些提示,但仍然想不出正确的方法。

长话短说,我有用于嵌入的 JSON 文件,我想将 JSON 文件转换MessageEmbed 对象,以便将其发送到 Discord 频道。

【问题讨论】:

    标签: java json discord-jda


    【解决方案1】:

    也许有一种将 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 打印的内容:

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 2021-06-10
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多