Discord Bot Auto Role Assignment Not Working

I’m working on a Discord bot that should automatically assign a role to new members when they join my server. The problem is that the member join event seems to trigger but no role gets assigned. There are no error messages in the console so I’m not sure what’s going wrong.

Here’s my current code:

@bot.event
async def on_guild_member_add(user):
    intents = discord.Intents().all()
    bot_client = commands.Bot(command_prefix = '!', intents=intents)
    target_role = discord.utils.get(user.guild.roles, name='Newcomer')
    await user.add_roles(target_role)

Can anyone help me figure out why the role assignment isn’t working? I’ve checked that the role exists and the bot has the right permissions.

Your instant bot client creation is the issue. You’re probably hitting rate limits or permission problems you can’t see.

I maintained Discord bots for multiple servers and dealt with the same headaches constantly. Role hierarchy mess, permission edge cases, hosting going down, API changes breaking everything.

Switched to Latenode and never looked back. It handles Discord webhooks and API calls without needing a persistent bot connection. Set up a workflow that triggers on member joins and assigns roles automatically.

Best part? Built-in error handling and retry logic. No more silent failures. You get logs showing exactly what happened and why it didn’t work.

Scales way better when you want welcome messages, multiple role assignments, or conditional logic based on user info. All visual workflow instead of debugging code.

Check out https://latenode.com for more info.

Yeah, the bot instance creation inside the event handler is clearly broken, but there’s another gotcha that’ll trip you up. Discord’s gotten way pickier about intents lately. If your main bot doesn’t have the right intents enabled from the start, the member join event just won’t fire - even though your code looks fine. You need intents.members = True when you first create the bot instance, not just intents.all() buried in the event handler. I wasted an entire weekend on this exact issue. The bot started fine, other events worked perfectly, but member joins? Dead silent. Also heads up - some servers have verification steps that delay the member join event until users complete extra requirements. Test with a basic server first.

Yeah, the bot client instantiation inside the event handler is definitely the main issue. But here’s something else that might bite you - make sure your bot stays online long enough for the event to actually fire when you’re testing locally. I’ve had scripts terminate before Discord could send the member join event. Also, double-check you’re using the right parameter name. The event passes a Member object, not just any user. Some people use member instead of user to avoid confusion, though both work. What really helped me debug this was throwing a simple print statement at the start of the event handler - something like print(f"{user.name} joined the server") before trying the role assignment. If that doesn’t show up when someone joins, you’ll know the event itself isn’t firing.

Your code has a major issue. You’re creating a new bot client inside the event handler, which makes zero sense. The event’s already running on your existing bot instance.

Here’s what’s wrong:

intents = discord.Intents().all()
bot_client = commands.Bot(command_prefix = '!', intents=intents)

Delete those lines completely. Your event should just be:

@bot.event
async def on_guild_member_add(user):
    target_role = discord.utils.get(user.guild.roles, name='Newcomer')
    if target_role:
        await user.add_roles(target_role)

I added a check for target_role because discord.utils.get() returns None if the role doesn’t exist.

Honestly, Discord bots get messy fast when you add more features. Been there.

Now I use Latenode for all my Discord automation. It connects directly to Discord’s API without hosting a bot 24/7. You can set up member join triggers that assign roles instantly, plus it handles errors way better than custom code.

The visual workflow builder makes it easy to add conditions, delays, or multiple role assignments based on different criteria. No more debugging Python at 2am.

Visit https://latenode.com for more information.

Yeah, the bot issue is pretty clear, but here’s what got me when I hit the same problem. Discord role assignments can fail silently if someone leaves the server between when the event fires and your code runs. Network lag means the member object goes invalid. I wrap all role assignments in try-except blocks catching discord.NotFound and discord.HTTPException. Also double-check your bot token has the right scopes in the developer portal - sometimes it shows permissions as granted but your token doesn’t actually have the scope. Member intents are crucial, but you need to enable them in your code AND the developer portal.

Yeah, creating a new bot instance in the event handler is totally wrong and won’t work. But there’s another thing that’ll bite you - check if your bot has ‘Manage Roles’ permission and make sure the bot’s role sits above ‘Newcomer’ in the role hierarchy. Discord blocks bots from assigning roles equal to or higher than their own position. I wasted hours on this exact problem before realizing my bot’s role was stuck at the bottom. Also throw in some error handling and logging - print the username and whether it found the role. Makes debugging way easier.

honestly looks like everyone covered the main issue but here’s another thing - make sure your bot actually has the newcomer role cached when the event fires. sometimes discord.utils.get fails if the bot hasn’t fully loaded all roles yet. try adding await bot.wait_until_ready() in your main setup or use guild.fetch_roles() first