繁体   English   中英

如何在 discord.py 中识别用户

[英]How to identify user in discord.py

您好,我正在使用 on_member_update(before, after) 事件参考来查看我的 discord 服务器 go 的管理员和版主何时在线和离线监控管理员活动。 到目前为止,我可以让我的程序告诉我有人上线和下线以及何时上线,但我无法确定哪个用户上线和下线,所以我得到了这样的信息:

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}.

所以我不知道谁上线和下线,所以就我所知,它一遍又一遍是同一个人。 由于 on_member_update() 事件引用中没有成员参数,我如何识别成员/用户。 如果您需要任何澄清,请询问。 谢谢!

这是我的代码:

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

after已经是 object 的成员,如果您想获取名称,只需添加.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 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) – 更新用户的旧信息。

  • after (User) – 更新用户的更新信息。

您可以在此处找到有关on_member_update事件的完整文档,并在此处找到discord.User object 的完整文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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