You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#开发Discord机器人:如何将Webhook Embeds转换为字典?

提取Discord Webhook Embed值并转为字典的实现思路(C#)

核心着手方向

  1. 确认依赖库
    优先使用Discord官方推荐的Discord.Net库,它已经封装好了Webhook消息和Embed的所有结构,不需要自己手动解析HTTP请求内容。

  2. 获取目标Embed对象
    当机器人接收Webhook请求时,从消息对象(如SocketMessage或WebhookMessage)中取出Embed集合message.Embeds,根据需求选择单个或遍历所有Embed进行处理。

  3. 提取属性并构建字典
    Embed包含基础文本、嵌套对象(Footer/Author)、媒体、字段等几类属性,逐个读取非空属性并添加到字典中即可。注意处理可选属性的空值判断,避免空引用错误。

示例实现代码

using Discord;
using System.Collections.Generic;

public Dictionary<string, object> ConvertEmbedToDict(IEmbed embed)
{
    var embedDict = new Dictionary<string, object>();

    // 基础文本属性
    if (!string.IsNullOrEmpty(embed.Title))
        embedDict.Add("Title", embed.Title);
    if (!string.IsNullOrEmpty(embed.Description))
        embedDict.Add("Description", embed.Description);
    if (!string.IsNullOrEmpty(embed.Url))
        embedDict.Add("Url", embed.Url);
    if (embed.Timestamp.HasValue)
        embedDict.Add("Timestamp", embed.Timestamp.Value.ToString("o"));
    if (embed.Color.HasValue)
        embedDict.Add("Color", embed.Color.Value.RawValue);

    // 处理Footer嵌套对象
    if (embed.Footer != null)
    {
        var footerDict = new Dictionary<string, string>();
        if (!string.IsNullOrEmpty(embed.Footer.Text))
            footerDict.Add("Text", embed.Footer.Text);
        if (!string.IsNullOrEmpty(embed.Footer.IconUrl))
            footerDict.Add("IconUrl", embed.Footer.IconUrl);
        embedDict.Add("Footer", footerDict);
    }

    // 处理Author嵌套对象
    if (embed.Author != null)
    {
        var authorDict = new Dictionary<string, string>();
        if (!string.IsNullOrEmpty(embed.Author.Name))
            authorDict.Add("Name", embed.Author.Name);
        if (!string.IsNullOrEmpty(embed.Author.Url))
            authorDict.Add("Url", embed.Author.Url);
        if (!string.IsNullOrEmpty(embed.Author.IconUrl))
            authorDict.Add("IconUrl", embed.Author.IconUrl);
        embedDict.Add("Author", authorDict);
    }

    // 媒体属性
    if (embed.Image != null)
        embedDict.Add("ImageUrl", embed.Image.Url);
    if (embed.Thumbnail != null)
        embedDict.Add("ThumbnailUrl", embed.Thumbnail.Url);

    // 处理Fields列表
    if (embed.Fields.Count > 0)
    {
        var fieldsList = new List<Dictionary<string, object>>();
        foreach (var field in embed.Fields)
        {
            var fieldDict = new Dictionary<string, object>
            {
                {"Name", field.Name},
                {"Value", field.Value},
                {"IsInline", field.IsInline}
            };
            fieldsList.Add(fieldDict);
        }
        embedDict.Add("Fields", fieldsList);
    }

    return embedDict;
}

额外注意事项

  • 字典的value使用object类型是为了兼容不同属性的类型(字符串、数字、布尔值、子字典等),如果需要序列化存储,可后续将字典转成JSON格式。
  • 可以根据实际需求删减不需要提取的属性,比如不需要颜色值就去掉Color相关代码。

内容的提问来源于stack exchange,提问作者Jnthnc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 06:07:49