c# - 如何使用嵌入的discord.net webhook C# 发送文件附件

标签 c# .net embed webhooks discord.net

我已经尝试了在网络 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#

Discord.net bot Embed Message

ModifyAsync Not Working

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

我知道上述大多数解决方案都有效,但不适用于我的情况。 我真的很感激有关如何解决这个问题的例子。谢谢!

最佳答案

据我所知,您正在尝试通过 IEnumerable<Embed>SendFileAsync 。问题是,你不能施放 EmbedBuilderIEnumerable<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/

相关文章:

c# - Visual Studio -2017。这些项目要么不受支持,要么需要影响修改的项目行为才能在此版本的 VS 中打开

c# - 多核处理器上的多个线程

c# - 3 层系统中数据库层的正确抽象?

c# - 必须声明标量变量@

c# - Nuget 冲突的项目引用和包引用

javascript - 用缩略图 + 视频链接替换嵌入或 iframe youtube 视频

fonts - 想要嵌入字体但无法从 Font Suitcase 或 PostScript Type 1 文件转换

c# - ? C# 中的(可空)运算符

c# - 存储库应该通过构造函数注入(inject)还是包装在使用中?

angular - 使用 Angular 2 在受信任的 HTML 中运行脚本