-2

I'm new to python, so can someone help me with my code, because it is not working properly. When the user that I want joins it doesn't give it a role. I want to make it work for a specific user only to give a role when joining discord server.

   @client.event 
    async def on_member_join(member):
      member = get(member.id, id=member_id)
      role = get(member.guild.roles, id=role_id)
      await member.id(member)
      await member.add_roles(role)

1 Answers1

2

I don't even know why you're making this so complicated.

Since you already have the member as an "argument" you can work with it and don't have to define member again.

We can get the ID of member very easily with member.id. If we want to compare this with a real ID, we do the following:

if member.id = TheIDHere:
    # Do what you want to do

The function for the role is correct, yet I cannot find a use for await member.id(member). What is the point of this/has it any use?

How you add the role at the end is also correct, but the code must be indented properly and best you work with an if / else statement, otherwise the bot will still give an error at the end in the console if always the wrong member joins.

The whole code:

@client.event
async def on_member_join(member):
    role = discord.utils.get(member.guild.roles, id=IDOfTheRole)  # Get the role from member.guild and the id
    if member.id == TheIDHere:  # If the member.id matches
        await member.add_roles(role)  # Add the role for the specific user
    else:  # If it does not match
        return  # Do nothing

You may also need to enable the members Intent, here are some good posts on that:

Dominik
  • 3,612
  • 2
  • 11
  • 44