简体   繁体   中英

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

I have a bot I'm building on discord.py, and I want to see how I can detect when a user has changed their status (online, idle, etc...), and also give me a push notification on my desktop when they do.

here's what I have working so far:

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

I've tried looking on other posts on how to check when a variable changed and so far haven't seen any success. I've tried the top answer on this post: How to check if a variable's value has changed and saw odd results, admittedly not knowing how to implement it to my code, as mine is very different than what is showed there.

Is there any simple way this can be done?

Using on_member_update(before,after) , it will run each time a user changes one of the following.

  • status
  • activity
  • nickname
  • roles

since you are online interested in status.

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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