繁体   English   中英

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

[英]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)

我曾尝试查看有关如何检查变量何时更改的其他帖子,但到目前为止还没有看到任何成功。 我已经尝试了这篇文章的最佳答案: 如何检查变量的值是否已更改并看到奇怪的结果,不可否认,我不知道如何将其实现到我的代码中,因为我的代码与那里显示的非常不同。

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

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

  • 地位
  • 活动
  • 昵称
  • 角色

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

在此处输入图片说明

@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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM