使用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(角色(。
虽然您确实需要更新和添加默认意图,但我认为您的用例不需要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]
这给了我新的角色列表