Giving Roles with Hikari and Lightbulb

Question:

I want to, when a command is ran give the person who ran it a certain role and another command to remove the role. How would I do this?

@bot.command
@lightbulb.command('getpings', 'Will be Pinged (/help For More Info)')
@lightbulb.implements(lightbulb.SlashCommand)
async def give_pings(ctx):
         await bot.rest.add_role_to_member(user=ctx.author, guild=ctx.guild_id, role=roleid)

Error:

E 2022-06-09 21:23:29,648 hikari.event_manager: an exception occurred handling an event (InteractionCreateEvent)
Traceback (most recent call last):
  File "C:Usersavih2AppDataLocalProgramsPythonPython310libsite-packageslightbulbapp.py", line 1154, in invoke_application_command
    await context.invoke()
  File "C:Usersavih2AppDataLocalProgramsPythonPython310libsite-packageslightbulbcontextbase.py", line 292, in invoke
    await self.command.invoke(self)
  File "C:Usersavih2AppDataLocalProgramsPythonPython310libsite-packageslightbulbcommandsbase.py", line 544, in invoke
    await self(context, **kwargs)
  File "C:Usersavih2AppDataLocalProgramsPythonPython310libsite-packageslightbulbcommandsbase.py", line 459, in __call__
    return await self.callback(context, **kwargs)
  File "c:Usersavih2DesktopPyProjectsAPI Testbot.py", line 35, in give_pings
    await bot.rest.add_role_to_member(user=ctx.author, guild=ctx.guild_id, role=roleid)
  File "C:Usersavih2AppDataLocalProgramsPythonPython310libsite-packageshikariimplrest.py", line 2705, in add_role_to_member
    await self._request(route, reason=reason)
  File "C:Usersavih2AppDataLocalProgramsPythonPython310libsite-packageshikariimplrest.py", line 820, in _request
    await self._handle_error_response(response)
  File "C:Usersavih2AppDataLocalProgramsPythonPython310libsite-packageshikariimplrest.py", line 842, in _handle_error_response
    raise await net.generate_error_response(response)
hikari.errors.ForbiddenError: Forbidden 403: (50013) 'Missing Permissions' for https://discord.com/api/v8/guilds/980618833838624821/members/439959575647748096/roles/980948889567911958

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

Traceback (most recent call last):
  File "C:Usersavih2AppDataLocalProgramsPythonPython310libsite-packageslightbulbapp.py", line 1195, in handle_interaction_create_for_application_commands
    await self.invoke_application_command(context)
  File "C:Usersavih2AppDataLocalProgramsPythonPython310libsite-packageslightbulbapp.py", line 1172, in invoke_application_command
    raise new_exc
lightbulb.errors.CommandInvocationError: An error occurred during command 'getpings' invocation
Asked By: Freaky

||

Answers:

First of all, you’re going to have to get the role ID of the role you want to add/remove.

Let’s assume the variable for the role you want to add is called roleid.

You can use lightbulb.BotApp.rest.add_role_to_member to add a role to a member.
assuming you have a variable called bot under which you instantiate lightbulb.BotApp,

await bot.rest.add_role_to_member(user=ctx.author, guild=ctx.guild_id, role=roleid)
Answered By: Rale

Your code is good, but the role you want to add to user needs to be under the bot role. (idk why but that worked for me)

Answered By: Matěj Hopp
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.