【问题标题】:How to check when a users status has changed on discord?如何检查用户状态何时在不和谐中发生变化?
【发布时间】:2020-08-25 17:14:41
【问题描述】:

我有一个正在 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 并看到了奇怪的结果,诚然不知道如何在我的代码中实现它,因为我的与那里显示的非常不同。

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

【问题讨论】:

  • 您可以将事件侦听器添加到具有beforeafteron_member_update,您可以比较这些值以跟踪更改。至于你用它做什么取决于你。

标签: python python-3.x discord discord.py discord.py-rewrite


【解决方案1】:

使用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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多