角色授予事件中的Discord Python

发布于 2021-01-29 14:59:28

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

@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'))
关注者
0
被浏览
75
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    首先,您必须检查用户是否具有新角色。然后,您可以检查他们所担任的角色是您所寻找的角色之一:

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看