How to set the image of a Discord embedded message with a variable?

15,066

Solution 1

If you want the simplest way of doing things, just sending the image url will work. The only problem is that it will send both the url and the image.

If you want a nicer result, you must do one if the following:

If you are using the rewrite branch, you need to do

imageURL = "image url"
embed = discord.Embed()
embed.set_image(url=imageURL)
await ctx.send(embed = embed)

If you are using the async branch, you need to do

imageURL = "image url"
embed = discord.Embed()
embed.set_image(url=imageURL)
await bot.send_message(ctx.message.channel, embed = embed)

To figure out which branch you have, you need to do print(discord.__version__). If 1.0.0a is printed, then you have the rewrite branch. If 0.16.2 is printed, then you have the async branch

Solution 2

An exception (discord.Forbidden) will be thrown if your bot cannot send embeds to the channel.

Sending an embed to a channel is as simple as:

# rewrite
await ctx.send(embed=embed_object)

# async
await bot.send_message(CHANNEL_ID, embed=embed_object)

Otherwise your when setting the image of an embed, you must pass in a string URL (see async doc and rewrite).

the URL must be a string and as stated in the docs "only HTTPS supported"

it doesn't matter that you're passing a variable because a variable is only a reference to an object, in this case, a string. if I recall correctly discord will not display an invalid image URL. So you may want to double check the URL.

otherwise, code such as this should work.

(assuming rewrite)

@bot.command()
async def image(ctx):
    return await ctx.send(embed=discord.Embed().set_image(url=ctx.author.avatar_url))

(assuming async)

@bot.command(pass_context=True)
async def image(ctx):
    em = discord.Embed().set_image(url=ctx.message.author.avatar_url)
    return await bot.send_message(ctx.message.channel.id, embed=em)
Share:
15,066

Related videos on Youtube

Mark W
Author by

Mark W

Updated on June 04, 2022

Comments

  • Mark W
    Mark W almost 2 years

    So I have my url of the image in a variable. It needs to be in the variable because it changes every time the command is run. Unfortunately, the documentation says that set_image requires a string url, and trying to use a variable throws the 400 error. I then tried doing a simple send_message with the link, but Discord does not download the image into the chat because it is not a string. Does anyone know how to get around this? Thank you!

    embed.set_image(url = exampleVariable) #throws error
    
  • Mark W
    Mark W over 6 years
    I have the async branch. My issue is that imageURL is equal to another variable and not a raw string. My code looks like: response = requests.get("https://dog.ceo/api/breeds/image/random") soupRaw = BeautifulSoup(response.text, 'lxml') soupBackend = str(soupRaw).split("message") soup2 = soupBackend[1] soup3 = soup2[3:] soup = soup3[:-20] embed = discord.Embed(title = "Here is your dog!", description = "Hope you like it", color = 0x5810ea) embed.set_image(url = soup) await bot.send_message(message.channel, embed=embed) Any way I can fix this?
  • Mark W
    Mark W over 6 years
    what if the variable that is being embedded is another variable. requests.get("https://dog.ceo/api/breeds/image/random") soupRaw = BeautifulSoup(response.text, 'lxml') soupBackend = str(soupRaw).split("message") soup2 = soupBackend[1] soup3 = soup2[3:] soup = soup3[:-20] embed = discord.Embed(title = "Here is your dog!", description = "Hope you like it", color = 0x5810ea) embed.set_image(url = soup) await bot.send_message(message.channel, embed=embed) Any way I can fix this? The kink I want is on this page and I need to remove all the stuff around it. @mental
  • Mark W
    Mark W over 6 years
    @qspitzwer Forgot to tag
  • mental
    mental over 6 years
    Well for starters you could stop posting raw code inside comments and use gists, hastebin or pastebin
  • qspitzer
    qspitzer over 6 years
    @MarkW, the problem is that the url includes the \'s. You are going to need to remove those before the link will work. This can be done with for loops and if statements, but there probably is a better way