从消息中获取图片 [英] Get a picture from the message

查看:30
本文介绍了从消息中获取图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从消息中获取图片(如果有的话)我使用改写的版本

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

我尝试过:

message.attachments[0]['url']

但是出现错误

    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>

如何获取图片网址?

推荐答案

您可以直接从对象中获取url属性

You can directly get the url property from the object

message.attachments[0].url

从根本上讲,该错误说明该对象未实现 __ getitem __ ,这意味着您无法在其上使用 [i] .

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

这篇关于从消息中获取图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆