discord.py - 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) 代替。
解决方案
虽然您确实必须更新和添加默认意图,但我认为您的用例不需要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]
这给了我新的角色列表
推荐阅读
- c# - ASP.NET 无法使用 itext7 下载 PDF(但可以保存到磁盘)
- gnu-make - 使用 Guile 生成完整的 GNU Make 配方
- mongodb - 如何在猫鼬中使用 $lt
- ios - 一旦被拒绝,如何通过弹出通知获得许可
- unity3d - 没有关于 void Start() 和更新统一与代码的参考
- ios - 如何快速更改从本地 json 文件加载的数据并在不同页面中同时显示更改的数据
- apache-kafka - 在任何一个 Cassandra 节点从 6 节点 Cassandra 崩溃后,风暴拓扑停止处理
- c - 为什么 main() 在 '.ll' 文件 LLVM 中有 '%retval'
- go - 如何在 golang 中从多个作家那里写作?
- javascript - 无法使用 Node.js Express 设置 cookie