【问题标题】:How to send file attachment with embed discord.net webhook c#如何使用嵌入discord.net webhook c#发送文件附件
【发布时间】:2020-10-11 03:31:32
【问题描述】:

我已经尝试了在网络挂钩上添加嵌入的所有解决方案,但对我的情况没有任何效果,还是我遗漏了什么?

我正在使用 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

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

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

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

未处理的异常:System.InvalidCastException:无法转换 要键入的“Discord.EmbedBuilder”类型的对象 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

我知道上面的大多数解决方案都有效,但对我来说不是。 我非常感谢有关如何解决此问题的示例。谢谢!

【问题讨论】:

  • 你能把你的代码复制粘贴在这里,而不是代码的图像吗?

标签: c# .net embed webhooks discord.net


【解决方案1】:

所以据我所知,您正试图将IEnumerable&lt;Embed&gt; 传递给SendFileAsync。问题是,您不能将EmbedBuilder 转换为IEnumerable&lt;Embed&gt;。您需要向它传递一个 IEnumerable&lt;Embed&gt;,您可以使用类似数组 (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);

【讨论】:

  • 如果您正在搜索此答案,请将其标记为答案
猜你喜欢
  • 1970-01-01
  • 2019-01-15
  • 2020-04-11
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 2020-07-10
  • 2021-11-20
  • 1970-01-01
相关资源
最近更新 更多