Search code examples
pythondiscorddiscord.pyroles

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


So, I'm making a reaction role command and it keeps giving me the same error,I have enabled intents. members, everything I need but still it gives me the same error.I dont know why its giving me this error,maybe its a bug or somethng??. Well I hope I cant get a answer from here it only hope rn >.<.(this is just to fill the "please add more details thing..sefsdfysdhfjawsdygdefshdgf)

The code :

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'

Solution

  • The member attribute isn't available in the on_raw_reaction_remove event.

    What you can do instead is

    1. Figure out whether to give or remove the role both in on_raw_reaction_add, when a user reacts check if they have the role, if not add if they do then remove the role. Then remove their reaction from the message. This would essentially be a toggle reaction role.

    OR

    1. Use the payload.user_id and fetch the member using await fetch_user(user_id).