简体   繁体   中英

Sending a File in an Embed Discord.py

I'm trying to make my bot download an image, change it to a url, and send it in an embed in Discord py. However during the process of changing the file to a url, the directory of the file can never be found. Here is the code I am using to change the file to a url:

f = discord.File(f"/Desktop/Bot/Dino_Battles/DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg", filename = f'DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg')

However, I always get an error that the directory cannot be found even when the image is there. Here is the Error:

FileNotFoundError: [Errno 2] No such file or directory: '/Desktop/Bot/Dino_Battles/DinoBattle_628693454754676768_VS_725411116272058369.jpg

And here is the file directory:

C:\Users\lbart\Desktop\Bot\Dino_Battles\DinoBattle_628693454754676768_VS_725411116272058369.jpg

How is discord not finding the file? Sorry if this is confusing.

I'd recommend you upload the file on https://imgur.com/ and then copy the link and use the link instead of a file. It's much easier:>

PS Remember to use the embed.set_image(url=imgurlink) or embed.set_thumbnail, whichever you want

As Daniel Tam said it is best to upload it to a website. imgur api docs . Also this is a good article on how to use it with steps link

Here is an example of how to upload and get the URL using pyimgur

import pyimgur

CLIENT_ID = "Your_applications_client_id"
PATH = "A Filepath to an image on your computer"

im = pyimgur.Imgur(CLIENT_ID)
uploaded_image = im.upload_image(PATH, title= f'DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}')
print(uploaded_image.title)
print(uploaded_image.link)
print(uploaded_image.size)
print(uploaded_image.type)

After you upload it you can use uploaded_image.link as the photo in the embed.

Try including the full path, from

f = discord.File(f"/Desktop/Bot/Dino_Battles/DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg", filename = f'DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg')

to

f = discord.File(f"c:/Users/lbart/Desktop/Bot/Dino_Battles/DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg", filename = f'DinoBattle_{str(ctx.author.id)}_VS_{str(user.id)}.jpg')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM