AttributeError: 'str' object has no attribute 'get' -- Discord.py

Viewed 891

I am trying to set a custom Emoji as my bot presence. However, I get this error.

AttributeError: 'str' object has no attribute 'get'

How could I fix this?

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'
1 Answers

There are a few things to have in mind within your problem.

First of all is that bot accounts are not allowed to set Custom activities (Activity type 4 in your case), this is a limitation from Discord API itself, nothing to do about this, I suggest you look at the other types of activities.

Second, when trying to build a CustomActivity object, you are passing a string of the emoji as parameter, where the documentation asks for a PartialEmoji object, you should first retrieve the PartialEmoji object and pass that as parameter regarding the documentation, but keep reading the following point.

Third and last, there is a known issue in discord.py when creating CustomActivity objects. The problem arises from a mistake in the documentation, where as I mentioned it tells you to pass a PartialEmoji as paramater, but this would only be right for the latest version of discord.py (if you look in the github issue it was updated yesterday). The problem is that the code does not expect a PartialEmoji, it is actually expecting a dict object with a PartialEmoji in it (hence why it is looking for a get() function). I suggest you to make use of this last way of passing the emoji parameter.

You can check this by looking at the source code of discord.py

Related