让Discord.py(重写)机器人在事件on_member_update上检测角色更改

时间:2020-06-21 03:19:17

标签: discord.py discord.py-rewrite

我试图做到这一点,以便该机器人可以祝贺从会员晋升为受人​​尊敬的任何人(它不会自动提升它;管理员/所有者可以做到)。但是,对于以下代码,每次我将某人从“会员”提升为“受人尊敬”时,都不会检测到该提升。

@client.event
async def on_member_update(before, after):
    if [i.id for i in before.roles].count(650019191092674569) == 1:
        if [i.id for i in after.roles].count('658164877172408320') == 1:
            channel = client.get_channel(649833392854007808)
            print(f"""Recognized that {after.name} has been promoted from Member to Respected""")
            await channel.send(f"""Congratulations {after.mention} for getting promoted to Respected!""")

我也尝试了这段代码,尽管该机器人也没有检测到提升。

@client.event
async def on_member_update(before, after):
    if before.roles.count(650019191092674569) == 1:
        if after.roles.count('658164877172408320') == 1:
            channel = client.get_channel(649833392854007808)
            print(f"""Recognized that {after.name} has been promoted from Member to Respected""")
            await channel.send(f"""Congratulations {after.mention} for getting promoted to Respected!""")

请帮助?谢谢!

2 个答案:

答案 0 :(得分:0)

我希望这里有一些代码可以帮助您寻求一些可行的解决方案。 只要有人获得受人尊敬的角色,此代码就会发送一条消息

@client.event
async def on_member_update(before, after):
    if len(before.roles) < len(after.roles):
        # The user has gained a new role, so lets find out which one
        newRole = next(role for role in after.roles if role not in before.roles)

        if newRole.name == "Respected":
            # This uses the name but you could always use newRole.id == Roleid here
            # Now, simply put the code you want to run whenever someone gets the "Respected" role here

希望这对您有所帮助!

答案 1 :(得分:0)

角色ID始终是整数,在第4行中有一个字符串。

您也可以省略== 1,因为您只能拥有0/1个角色。

@client.event
async def on_member_update(before, after):
    if [i.id for i in before.roles].count(650019191092674569):
        if [i.id for i in after.roles].count(658164877172408320):
            channel = client.get_channel(649833392854007808)
            print(f"""Recognized that {after.name} has been promoted from Member to Respected""")
            await channel.send(f"""Congratulations {after.mention} for getting promoted to Respected!""")