我已经尝试了在网络 Hook 上添加嵌入的所有解决方案,但没有一个适用于我的情况,还是我遗漏了一些东西?
我正在使用 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].
引用文献: Send a Discord Embed via Webhook C#
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);
关于c# - 如何使用嵌入的discord.net webhook C# 发送文件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62494644/