pythonpython-3.xdiscord.pyhttp-status-code-403

403 error with Discord API but bot has the permission


I'm making a "The Werewolves of Millers Hollow" game on a discord bot. For that I made that there is 9 roles, attributed to each player each meaning a role, so that the players wouldn't know and they could access different channels. But I ran into the problem where Discord, even tough I specified it in the scopes (and the bot is an admin), is specified in the overwrite permission, it gives:

'403 Forbidden (error code: 50013): Missing Permissions'

Full output :

2024-10-25 00:03:37 INFO     discord.client logging in using static token
2024-10-25 00:03:38 INFO     discord.gateway Shard ID None has connected to Gateway (Session ID: 9ffd92e484c54602bf4d2fcd50893267).
Creating roles
Deleting roles on users
[<Role id=1195679159821799444 name='@everyone'>, <Role id=1239274286880133123 name='Machine_control'>, <Role id=1220862610266783806 name='9'>, <Role id=1220862606789836831 name='8'>, <Role id=1220862604390432778 name='7'>, <Role id=1220862584001925193 name='6'>, <Role id=1220862581753909288 name='5'>, <Role id=1220862578280890420 name='4'>, <Role id=1220862575957246072 name='3'>, <Role id=1220862573004460177 name='2'>, <Role id=1220862570022436914 name='1'>, <Role id=1299129640807432195 name='DisDawn'>]
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
403 Forbidden (error code: 50013): Missing Permissions
Attributing roles
[<Role id=1195679159821799444 name='@everyone'>, <Role id=1239274286880133123 name='Machine_control'>, <Role id=1220862610266783806 name='9'>, <Role id=1220862606789836831 name='8'>, <Role id=1220862604390432778 name='7'>, <Role id=1220862584001925193 name='6'>, <Role id=1220862581753909288 name='5'>, <Role id=1220862578280890420 name='4'>, <Role id=1220862575957246072 name='3'>, <Role id=1220862573004460177 name='2'>, <Role id=1220862570022436914 name='1'>, <Role id=1299129640807432195 name='DisDawn'>]
[<Role id=1220862610266783806 name='9'>, <Role id=1220862570022436914 name='1'>, <Role id=1220862581753909288 name='5'>, <Role id=1220862606789836831 name='8'>, <Role id=1220862578280890420 name='4'>, <Role id=1239274286880133123 name='Machine_control'>, <Role id=1220862584001925193 name='6'>, <Role id=1220862575957246072 name='3'>, <Role id=1220862604390432778 name='7'>, <Role id=1220862573004460177 name='2'>]
ERROR:root:
Traceback (most recent call last):
  File "c:\Users\stard\OneDrive\Documents\code\Digi\Discord bot\v2\BOT.py", line 128, in start_game
    await game.assign_roles(ctx)
  File "c:\Users\stard\OneDrive\Documents\code\Digi\Discord bot\v2\Werewolf_game.py", line 104, in assign_roles
    await player.discord.add_roles(r)
  File "C:\Users\stard\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\discord\member.py", line 1051, in add_roles
    await req(guild_id, user_id, role.id, reason=reason)
  File "C:\Users\stard\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\discord\http.py", line 739, in request
    raise Forbidden(response, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

Here is the code causing the error :

async def reset(self, ctx: discord.ext.commands.Context):
        print("Deleting roles on users")
        overwrite = discord.PermissionOverwrite()
        overwrite.view_channel = False

        roles = list(ctx.guild.roles)
        print(roles)
        for element in roles:
            if element.name not in ["1","2","3","4","5","6","7","8","9"]:
                roles.remove(element)

        for role in roles:
            await self.werewolf_channel.set_permissions(role, overwrite=overwrite)
            await self.specials_channel.set_permissions(role, overwrite=overwrite)

        for user in self.player_list:
            for role in roles:
                try:
                    await user.discord.remove_roles(role)
                except Exception as error:
                    print(error)
async def assign_roles(self, ctx: discord.ext.commands.Context):
        print("Attributing roles")
        werewolf_channel = self.werewolf_channel
        specials_channel = self.specials_channel

        roles = list(ctx.guild.roles)
        print(roles)
        
        for element in roles:
            if element.name not in ["1","2","3","4","5","6","7","8","9"]:
                roles.remove(element)
        random.shuffle(roles)
        print(roles)

        for index, player in enumerate(self.player_list):
            r = roles[index]
            if player.role == "werewolf":
                await player.discord.add_roles(r)
                overwrite = discord.PermissionOverwrite()
                overwrite.read_message_history = False
                await werewolf_channel.set_permissions(r, overwrite=overwrite)

            if player.role in ["president", "cupidon", "hunter", "witch", "stealer"]:
                await player.discord.add_roles(r)
                overwrite = discord.PermissionOverwrite()
                overwrite.read_message_history = False
                await specials_channel.set_permissions(r, overwrite=overwrite)

            else:
                await player.discord.add_roles(r)

First, I verified that the scopes were correct, they were (In french, it shows that the bots is admin + has the manage roles ).

An image in french showing that the bot is admin

Then, I read that the bot's role must be higher than the roles it gives, which checks out too (the bot is called DisDawn)...

An image showing that the bot has the highest role possible.

It is also specified in the code:

intents = discord.Intents.default()
intents.message_content = True
intents.reactions = True
intents.members = True
overwrite = discord.PermissionOverwrite()
overwrite.read_messages = False
overwrite.manage_channels = True
overwrite.manage_roles = True
bot = commands.Bot(command_prefix="!", intents=intents, overwrite=overwrite)

Full code is available here : https://github.com/sachomatic/Disbot/tree/master

How to fix this error?


Solution

  • I found out why I got a code 403. I was, due to a faulty algorithm, trying to give users a bot's role (Machine_control):

    async def get_game_roles(self,ctx: discord.ext.commands.Context):
            roles = list(ctx.guild.roles)
            
            for role in roles:
                print(role)
                if role.name not in ["1","2","3","4","5","6","7","8","9"]:
                    roles.remove(role)
                    #print(f"Removed role {role} from roles")
    
            print(f"Keeping roles :")
            for prnt_role in roles:
                print(prnt_role.name,end=", ")
            random.shuffle(roles)
            print("")
            return roles
    

    So it returned [1,2,3,4,5,6,7,8,9,Machine_Control].