如何检查用户状态何时因不和谐而改变?

How to check when a users status has changed on discord?

我有一个正在 discord.py 上构建的机器人,我想看看如何检测用户何时更改了他们的状态(在线、空闲等...),并提供当他们这样做时,我会在桌面上收到推送通知。

这是我目前所做的工作:

@client.command()
async def status(ctx, user: discord.Member):

    status = discord.Embed (
        color = discord.Color.blue()
    )
    
    stat = user.status

    toaster = ToastNotifier()
    toaster.show_toast(f"Status for {user}", f"currently: {stat}", duration=10)

    status.add_field(name=f"Status for {user}", value=f"currently: {stat}")

    await ctx.send(embed=status)

我已经尝试查看其他 post 以了解如何检查变量何时更改,但到目前为止还没有看到任何成功。我已经尝试了这个 post 的最佳答案:How to check if a variable's value has changed 并看到了奇怪的结果,诚然不知道如何将它实现到我的代码中,因为我的与那里显示的非常不同。

有什么简单的方法可以做到这一点吗?

使用 on_member_update(before,after),每次用户更改以下一项时,它都会 运行。

  • 状态
  • activity
  • 昵称
  • 角色

因为您在线对状态感兴趣。

@client.event
async def on_member_update(before, after):
    if before.status != after.status:  # to only run on status
        embed = discord.Embed(title=f"Changed status")
        embed.add_field(name='User', value=before.mention)
        embed.add_field(name='Before', value=before.status)
        embed.add_field(name='After', value=after.status)
        # send to admin or channel you choose
        channel = client.get_channel(ID_HERE)  # notification channel
        await channel.send(embed=embed)
        admin = client.get_user(ID_HERE)  # admin to notify
        await admin.send(embed=embed)