【问题标题】:How can I send a message if a certain user goes offline?如果某个用户离线,我如何发送消息?
【发布时间】:2021-08-23 04:28:20
【问题描述】:

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

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    首先,检测是否有成员更新,为此您必须启用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")
    

    参考文献

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多