6

I need to get a picture from the message if it is there I use rewritten version

I tried this:

message.attachments[0]['url']

But getting an error

    Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\vlad0\AppData\Roaming\Python\Python36\site-packages\discord\client.py", line 227, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\vlad0\Desktop\bot\bot.py", line 121, in on_message
    print(message.attachments[0]['url'])
TypeError: 'Attachment' object is not subscriptable

If you leave only the index or only attachments, you get this:

<discord.message.Attachment object at 0x00000228B8E3BE80>

How can i get image url?

1
  • Evidently [] is not the correct way to get information from an Attachment object. Perhaps you should check its properties instead.
    – khelwood
    Mar 17, 2019 at 12:27

1 Answer 1

13

You can directly get the url property from the object

message.attachments[0].url

Basically, what the error says is that this object does not implement the __getitem__, which means you can't use [i] on it.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.