在对不同的消息做出反应时,如何让 discord bot 删除以前的角色?

时间:2021-02-16 08:46:46

标签: python discord discord.py

我已经让我的不和谐机器人在反应和不反应方面给予和删除角色。我如何做到这一点,以便当用户对一个表情符号做出反应而之前已经对另一个表情符号做出反应时,前一个表情符号会被删除?这样角色就不会叠加......?

这是我添加角色的代码:

@commands.Cog.listener()
    async def on_raw_reaction_add(self, payload):
        message_id = payload.message_id
        if message_id == 810784018953666580:
            guild_id = payload.guild_id
            guild = discord.utils.find(lambda g: g.id == guild_id, self.client.guilds)

            if payload.emoji.name == 'grey_B19FF9':
                role = guild.get_role(810471074500182036)
            elif payload.emoji.name == 'skyblue_11A7BB':
                role = guild.get_role(810471062449291296)
            else:
                role = discord.utils.get(guild.roles, name=payload.emoji.name)

            if role is not None:
                member = payload.member
                if member is not None:
                    await member.add_roles(role)
                    print("Done.")
                else:
                    print("Member not found.")
            else:
                print("Role not found")

这是为了删除角色:

@commands.Cog.listener()
    async def on_raw_reaction_remove(self, payload):
        message_id = payload.message_id
        if message_id == 810784018953666580:
            guild_id = payload.guild_id
            guild = discord.utils.find(lambda g: g.id == guild_id, self.client.guilds)

            if payload.emoji.name == 'grey_B19FF9':
                role = guild.get_role(810471074500182036)
            elif payload.emoji.name == 'skyblue_11A7BB':
                role = guild.get_role(810471062449291296)
            else:
                role = discord.utils.get(guild.roles, name=payload.emoji.name)

            if role is not None:
                guild = await self.client.fetch_guild(payload.guild_id)
                member = await guild.fetch_member(payload.user_id)
                if member is not None:
                    await member.remove_roles(role)
                    print("Done.")
                else:
                    print("Member not found.")
            else:
                print("Role not found")

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

当这段代码运行时会发生什么?我认为方法 remove_roles 需要一个角色列表,因此您应该在删除之前将该 role 参数转换为一个列表,add_roles

也是如此

您还应该查看 member.roles 以检查成员的角色,根据该列表中的内容,您应该有足够的信息来删除您不想要的角色