【问题标题】:AttributeError: 'str' object has no attribute 'get' -- Discord.pyAttributeError:“str”对象没有属性“get”——Discord.py
【发布时间】:2021-05-22 06:20:55
【问题描述】:

我正在尝试将自定义表情符号设置为我的机器人。但是,我收到此错误。

AttributeError: 'str' 对象没有属性 'get'

我该如何解决这个问题?

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
Bot = commands.Bot(command_prefix="&", intents=intents)

@Bot.event
async def on_ready():
    await Bot.change_presence(activity=discord.Activity(type=4, emoji="????", name="hello")) # The error is related to this line
    print("ready!")

Bot.run(TOKEN)
Ignoring exception in on_ready
Traceback (most recent call last):
  File "E:\projects\Virtual Environment\discord-bot\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "E:\projects\discord-bot\base.py", line 114, in on_ready
    await Bot.change_presence(activity=discord.Activity(type=4, emoji="????", name="hello"))
  File "E:\projects\Virtual Environment\discord-bot\lib\site-packages\discord\activity.py", line 193, in __init__
    self.emoji = PartialEmoji.from_dict(emoji)
  File "E:\projects\Virtual Environment\discord-bot\lib\site-packages\discord\partial_emoji.py", line 83, in from_dict
    animated=data.get('animated', False),
AttributeError: 'str' object has no attribute 'get'

【问题讨论】:

    标签: python python-3.x discord discord.py emoji


    【解决方案1】:

    在您的问题中需要注意一些事项。

    首先是不允许机器人帐户设置自定义活动(在您的情况下为活动类型4),这是Discord API本身的限制,没有为此,我建议您查看其他类型的活动。

    其次,在尝试构建CustomActivity 对象时,您传递的是表情符号字符串作为参数,其中documentation 要求PartialEmoji 对象,您应该首先检索PartialEmoji 对象并传递作为有关文档的参数,但请继续阅读以下几点。

    第三个也是最后一个,在创建CustomActivity 对象时,discord.py 中有一个已知的issue。问题源于文档中的一个错误,正如我所提到的,它告诉您将 PartialEmoji 作为参数传递,但这仅适用于最新版本的 discord.py(如果您查看 github 问题,它是昨天更新)。问题是代码不需要PartialEmoji,它实际上需要一个dict 对象,其中包含PartialEmoji(因此它正在寻找get() 函数)。我建议你使用最后一种传递表情符号参数的方式。

    您可以通过查看 discord.py 的 source code 来检查这一点

    【讨论】:

      猜你喜欢
      • 2019-05-23
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多