Remi_Zacharias
Remi_Zacharias

Reputation: 328

How to identify user in discord.py

Hi I'm using the on_member_update(before, after) event reference to see when the admins and moderators of my discord server go online and offline to monitor admin activity. So far I can get my program to tell me someone goes online and offline and at what time but I can't identify which user is coming online and offline, so I get something like this:

User: has come online at {current_time}.
User: has come online at {current_time}.
User: has come online at {current_time}.
User: has gone offline at {current_time}.
User: has gone offline at {current_time}.
User: has come online at {current_time}.
User: has gone offline at {current_time}.
User: has gone offline at {current_time}.

So I have no idea who is coming online and going offline so for all I know it's the same person over and over again. How can i identify the member/user since there is no member parameter in the on_member_update() event reference. If you need any clarification just ask. Thanks!

Here is my code:

@client.event
async def on_member_update(before, after):
    current_time = datetime.datetime.now()
    if str(after.status) == 'offline':
        print(f'User: has gone offline at {current_time}.'.format(after.name, after.status))
    if str(after.status) == 'online':
        print(f'User: has come online at {current_time}.'.format(after.name, after.status))

Upvotes: 0

Views: 540

Answers (1)

creed
creed

Reputation: 1427

after is already a member object, if you would like to get the name, simply add .name:

if str(after.status) == 'offline':
    print(f'{after.name}: has gone offline at {current_time}.'.format(after.name, after.status))
if str(after.status) == 'online':
    print(f'{after.name}: has come online at {current_time}.'.format(after.name, after.status)) 

# Output: JohnSmith: has gone offline at some_time.
# Output: JohnSmith: has gone online at some_time.

If you would like to get the # number too you can use this:

if str(after.status) == 'offline':
    print(f'{after.name}#{after.discriminator}: has gone offline at {current_time}.'.format(after.name, after.status))
if str(after.status) == 'online':
    print(f'{after.name}#{after.discriminator}: has come online at {current_time}.'.format(after.name, after.status)) 

# Output: JohnSmith#1234: has gone offline at some_time.
# Output: JohnSmith#1234: has gone online at some_time.
  • before (User) – The updated user’s old info.

  • after (User) – The updated user’s updated info.

You can find the full documentation on the on_member_update event here and the full documentation for the discord.User object here.

Upvotes: 1

Related Questions