【问题标题】:Migrating message embeds code to the 1.0 version迁移消息嵌入代码到 1.0 版本
【发布时间】:2020-03-25 10:56:02
【问题描述】:

不久前,我制作了一个不和谐机器人,其目的涉及我和我的朋友需要的消息嵌入。长话短说,由于主机(树莓派)死了,机器人离线了一年。今天快进,我们再次需要它,所以我尝试启动它,但注意到我的大部分代码不再工作了,因为 discord.py 的异步分支已更新到 v1.0,这带来了重大变化和需求代码迁移以符合新库。查看文档,除了我的机器人的嵌入部分之外,我能够弄清楚所有内容。哪个最重要。

这是我将重点关注的代码,后面还有更多,但这与这部分无关,因为如果我能成功地将我想要的值存储在字符串中,那么其余的应该可以工作。

async def on_message(message):
    serverid = message.guild.id
    channel = message.channel
    messagecontent = message.content
    if message.embeds:
        try:
            charaname = message.embeds[0]['author']['name']
            charaseries = message.embeds[0]['description']
        except AttributeError:
            return

我基本上想要做的是,如果消息有嵌入,那么我需要将名称和描述值存储在单独的字符串中,以供以后在代码中使用。但我试图这样做:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\discord\client.py", line 270, in _run_event
    await coro(*args, **kwargs)
  File "C:\path_to_script", line 35, in on_message
    charaseries = message.embeds[0]['description']
TypeError: 'Embed' object is not subscriptable

一些研究向我表明,“可下标”意味着一个对象可以包含多个其他对象,例如列表。更好地解释了here。如果它不可下标,那么我猜新库有一种全新的处理方式,我似乎无法弄清楚。所以我需要帮助来了解这里到底发生了什么,这样我才能调整我的代码并让这部分再次工作。

非常感谢您的帮助,谢谢!

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    TypeError: 'Embed' object is not subscriptable 如您所见,Embed 是 object,它是 not subscriptable

    可下标对象的一个​​例子是标准字典。这意味着它的属性可以通过["key_name"] 访问。

    为了使其他对象可以下标,它们需要实现__getitem__() dunder 方法。由于您收到 object not subscriptable 错误,这意味着您的 Embed 对象没有实现此方法。

    您过去可以通过这种方式访问​​它们,但如果您查看d.py migrating page,您会看到它们声明:

    Message.embeds 现在是一个 Embed 列表,而不是 dict 对象。

    所以它是一个 Embed 对象列表,如果您查看 current Embed documentation,您将看到如何访问它的属性 - 对于您的情况:

    charaname = message.embeds[0].author.name
    charaseries = message.embeds[0].description
    

    为了澄清,message.embeds 是一个 Embed 对象列表,因此使用 [0] 我们从该列表中获取第一个元素,即 Embed object

    如您所见from the documentation,我们可以通过description 属性访问它的描述,是不是很简单?

    如果我们做.author,从documentation 可以看出,我们将访问它的作者EmbedProxy object。现在我们可以从中访问什么?如果您查看以前的documentation link,它会声明See set_author() for possible values you can access.

    让我们看看set_author() 的文档,我们可以看到它的参数是

    name (str)
    url (str)
    icon_url (str)
    

    因此,按照我们知道的文档中的先前声明,我们可以访问这 3 个。

    所以这都是有效的:

    message.embeds[0].author.name
    message.embeds[0].author.url 
    message.embeds[0].author.icon_url 
    

    如果其中任何一个未设置,它将返回 Embed.Empty,如从 the docs 看到的那样

    因此,如果未设置它们,您将获得 Embed.Empty ,这种嵌入的一个示例是:

    embed = discord.Embed(title="Test embed #1", description="Some description")
    await ctx.send(embed=embed)
    

    如您所见,作者未设置,因此如果您获取message.embeds[0].author.name,您将获得Embed.Empty,而对于message.embeds[0].description,您将获得Some description,因为它已设置。

    在嵌入中设置作者的一个例子:

    embed = discord.Embed(title="Test embed #2", description="Some description").set_author(name=ctx.author.name)
    await ctx.send(embed=embed)
    

    (我们使用了set_author()) - 这将为我们提供作者姓名字符串,因为它是在嵌入初始化期间设置的。

    【讨论】:

    • 我明白了。这条清理后的消息嵌入得非常好,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多