'403 Forbidden (error code: 50001): Missing Access when adding role | discord.py

I am trying to ass different roles to people rapidly to give users the impression of their name being rainbow ( yes I know its against TOS ), and I am starting by adding roles to people before I remove them. However, when adding roles I get the error in the title of this post. I have looked into this and tried quite a few ways to fix it. The bot has a higher role than the roles being give out. Here is my code and the output:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="$")
role_name = "Rainbow Six Seige"
peopleWithRole = []
guild = discord.Guild 

@bot.event
async def on_ready():
    print("Logged in as")
    print(bot.user.name)
    print("------")
    guild = bot.guilds[0]
    colours = [discord.utils.get(guild.roles, name='red'),
               discord.utils.get(guild.roles, name='green'),
               discord.utils.get(guild.roles, name='blue')
               ]

    role = discord.utils.find(
        lambda r: r.name == role_name, guild.roles)
        
    for user in guild.members:
        if role in user.roles:
            peopleWithRole.append(user)

    for color in colours:
        for user in peopleWithRole:
            await user.add_roles(color)

bot.run("my token")

output:

Logged in as
test bot
------
Ignoring exception in on_ready
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\User\Desktop\test\test.py", line 29, in on_ready
    await user.add_roles(color)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py", line 641, in add_roles
    await req(guild_id, user_id, role.id, reason=reason)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\http.py", line 241, in request
    raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50001): Missing Access


Solution 1:[1]

Make sure you have a role given to the bot which has access to managing roles. Also when you add the bot from the dev portal you can give it permissions needed.

Best practice for verified bots is to make a role called BOT with all the permissions and give it to all the bots you have in the server

Solution 2:[2]

I'm just leaving this for any others struggling with this b/c I spent 3 days trying to debug a 50001 error and found nothing and this thread comes up as one of the first on google.

Apparently, if the Discord server your Bot / integration mandates multi-factor authentication, the developer account associated with your Bot MUST have MFA enabled. I enabled it on my discord developer account, and the 50001 errors immediately resolved.

Solution 3:[3]

Please please please please check twice that in your bot, you really use the correct:

  1. Discord Bot Token,
  2. Discord Guild ID,
  3. Discord Role ID / name,
  4. Discord User ID.

In my case, I was using a token from a different bot on the same server. After changing the token to the correct token, everything worked.

Morale of the story is: always check the dumbest and simplest things first!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 stvn1234
Solution 3