繁体   English   中英

discord.py Member.activity 返回自定义状态而不是游戏

[英]discord.py Member.activity returns custom status and not game

我正在创建一个机器人来检测用户是否开始玩游戏并为服务器成员提供相应的角色。

@client.event
async def on_member_update(before, after):
  role = discord.utils.get(after.guild.roles, name="roleName")
  games = ["Escape from Tarkov"]

  if after.activity and after.activity.name in games:
    await after.add_roles(role)

      #this takes back the role when the member exits the game
  elif before.activity and before.activity.name in games and not after.activity:
    if role in after.roles: # check they already have the role, as to not throw an error
      await after.remove_roles(role)

但是,当成员具有自定义状态时, after.activity.name返回自定义状态而不是正在玩的游戏,因此可以避免获得角色。 我想不出办法解决这个问题,也找不到与此相关的任何其他帖子。 任何帮助表示赞赏。

member.activities 返回一个包含所有活动的数组,包括当前游戏。

这就是我的个人资料的样子

这就是member.activities返回的内容

(<CustomActivity name='this is my custom status message for a test' emoji=None>, <Activity type=<ActivityType.playing: 0> name='Code' url=None details='Debugging main.py' application_id=782685898163617802 session_id=None emoji=None>)

如果你愿意,你可以在 on_message 事件中这样测试它:(记住使用这个事件会中断命令,所以不要忘记删除它)

@bot.event
async def on_message(message):
    for activity in message.author.activities:
        print(f"    > {activity}")

暂无
暂无

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

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