How to implement automatic role assignment for new members in specific Discord server using Python

I’m working on a Discord bot using Python and I want to automatically assign roles to new users when they join my server. The thing is, I only want this to work for one particular server, not all servers where my bot is present.

I’ve been trying to figure out the right way to do this but I’m having trouble with the logic. I need help creating code that will check if someone joins the specific server I want, and then give them a role automatically.

Here’s what I was attempting to do:

@bot.event
async def on_member_join(user):
    target_server = '123456789012345678'
    welcome_role = bot.get_role(987654321098765432)
    
    if user.guild.id == target_server:
        await user.add_roles(welcome_role)

But this code isn’t working properly. Can someone show me the correct way to set up automatic role assignment for new members in a specific Discord server?

u got it a bit wrong, target_server needs to be an int not a str. so just do target_server = 123456789012345678 without the quotes. also, double check if your bot has the permissions to assign the role and that the role is actually in that guild.

The main issue is that you’re trying to get the role from the bot object instead of the specific guild. When a user joins, you need to fetch the role from that particular guild, not globally. Try this approach:

@bot.event
async def on_member_join(member):
    target_server_id = 123456789012345678
    role_id = 987654321098765432
    
    if member.guild.id == target_server_id:
        role = member.guild.get_role(role_id)
        if role:
            await member.add_roles(role)

Note that I changed user to member since that’s the proper parameter name for the event. Also make sure your bot has the “Manage Roles” permission and that the bot’s highest role is positioned above the role you’re trying to assign in the server’s role hierarchy.

I ran into similar issues when I first started working with Discord bots. The problem with your original code is that you’re mixing string and integer types for the server ID comparison, plus the role fetching method is incorrect. Here’s what worked for me:

@bot.event
async def on_member_join(member):
    TARGET_GUILD_ID = 123456789012345678  # as integer
    ROLE_ID = 987654321098765432
    
    if member.guild.id == TARGET_GUILD_ID:
        guild = member.guild
        role = guild.get_role(ROLE_ID)
        
        if role is not None:
            try:
                await member.add_roles(role, reason="Auto-assign on join")
            except discord.Forbidden:
                print(f"Missing permissions to assign role in {guild.name}")
            except discord.HTTPException as e:
                print(f"Failed to assign role: {e}")

The key differences are using integers for IDs, getting the role from the specific guild object, and adding proper error handling. Make sure your bot role is positioned higher than the role you want to assign in the Discord server settings.