【问题标题】:Add a field to a c# embedded discord message将字段添加到 c# 嵌入式不和谐消息
【发布时间】:2020-04-12 05:36:50
【问题描述】:

在我的 Wpf C# 文档中,我需要找到一种将嵌入消息发送到 webhook 的方法,我在互联网上找到了一些示例并将它们放入我的文件中。尽管如此,我仍然找不到如何在嵌入消息中添加字段,这里是代码:

public class Json
{
    // 'Username' value
    [JsonProperty("username")]
    public string username { get; set; }
    // 'Avatar' value
    [JsonProperty("avatar_url")]
    // 'Content' value --> Always empty because we are using embed
    public string avatarurl { get; set; }
    [JsonProperty("content")]
    public string content { get; set; }
    // 'Embed' array value
    [JsonProperty("embeds")]
    public List<Embed> embeds { get; set; }
}

/*
   Json class to compile the single embed
   Action: n/a
*/
public class Embed
{
    [JsonProperty("author")]
    public Author author { get; set; }
    [JsonProperty("title")]
    public string title { get; set; }
    [JsonProperty("url")]
    public string url { get; set; }
    [JsonProperty("description")]
    public string description { get; set; }
    [JsonProperty("color")]
    public int color { get; set; }
    [JsonProperty("fields")]
    public List<Fields> fields { get; set; }
    [JsonProperty("thumbnail")]
    public Thumbnail thumbnail { get; set; }
    [JsonProperty("image")]
    public Image image { get; set; }
    [JsonProperty("footer")]
    public Footer footer { get; set; }

}

/*
   Json class to compile the author in an embed
   Action: n/a
*/
public class Author
{
    [JsonProperty("name")]
    public string name { get; set; }
    [JsonProperty("url")]
    public string url { get; set; }
    [JsonProperty("icon_url")]
    public string iconurl { get; set; }
}


/*
   Json class to compile the fields in an embed
   Action: n/a
*/


/*
   Json class to compile the thumbnail in an embed
   Action: n/a
*/
public class Thumbnail
{
    [JsonProperty("url")]
    public string url { get; set; }
}

/*
   Json class to compile the image in an embed
   Action: n/a
*/
public class Image
{
    [JsonProperty("url")]
    public string url { get; set; }
}

/*
   Json class to compile the footer in an embed
   Action: n/a
*/
public class Footer
{
    [JsonProperty("text")]
    public string text { get; set; }
    [JsonProperty("icon_url")]
    public string iconurl { get; set; }
}

/*
   Json class to compile the config
   Action: CONFIG
*/
public class Config
{
    [JsonProperty("webhook_url")]
    public string webhook { get; set; }
    [JsonProperty("json")]
    public Json json { get; set; }
}
public class Fields
{
}

然后,在 EmbedBuilder.cs 类中,我有:

public class EmbedBuilder
{
    /*
        Builds the JSON according to the local values
        Action: COMPILE/EXECUTE
    */
    public static void buildEmbed()
    {
        Json json = new Json()
        {
            embeds = new List<Embed>()
            {
                new Embed
                {
                    author = new Author
                    {
                        name = "",
                        iconurl = "",
                    },
                    title = "",
                    url = "",
                    color = 14957895,
                    description = "",
                    thumbnail = new Thumbnail
                    {
                        url = "",
                    },
                    image = new Image
                    {
                        url = ""
                    },
                    footer = new Footer
                    {
                        text = "",
                        iconurl = "",
                    }
                }
            }

        };
        // Call the HTTP client and execute request
        HTTP.MakeRequest(Properties.Settings.Default.discordhook, Newtonsoft.Json.JsonConvert.SerializeObject(json));

    }

    /*
        Compiles the local values into the format provided in Json.cs. Must have run the webhook once (soon will be dynamic)
        Action: COMPILE/SAVE
    */
    public static string saveEmbed()
    {
        // Make a new config
        Config json = new Config()
        {
            webhook = Properties.Settings.Default.discordhook,
            json = new Json
            {
                embeds = new List<Embed>()
                {
                    new Embed
                    {
                        author = new Author
                        {
                            name = "",
                            iconurl = "",
                        },
                        title = "",
                        url = "",
                        color = ,
                        description = "",
                        thumbnail = new Thumbnail
                        {
                            url = "",
                        },
                        image = new Image
                        {
                            url = ""
                        },
                        footer = new Footer
                        {
                            text = "",
                            iconurl = "",
                        }
                    }
                }
            }
        };
        // Return a serialized value
        return JsonConvert.SerializeObject(json);

    }
}

我怎样才能在我的消息中添加一个不和谐的嵌入字段,这是一张图片:

这只是我在互联网上找到的一个示例,我并不是专门寻找如何完全创建相同的东西,只是关于如何添加字段。

【问题讨论】:

    标签: c# wpf embed discord


    【解决方案1】:

    在对 discord 开发者门户网站进行了一些关于嵌入的研究 (https://birdie0.github.io/discord-webhooks-guide/structure/embed/fields.html) 之后,我设法做到了。主要问题是我没有将它设置为数组,这是正确的代码:

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WpfApp1
    {
    
    /*
       Json class to compile the full embed
       Action: n/a
    */
    public class Json
    {
        // 'Username' value
        [JsonProperty("username")]
        public string username { get; set; }
        // 'Avatar' value
        [JsonProperty("avatar_url")]
        // 'Content' value --> Always empty because we are using embed
        public string avatarurl { get; set; }
        [JsonProperty("content")]
        public string content { get; set; }
        // 'Embed' array value
        [JsonProperty("embeds")]
        public List<Embed> embeds { get; set; }
    }
    
    /*
       Json class to compile the single embed
       Action: n/a
    */
    public class Embed
    {
        [JsonProperty("author")]
        public Author author { get; set; }
        [JsonProperty("title")]
        public string title { get; set; }
        [JsonProperty("url")]
        public string url { get; set; }
        [JsonProperty("description")]
        public string description { get; set; }
        [JsonProperty("color")]
        public int color { get; set; }
        [JsonProperty("fields")]
        public List<Field> fields { get; set; }
        [JsonProperty("thumbnail")]
        public Thumbnail thumbnail { get; set; }
        [JsonProperty("image")]
        public Image image { get; set; }
        [JsonProperty("footer")]
        public Footer footer { get; set; }
    
    }
    
    /*
       Json class to compile the author in an embed
       Action: n/a
    */
    public class Author
    {
        [JsonProperty("name")]
        public string name { get; set; }
        [JsonProperty("url")]
        public string url { get; set; }
        [JsonProperty("icon_url")]
        public string iconurl { get; set; }
    }
    
    
    /*
       Json class to compile the fields in an embed
       Action: n/a
    */
    
    
    /*
       Json class to compile the thumbnail in an embed
       Action: n/a
    */
    public class Thumbnail
    {
        [JsonProperty("url")]
        public string url { get; set; }
    }
    
    /*
       Json class to compile the image in an embed
       Action: n/a
    */
    public class Image
    {
        [JsonProperty("url")]
        public string url { get; set; }
    }
    
    /*
       Json class to compile the footer in an embed
       Action: n/a
    */
    public class Footer
    {
        [JsonProperty("text")]
        public string text { get; set; }
        [JsonProperty("icon_url")]
        public string iconurl { get; set; }
    }
    
    /*
       Json class to compile the config
       Action: CONFIG
    */
    public class Config
    {
        [JsonProperty("webhook_url")]
        public string webhook { get; set; }
        [JsonProperty("json")]
        public Json json { get; set; }
    }
    public class Field
    {
        [JsonProperty("name")]
        public string name { get; set; }
    
        [JsonProperty("value")]
        public string value { get; set; }
    
        [JsonProperty("inline")]
        public bool inline { get; set; }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-11
      • 2021-12-03
      • 2019-09-29
      • 2019-07-18
      • 2021-12-12
      • 2021-12-17
      • 2021-10-09
      • 1970-01-01
      • 2021-03-09
      相关资源
      最近更新 更多