febitt
febitt

Reputation: 11

How to monitor the status of a specific Member discord.py

I need help with monitoring a specific members online/offline status with discord.py I want the discord bot to be constantly updating and printing the persons status.

def run_bot():

@client.event
async def on_ready():
    print(f"{client.user} is now running")
    await updater()

@client.event
async def updater():
    while True:
        sleep(1)
        for guilds in client.guilds:
            for member in guilds.members:
                if member.id == my_Member_id:
                    if member.status == discord.Status.online:
                        print(f"{member.name} is online")
                    else:
                        print(f"{member.name} is not online")
client.run(TOKEN)

The code I have displays the status of the person indefinitely however when the person changes their status it does not update and instead continues to display the old status

here is a screenshot of the output keep in mind the persons status was changed multiple times while the loop ran

Upvotes: 1

Views: 1749

Answers (1)

1037
1037

Reputation: 54

You can use discord.on_presence_update event listener to monitor changes in status/activity of members.

@client.event
async def on_presence_update(before: discord.Member, after: discord.Member):
    if after.id == my_Member_id:
        print('{} changed status to {}'.format(
            after.display_name,
            after.status
        ))

You can also use it to update locally stored member statuses and activities.

Upvotes: 2

Related Questions