python - 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()
可以返回成员而不是用户
解决方案
您可以轻松获取 Guild 版本的用户(discord.Member)。您正在尝试获取用户的角色,如果您使用get_user
or 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.
...
希望这对您有帮助祝您有愉快的一天!
推荐阅读
- flutter - 颤振改变底部导航栏高度
- suse - 如何在 SLES 12 上安装 podman
- r - 使用 ggplot2 基于两个因子变量(在 x 轴上)对箱线图进行排序
- nestjs - 如何使用 nginx 在数字海洋中运行nestjs?
- python - pytorch 中是否有 tfagents 的替代品
- windows - 如何以编程方式更改地理位置 Windows10?
- php - 警告:无法修改标头信息 php 中的错误
- composer-php - PHP Composer 打印帮助消息而不是运行命令
- flutter - 如何使用客户类型适配器(Hive/Flutter)?
- angular - 如何在没有 jquery 的情况下在 Angualr 中使用 Gridstack