首页 > 解决方案 > Discord.py 没有看到用户/成员角色的更新

问题描述

使用 discord.py 运行 Discord 机器人时,如果编辑了用户的角​​色,则机器人无法看到信息。例如:

1:用户 A(不是角色 R)使用“加入”命令。Bot 将用户添加到 R。

用户 A(现在在角色 R)使用“离开”命令。Bot 认为用户不在 R 中并抱怨。

2:用户 A(角色 R)使用“离开”命令。Bot 从 R 中删除用户

用户 A(不是角色 R)使用“加入”命令。Bot 认为用户已经在 R 中并抱怨。

如果我使用调试器并检查给定的用户,则该角色似乎永远不会被添加/删除。不过,角色变化确实出现在 Discord 本身中。重新启动机器人允许在它中断之前再次更新,所以它让我认为当用户的角色发生变化时某些东西没有更新。

代码也很简单:

async def giveRole(user, wantedRole, message):
    role = getRoleToAdd(wantedRole) # some logic to get the role to leave/join, verified to work

    if role in user.roles:
        await message.channel.send("You've already joined that role")

    else:
        await user.add_roles(role)

“离开”逻辑只是翻转,使用 user.remove_roles(role) 代替。

标签: discord.py

解决方案


虽然您确实必须更新和添加默认意图,但我认为您的用例不需要intents.members

我确实有一个类似的问题,虽然我一直Intents.members启用,但我的问题是我在分配新角色之前和之后重复使用相同的成员对象来检查角色。

roles_before = [role.name for role in updated_roles_member.roles]
await assignRolesToUser(roleList, mentioned_user, message.guild)
updated_roles_member = await mentioned_user.guild.fetch_member(mentioned_user.id)
roles_now = [role.name for role in updated_roles_member.roles]

这给了我新的角色列表


推荐阅读