Image caption Bot Tutorial: Code isn't working?

Question: I have forked over the code from the tutorial on the image caption bot. The tutorial has been helpful and updated as of May 2023 but the code isn’t working?

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.

Bot responds with

could I not be using the command correctly?

my repl link: https://replit.com/@niferdog97/ImageCaptionBot-new#main.py

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.

Try changing it into not mimetypes.guess_type(image_url)[0] in SUPPORTED_MIMETYPES:

Hey there thanks for the response, unfortunately that didn’t work either

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

2 Likes

Hi,

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&

This is causing the bot to not accept the image?

New repl is here: https://replit.com/@whiskeyspurs/ImageCaptionBot-updated

1 Like

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.
1 Like

Hi,

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

1 Like

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.