简体   繁体   English

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

[英]Remove a discord role when user removed a reaction python

So far I have a message that a new user can react to in a certain channel in discord that will assign them a role based on the reaction they choose (this part is working).到目前为止,我有一条消息,新用户可以在不和谐的某个频道中做出反应,这将根据他们选择的反应为他们分配一个角色(这部分正在工作)。 I also want the role to be removed from the user if they remove their reaction to that message (this is what's not working).如果用户删除了对该消息的反应,我还希望从用户中删除该角色(这是行不通的)。 I get an error message saying: line 23, in on_raw_reaction_remove role = discord.utils.get(payload.member.guild.roles, name='War Thunder') AttributeError: 'NoneType' object has no attribute 'guild'我收到一条错误消息:第 23 行,在 on_raw_reaction_remove role = discord.utils.get(payload.member.guild.roles, name='War Thunder') AttributeError: 'NoneType' object has no attribute 'guild'

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

As the documentation says, payload.member is only available if the event_type is REACTION_ADD .由于文档说, payload.member只能如果event_typeREACTION_ADD So, to get the guild, you must use payload.guild_id and either:因此,要获得公会,您必须使用payload.guild_id和:

  • Use client.fetch_guild() :使用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)
  • Use discord.utils.get() :使用discord.utils.get()
     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: Instead of writing discord.utils.get() everytime, you can write from discord.utils import get in your imports and write get(iterable, **attrs) . PS:不是每次都写discord.utils.get() ,你可以写from discord.utils import get在你的导入中并写get(iterable, **attrs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM