N
N
Nikita Mokhovikov2021-08-16 23:10:47
Python
Nikita Mokhovikov, 2021-08-16 23:10:47

Why is the role not issued after selecting the message reaction?

There is a code
async def on_raw_reaction_add(self, payload):
    if payload.message_id == config.POST_ID:
        channel = self.get_channel(payload.channel_id)
        message = await channel.fetch_message(payload.message_id)
        member = utils.get(message.guild.members, id=payload.user_id)

        try:
            emoji = str(payload.emoji)
            role = utils.get(message.guild.roles, id=config.ROLES[emoji])
        
            if(len([i for i in member.roles if i.id not in config.EXCROLES]) <= config.MAX_ROLES_PER_USER):
                await member.add_roles(role)
                print('[SUCCESS] User {0.display_name} has been granted with role {1.name}'.format(member, role))
            else:
                await message.remove_reaction(payload.emoji, member)
                print('[ERROR] Too many roles for user {0.display_name}'.format(member))
        
        except KeyError as e:
            print('[ERROR] KeyError, no role found for ' + emoji)
        except Exception as e:
            print(repr(e))

When a user leaves a reaction to a message, they are given a specific role. But when adding a reaction to the console, it is written
mistake
AttributeError("'NoneType' object has no attribute 'roles'")

What could be wrong?

PS on the server itself, the bot is above other roles

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rub1n, 2021-08-17
@mohovoy

Add this line to the code:
intents = discord.Intents.all()
And follow the link https://discord.com/developers/applications/
There, check the boxes "Presence intent" and "Server members intent"
I myself had such a problem, this method helped me.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question