首页 > 解决方案 > client.get_user(user_id) 返回“用户”而不是“成员”

问题描述

代码:

person = client.get_user(myid)
role = get(person.guild.roles, name = "rolename")

客户:

intents = discord.Intents.default()
client = discord.Client(intents=intents)

错误:

Traceback (most recent call last):
  File "C:\Python38\lib\site-packages\discord\client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "test.py", line 28, in on_message
    role = get(person.guild.roles, name = "rolename")
AttributeError: 'User' object has no attribute 'guild'

我尝试过使用意图,而不是使用意图,也没有返回成员对象。人们有什么方法client.get_user()可以返回成员而不是用户

标签: pythondiscord.py

解决方案


您可以轻松获取 Guild 版本的用户(discord.Member)。您正在尝试获取用户的角色,如果您使用get_useror fetch_user,它将通过机器人获取用户,因此您将不会拥有来自特定用户的角色。

这是一种获取用户拥有的角色列表的方法discord.utils

guild = message.guild
person = get(guild.users, id=myid)

role = get(guild.roles, name="rolename")

if role in person.roles: # This is what happens if the user has the role.
 ...

else: # This is what happens if the user doesn't have the role.
 ...

希望这对您有帮助祝您有愉快的一天!


推荐阅读