角色授权上的不和谐python甚至

2024-04-30 02:15:15 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我一直试图让bot在成员获得角色授权时宣布,我知道这段代码可能一点意义都没有,但这就是为什么我要问:我该怎么做?在

@bot.event
async def on_member_update(before, after):
    if str(after.roles) == 'android':
        fmt = "{0.mention} your role request has been accepted! :confetti_ball: You've been granted the role '{1}'"
        await bot.send_message(bot.get_channel('495285593711050754'), fmt.format(member, after.roles.name))
        print(colored('moderation.clf: ', 'blue'), colored('Android granted', 'white'))

    if str(after.roles) == 'human':
        fmt = "{0.mention} your role request has been accepted! :confetti_ball: You've been granted the role '{1}'"
        await bot.send_message(bot.get_channel('495285593711050754'), fmt.format(member, after.roles.name))
        print(colored('moderation.clf: ', 'blue'), colored('Human granted', 'white'))

    if str(after.roles) == 'moderator':
        fmt = "{0.mention} you are now part of the CyberLife staff, we're so excited to have you here! :confetti_ball:"
        await bot.send_message(bot.get_channel('495285593711050754'), fmt.format(member, after.roles.name))
        print(colored('moderation.clf: ', 'blue'), colored('Moderator granted', 'white'))

Tags: theifbotrolememberrolesbeencolored
1条回答
网友
1楼 · 发布于 2024-04-30 02:15:15

首先,您必须检查用户是否获得了新角色。然后你可以检查他们得到的角色是否是你要找的角色之一:

@bot.event
async def on_member_update(before, after):
    if len(before.roles) < len(after.roles):
        new_role = next(role for role in after.roles if role not in before.roles)
        if new_role.name in ('android', 'human'):
            fmt = "{0.mention} your role request has been accepted! :confetti_ball: You've been granted the role '{1}'"
            await bot.send_message(bot.get_channel('495285593711050754'), fmt.format(after, new_role.name))
        elif new_role.name in ('moderator'):
            fmt = "{0.mention} you are now part of the CyberLife staff, we're so excited to have you here! :confetti_ball:"
            await bot.send_message(bot.get_channel('495285593711050754'), fmt.format(after))

相关问题 更多 >