Cannot use a local image with EmbedBuilder
See original GitHub issueCross-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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Just a couple slightly more complete examples:
With
filePath
overload:With
stream
overload:With
stream
overload and an unrelated filename:Looks like this has been solved. Closing.