【问题标题】:Python Discord, check member statusPython Discord,检查会员状态
【发布时间】:2018-11-23 02:29:47
【问题描述】:

我想在我的 discord 机器人中实现一个功能,检查是否有任何成员离线,然后执行以下功能。我已经阅读了 API 参考页面,但不太明白该怎么做,这样的东西会起作用吗?

client = discord.Client()

@client.event
async def on_member_update(before, after):
    if before == online:
        if after == offline:
            print("{} has gone offline.".format(member))

我认为代码不会按预期工作,但它可能会为我的目标提供一些指导。

【问题讨论】:

  • 当你使用装饰器时,你必须调用你的函数一次来运行它。因此,您可以添加调度程序以在调用 on_memeber_update 函数时调用它。所以调度器会在下一次运行它。调度程序检查https://schedule.readthedocs.io/en/stable/.
  • 我并不是很想知道on_member_update 命令的触发器,我对后面的代码很感兴趣。

标签: python discord discord.py


【解决方案1】:

这样的?

@client.event
async def on_member_update(before, after):
    if str(before.status) == "online":
        if str(after.status) == "offline":
            print("{} has gone {}.".format(after.name,after.status))

也就是说,如果您希望它仅在用户在线并“离线”时触发
你可以做类似的事情

@client.event
async def on_member_update(before, after):
    if str(after.status) == "offline":
        print("{} has gone {}.".format(after.name,after.status))

如果您希望它在用户“空闲”或“dnd”并“离线”时触发。

【讨论】:

  • @abccd 是的,我猜如果他是“隐形人”,它会被注册为“离线”,并且任何操作(状态、游戏玩法、头像、昵称、角色)也会导致它被打印出来.不错的收获!
  • 为什么要比较字符串值而不是 Status.onlineStatus.offline
  • @PatrickHaugh 当我尝试这样做时,我遇到了“NameError: name 'Status' is not defined”,所以我选择了那个有效的。我可能在这里遗漏了一些东西,我的代码是:if(after.status == Status.online): 然后离线相同,其余相同
  • @Tristo 你做了from discord import Status吗?无论哪种方式都没什么大不了的
  • @PatrickHaugh 呵呵,我现在做到了,它奏效了。谢谢,这很有教育意义!
猜你喜欢
  • 2021-03-14
  • 2021-07-28
  • 2022-07-21
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多