Discord.py 赋予用户角色 - 'User' 对象没有属性 'guild'

时间:2021-02-02 19:02:07

标签: python discord.py

所以我正在尝试编写一个机器人,它为用户提供一个角色,以便他们可以看到他们的禁令信息。除了实际赋予用户所说的角色之外,一切都在工作。我在这些论坛、reddit 和文档上发现了相互矛盾的建议 - 但似乎没有一个解决方案有效。

这是我的代码:

role = await message.guild.create_role(name=ban_str)
category = discord.utils.get(message.guild.categories, name="Tickets")
overwrites = {
    message.guild.me: discord.PermissionOverwrite(read_messages=True),
    message.guild.default_role: discord.PermissionOverwrite(read_messages=False),
    role: discord.PermissionOverwrite(read_messages=True, send_messages=False)
}
channel = await message.guild.create_text_channel(banstr, category=category, overwrites=overwrites)
user = client.get_user(int(user_id))
await discord.Member.add_roles(user, role, reason="[Ban Ticket] Added user to ticket " + banstr)

我得到了错误:

AttributeError: 'User' object has no attribute 'guild'

我试图使“用户”成为成员对象,将 user = client.get_user(int(user_id)) 替换为 user = message.guild.get_member(int(user_id)),但随后出现错误:

AttributeError: 'NoneType' object has no attribute '_state'

如果有人能看到我在这里做错了什么,非常感谢任何帮助!

3 个答案:

答案 0 :(得分:0)

使用message.guild.get_member()

User 没有设置公会,因为用户只是一个你id是什么的人?你的头像是什么等等。 Member 是附有公会的东西。

答案 1 :(得分:0)

试试user = await client.fetch_user(int(user_id))

答案 2 :(得分:0)

你可以这样试试:

intents = discord.Intents.all()
intents.members = True
# Firstly you have to enable all intents in you bot application in the developer portal
# Secondly you should read what's there standing as description and maybe configure it for this

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

role = await message.guild.create_role(name=ban_str)
category = discord.utils.get(message.guild.categories, name="Tickets")
overwrites = {
    message.guild.me: discord.PermissionOverwrite(read_messages=True),
    message.guild.default_role: discord.PermissionOverwrite(read_messages=False),
    role: discord.PermissionOverwrite(read_messages=True, send_messages=False)
}
channel = await message.guild.create_text_channel(banstr, category=category, overwrites=overwrites)
user = client.get_user(int(user_id))
await user.add_roles(role, reason="[Ban Ticket] Added user to ticket " + banstr)

否则,如果您仍然收到来自 NoneType.get_user(),请尝试:

intents = discord.Intents.all()
intents.members = True
# Firstly you have to enable all intents in you bot application in the developer portal
# Secondly you should read what's there standing as description and maybe configure it for this

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

role = await message.guild.create_role(name=ban_str)
category = discord.utils.get(message.guild.categories, name="Tickets")
overwrites = {
    message.guild.me: discord.PermissionOverwrite(read_messages=True),
    message.guild.default_role: discord.PermissionOverwrite(read_messages=False),
    role: discord.PermissionOverwrite(read_messages=True, send_messages=False)
}
channel = await message.guild.create_text_channel(banstr, category=category, overwrites=overwrites)
user = client.fetch_user(int(user_id))
# ^ This is an API Function which you shouldn't use but in such cases you have to
await user.add_roles(role, reason="[Ban Ticket] Added user to ticket " + banstr)