如何解决如何检查用户状态何时发生不一致变化?
我有一个基于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,并看到了奇怪的结果,但我不知道如何将其实现到我的代码中,因为我的代码与此处显示的代码完全不同。
有什么简单的方法可以做到吗?
解决方法
使用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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。