EmeraldThunder-
EmeraldThunder-

Reputation: 53

How to get a role to add to a user discord.py

I've been trying to code a discord bot and am struggling to add a role to someone. I've read through a few topics and none of the solutions seem to help me.

Currently I'm getting the role when a reaction is added and later I will add the role to the user. I have a role called member and I am getting an error. Here is my code

@bot.event
async def on_raw_reaction_add(payload):
    channel_id = 888832144607166505
    post_cid = payload.channel_id
    reactor = await bot.fetch_user(payload.member)

    if channel_id == post_cid:
        print('Reaction added.', reactor)
        role = await discord.utils.get(reactor.server.roles, name="Member")
        print(role)

This is my error

Traceback (most recent call last):
  File "C:\Users\Home\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\Home\Documents\MandAStudios\bot.py", line 44, in on_raw_reaction_add
    reactor = await bot.fetch_user(payload.member)
  File "C:\Users\Home\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 1384, in fetch_user
    data = await self.http.get_user(user_id)
  File "C:\Users\Home\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 254, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In user_id: Value "TestAccount" is not snowflake.

Upvotes: 0

Views: 1213

Answers (2)

EmeraldThunder-
EmeraldThunder-

Reputation: 53

The correct way of doing it is using guild.get_member(payload.member.id) and make sure your bot has the relevant permissions Src

@bot.event
async def on_raw_reaction_add(payload):
    channel_id = 888832144607166505
    post_cid = payload.channel_id
    if channel_id == post_cid:
        guild = bot.get_guild(int(payload.guild_id))
        role = discord.utils.get(guild.roles, name="Member")
        print(role)
        member = guild.get_member(payload.member.id)
        await member.add_roles(role, reason="self-role reaction")

Upvotes: 0

Razzer
Razzer

Reputation: 884

You have reactor = await bot.fetch_user(payload.member) in your code, which is wrong. You provided a member object, instead of a member id, this is what the argument needs.

So you need to change it to reactor = await bot.fetch_user(payload.member.id)

Aside from that, you don't assign a role to anyone and your await is wrong at the role definition.

@bot.event
async def on_raw_reaction_add(payload):
    channel_id = 888832144607166505
    post_cid = payload.channel_id

    # Get reactor with id, without an API request

    reactor = payload.guild.get_member(payload.member.id)

    if channel_id == post_cid:
        print('Reaction added.', reactor)

        # get guild by id

        guild = bot.get_guild(int(payload.guild_id))


        # get role
        
        role = discord.utils.get(guild.roles, name="Member")
        print(role)

        # add role to reactor
        
        await reactor.add_roles(role, reason="self-role reaction")
        

Sources

Upvotes: 2

Related Questions