如何在discord.py中进行角色登录



我想在discord.py中进行角色日志记录,例如:

@bot.event
async def on_member_update(before, after)
await channel.send(after.role)

因此它将发送after角色(新角色(,但after没有返回新角色的after.role那么我该如何获得新的角色名称和id呢?

由于after.roles返回一个列表和before.roles,您可以使用检查发生了什么变化

changed_role_set = set(before.roles) ^ set(after.roles)
if len(changed_role_set) > 0
changed_role = next(iter(changed_role_set))
#and then answer
await channel.send(f"The role {changed_role.name} got either removed or added to the user {after.name}")

参考

  • 检查两个列表之间的差异
  • 构件.孔

最新更新