K
K
kirka20222022-03-07 09:23:59
Python
kirka2022, 2022-03-07 09:23:59

What should I do if an error occurs when trying to assign a role through a discord bot?

Hello, I am writing a python bot for discord that will be used to get a role on any server, but when I enter the bot command to issue it in the bot console, an error pops up:

Ignoring exception in command protect:
Traceback (most recent call last):
  File "C:\Users\Kirill-pc\AppData\Local\Programs\Python\Python37\lib\site-packa
ges\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Kirill-pc\Desktop\admin\bot.py", line 23, in protect
    await author.add_roles(role)
  File "C:\Users\Kirill-pc\AppData\Local\Programs\Python\Python37\lib\site-packa
ges\discord\member.py", line 777, in add_roles
    await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Kirill-pc\AppData\Local\Programs\Python\Python37\lib\site-packa
ges\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Kirill-pc\AppData\Local\Programs\Python\Python37\lib\site-packa
ges\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Kirill-pc\AppData\Local\Programs\Python\Python37\lib\site-packa
ges\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Att
ributeError: 'NoneType' object has no attribute 'id'

here is the bot code:
import discord
from discord.ext import commands

TOKEN = "OTUwMjIxNTUyMDkwMjM0OTAw.YiVwyA.TGXuUXTYpH80ujpqqLa0B9UI4Gk"

bot = commands.Bot(command_prefix=('ab.'))
bot.remove_command( 'help' )

@bot.event
async def on_ready():
    print("Я запущен!")

@bot.command()
async def pro(ctx):
    await ctx.send('Недостаточно прав')

@bot.command()
async def protect(ctx):
    author = ctx.message.author
    guild = bot.get_guild(805137789661872129)
    role = guild.get_role(940997266100064316)

    await author.add_roles(role)


bot.run(TOKEN)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-03-07
@Vindicar

What to do? Open your eyes, read the error, understand the error. Think logically.

await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'

"None has no id attribute." Those. passed None instead of some object with id attribute.
We look at the line of code and look for an appeal to the id attribute. This is role.id i.e. None was passed to the method instead of the role parameter.
We look above until we find a link to a line not from discord.py.
await author.add_roles(role)
Those. your role variable is set to None.
We are looking for this line in the code, then we look above where the role came from. See documentation on get_role(
role = guild.get_role(940997266100064316)
) .
Returns the role or None if not found.

Those. role may be None if the passed id does not match a role in this server.
Here is your answer. Recheck the id you pass to get_role().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question