如果某个用户离线,我如何发送消息?

时间:2021-06-05 10:44:07

标签: discord.py

我的机器人是为一台服务器制作的,我想在某个用户(机器人)离线时向特定频道发送消息,我该怎么做?

1 个答案:

答案 0 :(得分:0)

首先,检测是否有成员更新,为此您必须启用 intents.members,如 this post 中所述。 为了检测成员更新,有事件 on_member_update:

@bot.event #could be client.event for you
async def on_member_update(before, after):

然后,检查更新的人是否是应该寻找的人:

user = await bot.fetch_user(user_id_of_user_to_look_for) #could be client.fetch_user for you
if user == after:

然后检查状态是否更改为离线:

if before.status != discord.Status.offline and after.status == discord.Status.offline:

然后向频道发送消息,其中 channel 是您要将消息发送到的频道的频道对象:

await channel.send("User went offline")

参考文献