如何检查用户状态在discord上的更改?

2024-06-08 13:43:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个在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)

我试着在其他帖子上查看如何检查变量何时更改,但到目前为止还没有看到任何成功。我在这篇文章中尝试了最重要的答案:How to check if a variable's value has changed并看到了奇怪的结果,无可否认,我不知道如何在我的代码中实现它,因为我的代码与这里显示的非常不同

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


Tags: 代码用户pyforvalue状态status机器人
1条回答
网友
1楼 · 发布于 2024-06-08 13:43:41

使用on_member_update(before,after),它将在用户每次更改以下内容时运行

  • 地位
  • 活动
  • 绰号
  • 角色

因为你对状态感兴趣

enter image description here

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

相关问题 更多 >