(不和谐机器人蟒蛇)
代码是,如果有人聊天,如果这个人正在玩《守望先锋》,他或她将被提升为玩家角色,如果没有,他或她将被删除或什么也不会发生。但我正在寻找一种无需聊天即可识别正在玩的游戏的方法。有人能帮我吗?
@client.event
async def on_message(message):
man = message.author.activity.name
role = discord.utils.get(message.guild.roles, name="Gamer")
if man == "Overwatch":
await message.author.add_roles(role)
else:
await message.author.remove_roles(role)
你会想要调查一下on_member_update() https://discordpy.readthedocs.io/en/latest/api.html#discord.on_member_update事件。像这样:
@client.event
async def on_member_update(prev, cur):
role = discord.utils.get(cur.guild.roles, name="Gamer")
games = ["overwatch", "rocket league", "minecraft"]
# make sure game titles are lowercase
if cur.activity and cur.activity.name.lower() in games:
await cur.add_roles(role)
# only add the rest if you want them to have the role for the duration of them
# playing a game
elif prev.activity and prev.activity.name.lower() in games and not cur.activity:
if role in cur.roles: # check they already have the role, as to not throw an error
await cur.remove_roles(role)
参考:
- on_member_update() https://discordpy.readthedocs.io/en/latest/api.html#discord.on_member_update
- utils.get() https://discordpy.readthedocs.io/en/latest/api.html#discord.utils.get
- Activity.name https://discordpy.readthedocs.io/en/latest/api.html#discord.Activity.name
- Member.remove_roles() https://discordpy.readthedocs.io/en/latest/api.html#discord.Member.remove_roles
- Member.add_roles() https://discordpy.readthedocs.io/en/latest/api.html#discord.Member.add_roles
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)