提问者:小点点

当用户移除反应python时移除不和谐角色


到目前为止,我有一条消息,新用户可以在不和谐的某个频道中做出反应,该消息将根据他们选择的反应为他们分配一个角色(这部分有效)。我还希望如果他们删除了对该消息的反应,则该角色将从用户中删除(这是不起作用的)。我收到一条错误消息:第23行,在on_raw_reaction_remove中,角色=discord. utils.get(payload.成员.gul.角色,名称='War Thunder')属性错误:'NoneType'对象没有属性'行业'

@client.event
# this works to assign a role
async def on_raw_reaction_add(payload):
    # channel and message IDs should be integer:
    if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
        if str(payload.emoji) == "<:WarThunder:745425772944162907>":
            role = discord.utils.get(payload.member.guild.roles, name='War Thunder')
            await payload.member.add_roles(role)

# this doesn't work in removing the role
async def on_raw_reaction_remove(self, payload):
    if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
        if str(payload.emoji) != "<:WarThunder:745425772944162907>":
            role = discord.utils.get(payload.member.guild.roles, name='War Thunder')
            await payload.member.remove_roles(role)

共1个答案

匿名用户

正如留档所说,只有当event_typeREACTION_ADD时,有效成员才可用。因此,要获得公会,您必须使用payload。guild_id并且:

  • Use client.fetch_guild():
    async def on_raw_reaction_remove(payload):
        if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
            if str(payload.emoji) == "<:WarThunder:745425772944162907>":
                guild = await client.fetch_guild(payload.guild_id)
                role = discord.utils.get(guild.roles, name='War Thunder')
                await payload.member.remove_roles(role)
    
    async def on_raw_reaction_remove(payload):
        if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
            if str(payload.emoji) == "<:WarThunder:745425772944162907>":
                guild = discord.utils.get(client.guilds, id=payload.guild_id)
                role = discord.utils.get(guild.roles, name='War Thunder')
                await payload.member.remove_roles(role)
    

    PS:不是每次都写discord. utils.get(),你可以在导入中写from discord.utils import get,然后写get(iterable,**attrs)