Discord.py: How do I get the name of the user who triggers on_member_update?

Viewed 214
async def on_member_update(before, after):
    print(str(after.username))
    if str(after.username) == "example username" # I'm looking for something like this
        print(str(af`enter code here`ter.username))
    if str(before.status) == "online":
        if str(after.status) == "offline":
            print("offline")
    elif str(before.status) == "offline":
        if str(after.status) == "online":
            print("online")

So how can I get it to display which user is triggering this event?

1 Answers

before and after are member objects so you can get the member name from there easily.

Here's how your code should be:

async def on_member_update(before, after):

    member = bot.get_guild(before.guild.id).get_member(before.id)
    print(member.name)

    if str(member.name) == "example username" 
        print(member.name)

    if str(before.status) == "online":
        if str(after.status) == "offline":
            print("offline")

    elif str(before.status) == "offline":
        if str(after.status) == "online":
            print("online")
Related