question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot use a local image with EmbedBuilder

See original GitHub issue

Cross-posted from question on StackOverflow

According to the Discord.Net API Documentation page for the EmbedBuilder class, the syntax to add a local image to an EmbedBuilder object should look something like this (converted to VB):

Dim fileName = "image.png"
Dim embed = New EmbedBuilder() With {
    .ImageUrl = $"attachment://{fileName}"
}.Build()

I’m trying to use something like this to add a dynamically created image to the EmbedBuilder, but I can’t seem to get it to work properly. Here’s basically what I’ve got:

Dim Builder As New Discord.EmbedBuilder
Dim DynamicImagePath As String = CreateDynamicImage()
Dim AttachURI As String = $"attachment:///" & DynamicImagePath.Replace("\", "/").Replace(" ", "%20")

With Builder
    .Description = "SAMPLE DESCRIPTION"
    .ImageUrl = AttachURI
End With

MyClient.GetGuild(ServerID).GetTextChannel(PostChannelID).SendMessageAsync("THIS IS A TEST", False, Builder.Build)

My CreateDynamicImage method returns the full path to the locally created image (e.g., C:\Folder\Another Folder\image.png). I’ve done a fair amount of “fighting”/testing with this to get past the Url must be a well-formed URI exception I was initially getting because of the [SPACE] in the path.

MyClient is a Discord.WebSocket.SocketClient object set elsewhere.

The SendMessageAsync method does send the embed to Discord on the correct channel, but without the embedded image.

If I instead send the image using the SendFileAsync method (like so):

MyClient.GetGuild(ServerID).GetTextChannel(PostChannelID).SendFileAsync(DynamicImagePath, "THIS IS A TEST", False, Builder.Build)

the image is sent to Discord, but as a part of the message rather than included as a part of the embed (NOTE: this behavior is expected for the SendFileAsync method - I only mention it here because this was a part of my testing to ensure that there wasn’t some sort of problem with actually sending the image to Discord).

I’ve tried using the file:/// scheme instead of the attachment:/// scheme, but that results in the entire post never making it to Discord at all.

Additionally, in other parts of my code, I can successfully set the ImageUrl property to a Web resource (e.g., https://www.somesite.com/someimage.png) and the embed looks exactly as expected with the image and everything when it successfully posts to Discord.


According to the only answer currently posted to the referenced StackOverflow question, it appears this may something to do with the CDN URL used by Discord.Net. - https://cdn.discordapp.com/attachments instead of the “newer”(?) address of https://media.discordapp.net. Obviously, if this is the case, I could try to rework and recompile the code for this bit, but as I have a fairly extensive amount of work yet to do, I believe this task is better suited to the library’s author for investigation.

In the meantime, since using images that are already available with a Web URL seems to work as expected, I believe I have a solution that involves uploading the image to Imgur (using the Imgur.API NuGet library. Once the image is uploaded there, I should be able to use the generated link to populate the EmbedBuilder’s ImageUrl property. I haven’t had a chance to test this thoroughly yet, but based on my experience so far, I would assume that it should work.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
BobVulcommented, Aug 18, 2020

Just a couple slightly more complete examples:

With filePath overload:

var filename = Path.GetFileName(filePath);

var embed = new EmbedBuilder()
    .WithImageUrl($"attachment://{filename}")
    .Build();

await channel.SendFileAsync(filePath: filePath, embed: embed, text: null);

With stream overload:

using var stream = File.OpenRead(filePath);
var filename = Path.GetFileName(filePath);

var embed = new EmbedBuilder()
    .WithImageUrl($"attachment://{filename}")
    .Build();

await channel.SendFileAsync(stream: stream, filename: filename, embed: embed, text: null);

With stream overload and an unrelated filename:

using var stream = File.OpenRead(filePath);
var filename = "image.png";

var embed = new EmbedBuilder()
    .WithImageUrl($"attachment://{filename}")
    .Build();

await channel.SendFileAsync(stream: stream, filename: filename, embed: embed, text: null);
1reaction
Still34commented, Aug 18, 2020

Looks like this has been solved. Closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using a local image with EmbedBuilder - vb.net
You could solve this issue by using https://media.discordapp.net, but it seems like Discord.net is configured to use the old domain.
Read more >
[Vb.Net]-Using a local image with EmbedBuilder
The Embed (and EmbedImage ) objects don't do anything with files. They simply pass the URI as configured straight into Discord.
Read more >
Embeds | discord.js Guide
You can upload images with your embedded message and use them as source for embed fields that support image URLs by constructing a ......
Read more >
Use local images in embed : r/Discord_Bots
Use local images in embed. So I basically want to add the a different thumbnail in the embed dynamicly according to the result...
Read more >
net.dv8tion.jda.core.EmbedBuilder.setImage java code ...
Sets the Image of the embed. Example. Uploading images with Embeds
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found