如何使用 embed discord.net webhook c# 发送文件附件

How to send file attachment with embed discord.net webhook c#

我已经尝试了所有关于在 Web 挂钩上添加嵌入的解决方案,但 none 对我的情况有效,还是我遗漏了什么?

我正在使用 Discord.Net v2.2.0

这是我的部分代码

var DCW = new DiscordWebhookClient(DCWebhook)

using (var client = DCW)
{
    var eb = new EmbedBuilder();
        eb.WithDescription("some text")
      .Build();

    await client.SendFileAsync(filePath: "file.txt", text: null, embeds: eb);
}

此代码显示错误

cannot convert from 'Discord.Embed' to System.Collections.Generic.IEnumerable<Discord.Embed>

我试过这段代码并修复了错误

await client.SendFileAsync(filePath: "file.txt", text: null, embeds: (IEnumerable<Embed>)eb);

我构建并 运行 .exe 文件,但控制台出现错误

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'Discord.EmbedBuilder' to type System.Collections.Generic.IEnumerable 1[Discord.Embed].

参考资料:

https://discord.foxbot.me/docs/api/Discord.EmbedBuilder.html

我知道上面的大多数解决方案都有效,但对我来说不行。 我真的很感激关于如何解决这个问题的例子。谢谢!

据我所知,您正试图将 IEnumerable<Embed> 传递给 SendFileAsync。问题是,您不能将 EmbedBuilder 转换为 IEnumerable<Embed>。你需要给它传递一个 IEnumerable<Embed> ,你可以用数组 (Embed[]) 之类的东西得到它。

// This creates the Embed builder
var eb = new EmbedBuilder();
    eb.AddField("RandomField", "Hello, my name is random Field"); 

// Here you make an array with 1 entry, which is the embed ( from EmbedBuilder.Build() )
Embed[] embedArray = new Embed[] { eb.Build() };

// Now you pass it into the method like this: 'embeds: embedArray'
await DCW.SendFileAsync(filePath: "C:\RandomFile.txt", text: "Embed", embeds: embedArray);