Hey everyone! I’m stuck with my Discord bot. I want it to automatically give a role to new members when they join the server. But it’s not working at all. The bot doesn’t show any errors. It just doesn’t do anything when someone joins.
Here’s what my code looks like:
@bot.event
async def on_member_join(new_member):
bot_intents = discord.Intents.default()
bot_intents.members = True
my_bot = commands.Bot(command_prefix='!', intents=bot_intents)
newbie_role = discord.utils.get(new_member.guild.roles, name='Newcomer')
await new_member.add_roles(newbie_role)
Can anyone spot what I’m doing wrong? I thought this would work but it’s not doing anything. Any help would be awesome!
I’ve encountered this issue before. The problem is likely with your bot’s intents setup. You’re creating a new bot instance inside the event handler, which isn’t correct. Instead, set up the intents when you initially create your bot.
Try modifying your code like this:
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_member_join(new_member):
newbie_role = discord.utils.get(new_member.guild.roles, name='Newcomer')
await new_member.add_roles(newbie_role)
This should resolve the issue. Make sure you’ve also enabled the ‘Server Members Intent’ in your Discord Developer Portal for the bot. Let me know if you need any further clarification!
hey dude, i think ur problem might be with the intents. u gotta set them up when u first create the bot, not in the event handler. Also, make sure u enabled ‘Server Members Intent’ in the dev portal. that should fix it for ya!
I’ve had similar issues when working with Discord bots. One thing that’s not been mentioned yet is error handling. Sometimes, the bot might encounter an error but silently fail without logging anything. To troubleshoot, I’d suggest wrapping your role assignment in a try-except block:
@bot.event
async def on_member_join(new_member):
try:
newbie_role = discord.utils.get(new_member.guild.roles, name='Newcomer')
if newbie_role:
await new_member.add_roles(newbie_role)
print(f'Role added to {new_member.name}')
else:
print('Newcomer role not found')
except Exception as e:
print(f'Error adding role: {str(e)}')
This way, you’ll at least see if there’s an error or if the role isn’t found. Also, double-check that your bot has the necessary permissions in the server to assign roles. Sometimes it’s the simple things that trip us up!