Наличие бота Discord.py (перезаписи) обнаруживает изменения ролей в событии on_member_update - PullRequest
0 голосов
/ 21 июня 2020

Я попытался сделать так, чтобы бот поздравлял всех, кто повысился от Участника до Уважаемого (он не продвигает его автоматически; администраторы / владелец делают). Однако для следующего кода каждый раз, когда я продвигаю кого-то из Участника в Уважаемый, он не обнаруживает продвижение.

@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 голосов
/ 21 июня 2020

Идентификатор роли всегда является целым числом, в строке 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!""")
0 голосов
/ 21 июня 2020

Вот код, который, я надеюсь, поможет вам в поисках рабочих решений. Этот код отправит сообщение всякий раз, когда кто-то получит уважаемую роль

@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

Надеюсь, это помогло!

...