我在对 Discord 机器人进行编程时遇到了另一个问题。当我尝试制作包含标题、描述和图像的嵌入时,问题就出现了。问题在于嵌入了标题和说明,并且图像作为不同的消息发送。
这是代码:
[Command("spawn"), Summary("Spawn a monster")]
public async Task Embed()
{
EmbedBuilder Embed = new EmbedBuilder();
Embed.WithAuthor("Test Title");
Embed.WithDescription("Test Description");
Embed.WithThumbnailUrl($"{Context.Channel.SendFileAsync(@"Core\Data\1.png", "", false, null)}");
Embed.WithColor(0, 255, 0);
await Context.Channel.SendMessageAsync("", false, Embed.Build());
}
谢谢你
执行此操作的唯一方法是将 png 上传到某个位置并复制 png 文件的 URL。
BOT 发送两条不同消息的原因是......
这一行:
Embed.WithThumbnailUrl($"{Context.Channel.SendFileAsync(@"Core\Data\1.png", "", false, null)}");
等于:
Embed.WithThumbnailUrl(null);
Context.Channel.SendFileAsync(@"Core\Data\1.png", "", false, null);
我找到了解决办法
[Command("spawn"), Summary("Spawn a monster")]
public async Task Embed()
{
RestUserMessage picture = await Context.Channel.SendFileAsync(@"Resources\test.png");
string imgurl = picture.Attachments.First().Url;
await Context.Channel.SendMessageAsync("", false, new EmbedBuilder { Title = "Test", Description = "Test", Color = new Color(0,255,0), ImageUrl = imgurl }.Build());
await picture.DeleteAsync();
}
首先我们发送图片并捕获消息 (
RestUserMessage picture = x
),之后我们获取附件的 URL (string imgurl = picture.Attachments.First().Url;
),之后我们发送带有图片 URL 的嵌入消息 (ImageUrl = imgurl
),最后我们删除唯一的彩信(await picture.DeleteAsync();
)。