如何从用户ID discord.py获取会员对象

PythonSnek

我想从他们的ID中获得用户的昵称。我有以下代码:

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

但是代码给出了一个错误:

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'

错误似乎member2总是如此None这是None不管我用什么ID。
我已经阅读了文档,并且bot.guilds[guild_id]是正确的,但是该get_member()功能似乎导致了错误。
我该如何解决?

努尔克姆

在新版本的discord.py(1.5.x)中,有关的更新Intents意图就像权限,您必须对其进行定义以获取频道,成员(如guild.get_member())和一些事件等。您必须在定义之前对其进行定义client = discord.Bot(prefix='')

import discord

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

如果要获取有关Intent的更多信息,可以查看API References

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章