Search code examples
pythonherokudiscorddiscord.py

I'm trying to cast discord.py role on the line


When using the rolver command, I want the specified person to be given the specified role (to use the command, the author must have the role of "İMPARATOR") but for some reason I put it does not work. My Code:

@bot.command(pass_context=True)
@commands.has_role("İMPARATOR")
async def rolver(ctx, user: discord.Member, role: discord.Role):
    try:
        await user.add_roles(role)
        await ctx.send(f"{ctx.author.name} tarafından, {user.name} kişisine ***{role.name}*** rolü verildi.")
    except:
        await ctx.channel.send("HATA")

@bot.command(pass_context=True)
@commands.has_role("İMPARATOR")
async def rolsil(ctx, user: discord.Member, role: discord.Role):
    try:
        await user.remove_roles(role)
        await ctx.send(f"{ctx.author.name} tarafından, {user.name} kişisinden ***{role.name}*** rolü silindi.")
    except:
        await ctx.channel.send("HATA") 

I did not use try except and I even got:

2021-01-22T14:25:18.721102+00:00 app[worker.1]: 

2021-01-22T14:25:18.721103+00:00 app[worker.1]: The above exception was the direct cause of the following exception:

2021-01-22T14:25:18.721104+00:00 app[worker.1]: 

2021-01-22T14:25:18.721104+00:00 app[worker.1]: Traceback (most recent call last):

2021-01-22T14:25:18.721105+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 902, in invoke

2021-01-22T14:25:18.721105+00:00 app[worker.1]:     await ctx.command.invoke(ctx)

2021-01-22T14:25:18.721105+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 864, in invoke

2021-01-22T14:25:18.721106+00:00 app[worker.1]:     await injected(*ctx.args, **ctx.kwargs)

2021-01-22T14:25:18.721106+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped

2021-01-22T14:25:18.721106+00:00 app[worker.1]:     raise CommandInvokeError(exc) from exc

2021-01-22T14:25:18.721115+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

Solution

  • That is a pretty normal error, it happens because your bot is not able to assign a role which has a higher hierarchy than the bot's role. To solve it make sure that the bot's role is higher in the hierarchy than the role that you are trying to assign, otherwise it will always raise a 403 error when that happens.

    There is no way to solve this issue, as this is the intended behaviour, but using the try except you can catch this error, and you can also use an error handler to do so.