Discord python embed image

discord.py embed with locally saved images

But my problem is that I don’t know how I would set it as an image that is saved locally, so I don’t have to keep making connections to these URL’s. Thanks. (link to docs: http://discordpy.readthedocs.io/en/latest/api.html#discord.Embed.set_image)

2 Answers 2

You need to set the local image from the bot’s root dir as an attachment and you need to mention the file in ctx.send aswell! I tried doing attachment only, but it shows a blank embed. So take mention the file and take the same file into the set_image as an attachment and try running the code. It works 😃

 file = discord.File("output.png") e = discord.Embed() e.set_image(url="attachment://output.png") await ctx.send(file = file, embed=e) 

Let me know if you still face an issue!

I have a folder with images on my computer which my discord bots use to randomly pick and send images.

This is my code simplified and repurposed to just send one random image:

import os @client.event async def on_message(message): if message.content == ".img": # Replace .img with whatever you want the command to be imgList = os.listdir("./All_Images") # Creates a list of filenames from your folder imgString = random.choice(imgList) # Selects a random element from the list path = "./All_Images/" + imgString # Creates a string for the path to the file await client.send_file(message.channel, path) # Sends the image in the channel the command was used 

Also in @client.event you need to remember to replace client with whatever you named your client further up in your code.

Читайте также:  Модели обработки запросов java

And of course also change the folder name to one that is actually in the same folder as your file.

I hope this was the answer you were looking for, good luck!

Источник

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

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 

2 Answers 2

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

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?

@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

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.

@bot.command() async def image(ctx): return await ctx.send(embed=discord.Embed().set_image(url=ctx.author.avatar_url)) 
@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) 

Источник

Embeds

You might have seen some special messages on Discord (often sent by bots/webhooks), that have a colored border, embedded images, text fields and other properties. These elements are referred to as Embeds, and this section will cover how you can create and send one with your bot. This is done with Embed object.

This section will extensively cover the attributes and methods used with embeds. Thus, we recommend skipping to your desired topic via the table of contents.

Embed preview​

Here is an example of how an embed may look. We will go over embed construction in the next part of this article.

Embed Author

Embed Footer • 01/01/2077

The code for this embed is given below.

# At the top of the file. import disnake from disnake.ext import commands  # Inside a command, event listener, etc. embed = disnake.Embed(  title="Embed Title",  description="Embed Description",  url="https://disnake.dev/",  color=disnake.Colour.yellow(),  timestamp=datetime.datetime.now(), ) embed.set_author(  name="Embed Author",  url="https://disnake.dev/",  icon_url="https://disnake.dev/assets/disnake-logo.png", ) embed.set_footer(  text="Embed Footer",  icon_url="https://disnake.dev/assets/disnake-logo.png", ) embed.set_thumbnail(url="https://disnake.dev/assets/disnake-logo.png") embed.set_image(url="https://disnake.dev/assets/disnake-banner-thin.png") embed.add_field(name="Regular Title", value="Regular Value", inline=False) embed.add_field(name="Inline Title", value="Inline Value", inline=True) embed.add_field(name="Inline Title", value="Inline Value", inline=True) embed.add_field(name="Inline Title", value="Inline Value", inline=True)  await ctx.send(embed=embed) 

It is not strictly necessary to use all elements showcased above. You’re free to leave some out as per your requirements.

The colour of the embed (via the colour parameter) accepts a Colour instance, a HEX string or an integer.

To add a blank field to the embed, you can use embed.add_field(name=’\u200b’, value=’\u200b’) .

Creating an embed​

You can use the Embed object for the creation and manipulation of embeds.

embed = disnake.Embed(  title="Embed Title",  description="Embed Description",  colour=0xF0C43F, ) 

Setting the author​

You can set the author of the embed with the set_author attribute. Note that this code will come after you have defined embed via embed = disnake.Embed(. ) .

embed.set_author(  name="Embed Author",  url="https://disnake.dev/",  icon_url="https://disnake.dev/assets/disnake-logo.png", ) 

Embed Author

Since we have set a URL in this case, clicking on «Embed Author» will redirect the user to the disnake.dev website.

You can set the footer of the embed with the set_footer attribute. Note that this code will come after you have defined embed via embed = disnake.Embed(. ) .

embed.set_footer(  text="Embed Footer",  icon_url="https://disnake.dev/assets/disnake-logo.png", ) 

Embed Author

Embed Footer

Setting the thumbnail​

The thumbnail of the embed is shown in it’s top right corner. It can be set using the set_thumbnail attribute.

embed.set_thumbnail(url="https://disnake.dev/assets/disnake-banner-thin.png") 

Embed Author

It is shown in the top-right corner of the embed. You can set this as a URL, but disnake also allows you to use a locally stored file instead, using the file parameter.

Embed Footer • 01/01/2077

Using timestamps​

Timestamps are shown in the footer of the embed, indicating the time at which the embed was sent/initiated. This can be done using the timestamp parameter of disnake.Embed() . Note that you will need to import the datetime package into your script.

# At the top of your script import datetime  # Inside a command, event listener, etc. embed = disnake.Embed(  title="An Embed!",  description="A description!",  colour=0xF0C43F,  timestamp=datetime.datetime.now(), ) 

Embed Author

Note that datetime.datetime.utcnow() can also be used here. You can also specify the timezone via the tzinfo parameter.

Embed Footer • 01/01/2077

Inserting fields​

Embed fields have two parameters — a name(or title) and a value, inside the add_field attribute. It is also possible to use markdown in both parameters.

# Regular Fields embed.add_field(name="Regular Title", value="Regular Value", inline=False)  # Inline Fields embed.add_field(name="Inline Title", value="Inline Value", inline=True) embed.add_field(name="Inline Title", value="Inline Value", inline=True) embed.add_field(name="Inline Title", value="Inline Value", inline=True) 

Embed Author

Embed Footer • 01/01/2077

These attributes also fully support the use of markdown, as well as highlight links. You can also insert fields at a particular position, with a specified index using embed.insert_field_at(index, . ) .

Inserting images​

This can be done using the set_image attribute, which accepts either a URL or a File object.

# Using a URL embed.set_image(url="https://disnake.dev/assets/disnake-banner-thin.png")  # Using a local file embed.set_image(file=disnake.File("path/to/file.png")) 

Embed Author

To use a local asset for images or thumbnail with the set_image attribute, use the file kwarg which accepts a disnake.File() object.

Embed Footer • 01/01/2077

Sending an embed​

Once the embed is created, you need to send it to a channel too. This means you need to call send(embed=embed) on a messageable object, for example a TextChannel object (i.e. message.channel.send ) or a Context object ( ctx.send ). Otherwise, the embed will not be sent.

Dictionaries to embeds​

A dict datatype (and essentially a json file) can be converted into an embed, using the Embed.from_dict() method. We can recreate the embed made at the start of this page, using the same.

embed_dict =   "title": "Embed Title", "description": "Embed Description", "color": 0xFEE75C, "timestamp": datetime.datetime.now().isoformat(), "author":   "name": "Embed Author", "url": "https://disnake.dev/", "icon_url": "https://disnake.dev/assets/disnake-logo.png", >, "thumbnail": "url": "https://disnake.dev/assets/disnake-logo.png">, "fields": [ "name": "Regular Title", "value": "Regular Value", "inline": "false">, "name": "Inline Title", "value": "Inline Value", "inline": "true">, "name": "Inline Title", "value": "Inline Value", "inline": "true">, "name": "Inline Title", "value": "Inline Value", "inline": "true">, ], "image": "url": "https://disnake.dev/assets/disnake-banner-thin.png">, "footer": "text": "Embed Footer", "icon_url": "https://disnake.dev/assets/disnake-logo.png">, >  await channel.send(embed=disnake.Embed.from_dict(embed_dict)) 

This will give the exact same result as the embed shown here. Note that the timestamp passed through a dictionary should be in ISO8601 format (which has been achieved here by using datetime.datetime.now().isoformat() ). You can learn more about the dict format of embeds in the official Discord documentation.

Embed notes​

  • To display fields side-by-side, you need at least two consecutive fields set to inline.
  • The timestamp will automatically adjust the timezone depending on the user’s device.
  • Mentions of any kind in embeds will only render correctly within embed descriptions and field values.
  • Mentions in embeds will not trigger a notification.
  • Embeds allow masked links (e.g. [Guide](https://guide.disnake.dev/ ‘optional hovertext’) ), but only in description and field values.

Embed limits​

There are a few limits to be aware of while planning your embeds due to the API’s limitations. Here is a quick reference you can come back to:

  • Embed titles are limited to 256 characters.
  • Embed descriptions are limited to 4096 characters.
  • There can be up to 25 fields.
  • A field’s name is limited to 256 characters and its value to 1024 characters.
  • The footer text is limited to 2048 characters.
  • The author name is limited to 256 characters.
  • The sum of all characters from all embed structures in a message must not exceed 6000 characters.
  • 10 embeds can be sent per message.

Resulting code​

The code showcased in this section can be found on our GitHub repository here.

Источник

Оцените статью