#How to add an image from a link to a container

1 messages · Page 1 of 1 (latest)

dawn wave
golden quail
# dawn wave you use the https://docs.pycord.dev/en/master/api/ui_kit.html#discord.ui.Contain...
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/kotiklinok/PythonProjects/MaidTouchiBot-pycord/.venv_vps/lib/python3.11/site-packages/discord/bot.py", line 1154, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "/home/kotiklinok/PythonProjects/MaidTouchiBot-pycord/.venv_vps/lib/python3.11/site-packages/discord/commands/core.py", line 435, in invoke
    await injected(ctx)
  File "/home/kotiklinok/PythonProjects/MaidTouchiBot-pycord/.venv_vps/lib/python3.11/site-packages/discord/commands/core.py", line 146, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In components.0.components.2.file.url: This UnfurledMediaItem does not support arbitrary external urls. You must use the `attachment://` reference system.
#

I tried this before asking, but decided to repeat it to send the error.

dawn wave
#

do you understand what the error means?

golden quail
#

relatively

#

I'm not sure where I could put the image URL so that I could use attachment://.

rotund egretBOT
#

Tag not found.

paper kelp
#

.tag attachment

rotund egretBOT
#

Tag not found.

golden quail
paper kelp
rotund egretBOT
paper kelp
#

This one

#

Just the first part

#

Ignore the embed part

golden quail
#

But this is not a file

#

its link

#

It turns out I need something to work with non-local.

paper kelp
paper kelp
# golden quail
import io
import aiohttp
import discord

def whatever() -> None:
    url = "https://example.com/some-image.png"

    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            await resp.raise_for_status()
            data = await resp.read()

    file_bytes = io.BytesIO(data)
    file_bytes.seek(0)

    fle = discord.File(file_bytes, filename="filename.png")
vagrant light
golden quail
vagrant light
#

a gallery can have up to 10 items

golden quail
#
gallery = discord.ui.MediaGallery()
gallery.add_item(image_url)
vagrant light
#

typo but yeah

golden quail
#

I'm a little confused about how this should be added to the container. I've tried all the options with simple addition.

vagrant light
#

if you're still having trouble, could you show your code?

golden quail
vagrant light
golden quail
#

I'll run it with item now and send the error.

#

08-12 02:56:00 - bot.commands.get_pictures - ERROR - func create_containers_with_pictures: 'file_url'

vagrant light
#

that's something you've done wrong

#

do you not have a full traceback

#

i can only guess that's a keyerror on post

#

(tip: your try-except is way too loose)

golden quail
#

I added another try-except and everything worked

golden quail
vagrant light
#

Container has a remove_item function - this can take either the Item itself, or its id or custom_id. If you store data about the buttons, removing them should be trivial.

#

or you should only modify a copy of the container rather than the original passed in

golden quail
vagrant light
#

That's just how referencing works in Python

golden quail
vagrant light
#

I reran your same code but added remove_item calls in the interaction callback, and it worked as expected; you're not seeing any edits because discord thinks there's nothing to edit

golden quail
#

I was able to write this. Thanks for your help.

vagrant light
#

allgood

golden quail
#

I want to use message_command on servers where there is no bot. Is this possible, or am I misunderstanding something?