L
L
Leonius062020-12-23 23:43:55
Python
Leonius06, 2020-12-23 23:43:55

When I try to assign a role, I get an error AttributeError("'NoneType' object has no attribute 'roles'"). I looked at a lot of answers, nothing helped. Can you help?

I took the code from a youtuber, somehow finished it myself, something according to the advice from the Internet, but it still doesn’t work

import discord
import configs

from discord.ext import commands
from config import settings
from discord import utils

bot = commands.Bot(command_prefix = settings['prefix'])

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 == configs.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=configs.ROLES[emoji])

        if(len([i for i in member.roles if i and i.id not in configs.EXCROLES]) <= configs.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=configs.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))

@bot.command()
async def привет(ctx): 
  author = ctx.message.author 
  await ctx.send(f'Ну здарова, {author.mention}!') 
@bot.command()
async def warm(ctx):
  author = ctx.message.author
  await ctx.send(f'тоби варн, {author.mention}!')


client = MyClient() # Привязываем класс
client.run(settings['token'])  # Подключаемся к серверам Discord
bot.run(settings['token'])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wz, 2021-02-07
@rensly

there is no such attribute.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question