如何将图像和文本嵌入到一条消息中?

时间:2018-06-19 14:25:58

标签: c# bots discord

我对我的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());
    }

谢谢

1 个答案:

答案 0 :(得分:0)

执行此操作的唯一方法是需要将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的嵌入消息({ {1}}),最后我们删除唯一的图片信息(ImageUrl = imgurl)。

相关问题