N
N
not important .2020-10-30 23:49:07
Python
not important ., 2020-10-30 23:49:07

AttributeError("'NoneType' object has no attribute 'roles'") - what is the error?

I found a suitable question, but I didn’t find the answer, the bot of one youtuber, the essence of the bot is that it should issue roles by clicking on the reactions in the discord, but when the reaction is pressed in the console, an error is displayed - AttributeError("'NoneType' object has no attribute 'roles'"), but if a person enters the voice channel and clicks on the reaction again, the bot will work as it should, help with the bot please...
the code itself:

import discord
from discord import utils

import config

class MyClient(discord.Client):
  async def on_ready(self):
    print('Logged on as {0}!'.format(self.user))

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

  async def on_raw_reaction_remove(self, payload):
    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]) # объект выбранной роли (если есть)

      await member.remove_roles(role)
      print('[SUCCESS] Role {1.name} has been remove for user {0.display_name}'.format(member, role))

    except KeyError as e:
      print('[ERROR] KeyError, no role found for ' + emoji)
    except Exception as e:
      print(repr(e))

# RUN
client = MyClient()
client.run(config.TOKEN)

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
Dmitry, 2020-10-31
@26DiDi12

Heh. About 2 hours after I got to work, I solved the problem. First, make sure you have version 1.5 of the library installed.
If the version does not match, then in cmd.exe run the command:
pip install -U discord.py==1.5
Then modify your code as follows:

#до того, как запустили бота
intents = discord.Intents.all()
#запуск бота
client = discord.Client(intents=intents)

Also, you may need to go to https://discord.com/developers/applications/ , select your application there, go to the Bot tab and check the box PRESENCE INTENT.
I'll add a picture just in case:
5f9c96378aa68237976303.png
After these steps, everything should work.

N
not important ., 2020-11-01
@26DiDi12

5f9ddfde522f0121367209.jpeg- here, too, you need to check the box for everything to work!

T
Tornadoff, 2020-11-04
@Tornadoff

I’ll also add a little to the answer above, if earlier (before updating the library) the line
if payload.message_id == config.POST_IDs
could read several message id (that is, in the config, write several id in the POST_IDs category and they were all read), but now for some reason this does not work as alternatives can be separated by POST_ID and written for example

if payload.message_id == config.POST_ID1 or payload.message_id == config.POST_ID2

(and enter one message id in each category in the config)

R
reSpiTe, 2021-01-16
@reSpiTe

None of the solutions above helped :(

O
Oleg Baturin, 2021-09-03
@batyrin

https://ru.stackoverflow.com/questions/1259481/%D0... everything is fixed here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question