The bot runs fine but it is saying the file types are Not supported when I am using PNG and or JPEG just fine. I am confused and help would be appreciated.
Note: I am using this tutorial and replit to help learn about bot making. I am a beginner.
I have a feeling it has something to do with this part of the code:
# Fetch image file
response = requests.get(image_url)
# Store image file name
image_filename = ctx.message.attachments[0].filename
# Caption image
final_image = caption_image(BytesIO(response.content), caption_text)
# File must be an image
if mimetypes.guess_type(image_url)[0] not in SUPPORTED_MIMETYPES:
await ctx.message.reply(
"Sorry, the file you attached is not a supported image format. Please"
" upload a PNG, JPEG or WebP image."
)
else:
await ctx.message.reply(
file=discord.File(
BytesIO(final_image), filename=f"captioned-{image_filename}"
)
)
return
Even if the image type is correct, it still gives the ‘not in’ statement. I am unsure what is causing this. If it is user error, that is fine, but so far none of the commands I try via discord work at all.
I don’t think this link works, it lead me to a 404 page
Is your repl private? If so, please change it into public
Also, before I get access to your repl, you can try to add a print(mimetypes.guess_type(image_url)[0]) right before the if statement you think the problem is located at (or change the print() into _ = input() to pause the program)
Always try to debug on your own using loggings (you can use icecream.ic, logging.info, _ = input, print, or anything related to output, base on your favour) before reaching out for help, it saves your time and helps you train your debugging skill
Thanks for the tip. tried the print function it was insightful. Again as mentioned, im still relatively new to this and I am trying to understand it.
From what i can see perhaps the fact that the discord image has the expiration string attached
ie: .jpg?ex=65418a59&is=652f1559&hm=c353e8c823ad58453fd8891033fc6fcf4f2bd6cf7a929d91ba03294478ed2af1&
That is NOT an expiration string; it is actually a signature that Discord recently included. It is not yet enforced, so you can ignore it. However, yes, that is probably the issue. Just split it off from the URL to make it normal.
Mini Analysis for new signature:
ex: A timestamp indicating when the attachment URL will expire. After this point, you would need to retrieve another URL, such as by retrieving a message via HTTP.
is: A timestamp indicating when the URL was issued.
hm: A unique signature that remains valid until ex.
Thank you for your insight and for helping me identify the issue!
Is there some kind of function that could detach the signature from the URL? If the signature is the issue then it comes with every single discord attachment. The bot currently uses the URL created with each attachment.
A point in the right direction would be much appreciated
You can use regex or simply split the signature from the URL completely. As I mentioned, Discord is currently not enforcing this signature, so it’s not necessary to include it.