'Discord.py new member role code not working [duplicate]

I want my bot to add a role to members that have just joined; however, my code seems to not react at all. I get 0 responses on both Discord and console.

The bot's role is on the top of the role list.

@client.event
async def on_member_join(member):
  join_guild = await client.fetch_guild(id_here)
  role = join_guild.get_role(id_here)


  await member.add_roles(role)


Solution 1:[1]

You don't need to use fetch_guild() you already have your discord.Member object from the on_member_join event

@client.event
async def on_member_join(member):
    role = member.guild.get_role(...)  # role id
    await member.add_roles(role)

The reason you're not getting any response is because on_member_join requires Intents.members to be enabled, here is an example

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True  # Subscribe to the privileged members intent.
bot = commands.Bot(command_prefix='!', intents=intents)

Learn more about Intents

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jawad