AttributeError: 'NoneType' object has no attribute 'remove_roles'

AttributeError: 'NoneType' object has no attribute 'remove_roles'

所以,我正在制作一个反应角色命令,它一直给我同样的错误,我已经启用了意图。成员们,我需要的一切,但它仍然给我相同的 error.I 不知道为什么它给我这个错误,也许它是一个错误或什么??。好吧,我希望我不能从这里得到答案,只希望 rn >.<.(这只是为了填写“请添加更多详细信息……sefsdfysdhfjawsdygdefshdgf)

代码:

import discord
from discord.ext import commands
import random
from io import BytesIO
import DiscordUtils
from discord.utils import get 
import os 
import asyncio as asyncio
import aiohttp
import datetime
import json


intents = discord.Intents.all()
intents.members = True
client = discord.Client(intents=intents)



client = commands.Bot(command_prefix="?",case_insensitive = True)

client.remove_command("help")

@client.event
async def on_raw_reaction_add(payload):
    if payload.member.bot:
        pass
    else:
        with open('reactrole.json') as react_file:
            data = json.load(react_file)
            for x in data:
                if x['emoji'] == payload.emoji.name:
                    role = discord.utils.get(client.get_guild(
                        payload.guild_id).roles, id=x['role_id'])
                    await payload.member.add_roles(role)

@client.event
async def on_raw_reaction_remove(payload):
    guild = client.get_guild(payload.guild_id)
    print("Guild checked.")
    member = guild.get_member(payload.user_id)
    print("Member checked.")
    with open('reactrole.json') as react_file:
        data = json.load(react_file)
        for x in data:
            if x['emoji'] == payload.emoji.name:
                role = discord.utils.get(client.get_guild(payload.guild_id).roles, id=x['role_id'])
                print("got the role")
                
                await member.remove_roles(role)

@client.command(aliases = ['rr'])
@commands.has_permissions(administrator=True, manage_roles=True)
async def reactrole(ctx, emoji, role: discord.Role, *, message):
    emb = discord.Embed(title = "REACTION ROLE !",description=message,timestamp = datetime.datetime.utcnow(),color = 0xADD8E6)
    emb.set_thumbnail(url = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQPiUxyDTBu4dkC8f3tBeOzM8b0sEnK_8iLUg&usqp=CAU")
    emb.set_footer(icon_url = ctx.author.avatar_url, text = f"reactrole embed created by : {ctx.author.name}")
    emb.set_image(url = "https://www.logolynx.com/images/logolynx/f8/f843b4dd5ec5dc4e56a3f5639341516a.png")
    msg = await ctx.channel.send(embed=emb)
    await msg.add_reaction(emoji)
    with open('reactrole.json') as json_file:
        data = json.load(json_file)
        new_react_role = {'role_name': role.name, 
        'role_id': role.id,
        'emoji': emoji,
        'message_id': msg.id}
        data.append(new_react_role)
    with open('reactrole.json', 'w') as f:
        json.dump(data, f, indent=4) ```



and heres the error:
Guild checked.
Member checked.
got the role
Ignoring exception in on_raw_reaction_remove
Traceback (most recent call last):
  File "C:\Users\pangh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\pangh\Desktop\Engineer\Engineer.py", line 256, in on_raw_reaction_remove
    await member.remove_roles(role)
AttributeError: 'NoneType' object has no attribute 'remove_roles'

on_raw_reaction_remove事件中的成员属性isn't available

你可以做的是

  1. on_raw_reaction_add 中确定是授予还是删除角色,当用户做出反应时检查他们是否具有该角色,如果没有则添加,如果他们有则删除该角色。然后从消息中删除他们的反应。这本质上是一个切换反应角色。

  1. 使用 payload.user_id 并使用 await fetch_user(user_id) 获取成员。