c# - 如何使用嵌入discord.net webhook c#发送文件附件
问题描述
我已经尝试了在网络钩子上添加嵌入的所有解决方案,但对我的情况没有任何作用,或者我错过了什么?
我正在使用 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);
}
此代码显示错误
无法从“Discord.Embed”转换为 System.Collections.Generic.IEnumerable<Discord.Embed>
我尝试了这段代码并修复了错误
await client.SendFileAsync(filePath: "file.txt", text: null, embeds: (IEnumerable<Embed>)eb);
我构建并运行了 .exe 文件,控制台出现错误
未处理的异常:System.InvalidCastException:无法将“Discord.EmbedBuilder”类型的对象转换为 System.Collections.Generic.IEnumerable 1[Discord.Embed]。
参考: 通过 Webhook C# 发送 Discord 嵌入
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# - 错误:模型项传入字典不匹配
- windows - 如何在 Windows 上使用 clang 从命令行传递链接器脚本?
- git - 意外删除了客户端存储库 + 目录结构。RSA 密钥不再有效
- android - SLF4J 登录 Android 应用程序的非 Android 模块
- typescript - 在打字稿中导入超集群时出错(TypeError:无法设置未定义的属性“选项”)
- python - 将参数提供给函数的更好方法
- python - 根据另一列的条件创建熊猫列
- mysql - 使用 mysql 数据库时使用 BCrypt 的身份验证不保存 hashed_password
- webrtc - WebRTC + Adapter.js 在 MS Edge 中连接音频呼叫时出现 addremoteCandidate 错误
- powershell - 将数组列表从第一个脚本传递到第二个脚本