Answer the question
In order to leave comments, you need to log in
Discord.py AttributeError(“'NoneType' object has no attribute 'roles'”)?
There is a bot that, when the user clicks on the reaction under the message, gives out the role (if the reaction is removed, it removes the role). Exactly a year ago, it all worked, I launch it now and get:
AttributeError("'NoneType' object has no attribute 'roles'")
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.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))
client = MyClient()
client.run(config.TOKEN)
Answer the question
In order to leave comments, you need to log in
import discord
from discord import utils
import logging
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode= 'w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
import config
intents = discord .Intents.all()
intents.members = True
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) # get the channel object
message = await channel.fetch_message(payload.message_id) # get the message object
member = utils.get(message.guild.members, id=payload.user_id) # get the object user who set reaction
try:
emoji = str(payload.emoji) # emoji who selected user
role = utils.get(message.guild.roles, id=config.ROLES[emoji]) # selected role object (if any)
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))
async def on_raw_reaction_remove(self, payload):
channel = self.get_channel(payload.channel_id) # get channel object
message = await channel.fetch_message(payload.message_id ) # get the message object
member = utils.get(message.guild.members, id=payload.user_id) # get the user object that set the response
try:
emoji = str(payload.emoji) # The emoji the user chose
role = utils.get(message.guild.roles, id=config.ROLES[emoji]) # selected role object (if any)
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))
client = discord.Client(intents=intents)
client = MyClient(intents = discord.Intents.all())
# RUN
client.run(config.TOKEN)
__________________________________________________________________________
TOKEN = '---' # bot token
POST_ID = --- - # post id to read reactions from
# roles list - list of roles that will be issued
ROLES = {
'': 882250830659596319, #
'': 882294605461213194, #
' ':
882250955809243209
, #
'': 882293766080319528, #
} = ()
MAX_ROLES_PER_USER = 2 # max ammount of roles a user
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question