Discord python on role grant event

因此,我一直试图让机器人在会员获得授权角色时宣布,我知道这段代码根本没有意义,但这就是为什么我问:我怎么能这样做?

@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'))

解决方法:

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

@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))
上一篇:javascript – Discord.js回复消息然后等待回复


下一篇:javascript – 循环雪花阵列