'NoneType' 对象没有属性 'remove_roles' Discord.py

时间:2021-02-03 11:08:15

标签: python discord.py

reaction_remove 不断出现错误,只是复制粘贴了我的整个代码(减去客户端 ID),因为它可能会有所帮助,我不知道出了什么问题,因为前半部分工作得很好。 Error in console

另外,尝试添加 “如果成员不是无: 等待 member.remove_roles(role) 别的: 打印(错误)” 最后,每次我尝试删除角色时都会出错,以防万一。

import discord

client = discord.Client()

@client.event 
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event 
async def on_raw_reaction_add(payload):
    message_id = payload.message_id
    
    if message_id == 806406876593127464:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
        if payload.emoji.name == "049":
            role = discord.utils.get(guild.roles, name = 'SCP-049')
        elif payload.emoji.name == "0492":
            role = discord.utils.get(guild.roles, name = 'SCP-049-2')
        elif payload.emoji.name == "096":
            role = discord.utils.get(guild.roles, name = 'SCP-096')
        elif payload.emoji.name == "106":
            role = discord.utils.get(guild.roles, name = 'SCP-106')
        elif payload.emoji.name == "173":
            role = discord.utils.get(guild.roles, name = 'SCP-173')
        elif payload.emoji.name == "682":
            role = discord.utils.get(guild.roles, name = 'SCP-682')
        elif payload.emoji.name == "939":
            role = discord.utils.get(guild.roles, name = 'SCP-939')
        elif payload.emoji.name == "999":
            role = discord.utils.get(guild.roles, name = 'SCP-999')
        elif payload.emoji.name == "Chaos":
            role = discord.utils.get(guild.roles, name = 'Chaos Insurgency')
        elif payload.emoji.name == "DClass":
            role = discord.utils.get(guild.roles, name = 'D-Class')
        elif payload.emoji.name == "MTF":
            role = discord.utils.get(guild.roles, name = 'MTF')
        elif payload.emoji.name == "Scientist":
            role = discord.utils.get(guild.roles, name = 'Scientist')

        member = payload.member
        await member.add_roles(role)

@client.event 
async def on_raw_reaction_remove(payload):
    message_id = payload.message_id
    
    if message_id == 806406876593127464:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
        if payload.emoji.name == "049":
            role = discord.utils.get(guild.roles, name = 'SCP-049')
        elif payload.emoji.name == "0492":
            role = discord.utils.get(guild.roles, name = 'SCP-049-2')
        elif payload.emoji.name == "096":
            role = discord.utils.get(guild.roles, name = 'SCP-096')
        elif payload.emoji.name == "106":
            role = discord.utils.get(guild.roles, name = 'SCP-106')
        elif payload.emoji.name == "173":
            role = discord.utils.get(guild.roles, name = 'SCP-173')
        elif payload.emoji.name == "682":
            role = discord.utils.get(guild.roles, name = 'SCP-682')
        elif payload.emoji.name == "939":
            role = discord.utils.get(guild.roles, name = 'SCP-939')
        elif payload.emoji.name == "999":
            role = discord.utils.get(guild.roles, name = 'SCP-999')
        elif payload.emoji.name == "Chaos":
            role = discord.utils.get(guild.roles, name = 'Chaos Insurgency')
        elif payload.emoji.name == "DClass":
            role = discord.utils.get(guild.roles, name = 'D-Class')
        elif payload.emoji.name == "MTF":
            role = discord.utils.get(guild.roles, name = 'MTF')
        elif payload.emoji.name == "Scientist":
            role = discord.utils.get(guild.roles, name = 'Scientist')

        member = payload.member
        await member.remove_roles(role)
client.run()

添加反应后可以给我一个角色,但删除该反应后无法删除角色here

3 个答案:

答案 0 :(得分:1)

如果您尝试打印 payload.member,输出将是 None - 这是因为您没有启用 intents.members。启用它们:

intents = discord.Intents.default()
intents.members = True 

client = discord.Client(intents=intents)

还要确保在 developer portal

中启用意图

How to enable privileged intents

答案 1 :(得分:1)

以下答案是使此代码起作用的解决方案的一部分,因此请实施提供的答案。我在另一个问题中发现,文档中的 payload.member 仅在 event_typeREACTION_ADD 时才可用,因此我通过添加以下内容来修复它:

intents = discord.Intents.default()
intents.members = True 

client = discord.Client(intents=intents)     

添加后,更改

member = payload.member 

member = guild.get_member(payload.user_id)

这对我有用,如果有帮助,请告诉我。

答案 2 :(得分:0)

当事件为 None 时,

payload.member 将始终为 on_raw_reaction_remove。因此,您必须使用 guild.get_member() 和有效负载的 user_id 从缓存中获取成员,这还需要启用 members 意图