2

I want to get the nickname of a user from their Id. I have the following code:

id = 235088799074484224 # User Id
member2 = bot.guilds[guild_id].get_member(id)
print(member2.nick)

But the code gives an error:

Traceback (most recent call last):
  File "C:\Users\Vlad\PycharmProjects\untitled\venv\lib\site-packages\discord\client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/Vlad/PycharmProjects/untitled/static/bot.py", line 61, in on_message
    messages = await clean_up_messages(messages)
  File "C:/Users/Vlad/PycharmProjects/untitled/static/bot.py", line 21, in clean_up_messages
    nick = await replace_nick(i.author.id)
  File "C:/Users/Vlad/PycharmProjects/untitled/static/bot.py", line 15, in replace_nick
    return member2.display_name  # or .nick
AttributeError: 'NoneType' object has no attribute 'display_name'

The error seems to come from the fact that member2 is always None. It is None no matter what id I use.
I have read the documentation and the bot.guilds[guild_id] is correct, but the get_member() function seems to cause the error.
How do I fix this?

PythonSnek
  • 542
  • 4
  • 21
  • Isn't it, `print(member2.nick())` (including the "()" at the end of nick). I could be wrong on this – Evorage Oct 26 '20 at 22:07
  • Check if your member id and guild id are correct. [Discord Api](https://discordpy.readthedocs.io/en/latest/api.html#discord.Guild.get_member) says: Returns the member or `None if not found.` – thoerni Oct 26 '20 at 22:10
  • Nope, nick is a variable in the Member object – PythonSnek Oct 26 '20 at 22:11
  • All the info is correct, the guild_id is the place of the guild in a list(the guild object is fine) and the member id is correct too – PythonSnek Oct 26 '20 at 22:12
  • Does your bot have the needed permissions? I mean there probably is a problem that the API returns `None` at `get_member`, which only should happen if the member cannot be found... – thoerni Oct 26 '20 at 22:16
  • it has Admin perms – PythonSnek Oct 26 '20 at 22:17

1 Answers1

2

In the new version of discord.py(1.5.x), there're some updates about Intents. Intents are like permissions, you have to define it to get channels, members(like guild.get_member()) and some events etc. You have to define it before defining the client = discord.Bot(prefix='').

import discord

intents = discord.Intents().all()
client = discord.Bot(prefix='', intents=intents)

If you want to get more information about Intents, you can look at the API References.

Nurqm
  • 4,715
  • 2
  • 11
  • 35