0
votes

I am writing a Discord bot that assigns a role to new users when they join my Discord guild. Let's say the role is called "Friend".

The bot is written in Python and uses the Discord.py library.

I am overwriting the on_member_join() function.

@bot.event async def on_member_join(member):

  • assign member to Friend role

I see that there is a add_roles() function from the Member class, but it hasn't worked in this case because the new member doesn't have permissions to manage roles. For security concerns, the default @everyone role shouldn't have the ability to manage roles and be able to assign whatever role it wants to be.

The Discord Bot does have permissions to manage roles.

Is there a way to have the bot automatically assign a new member to the Friend role?

Thank you.

2
Unfortunately not. I get this error: "'Member' object has no attribute 'server'". Furthermore "'Bot' object has no attribute 'add_roles'"Cuttles
That was great! Thanks!Cuttles

2 Answers

1
votes

Solved! Thanks Bagle for helping out.

Here is what worked:

@bot.event
async def on_member_join(member):
        default_role = discord.utils.get(member.guild.roles, id=DEFAULT_ROLE_ID)
        await member.add_roles(default_role)

DEFAULT_ROLE_ID is an integer of the role id I'm trying to assign.

Make sure that your bot has "Manage Roles" permission set AND is above the role you're trying to set in your Guild's role hierarchy.

For example, if your bot has a role of Bot and the role you're trying to auto assign is Friend, make sure Bot is above Friend in the "Roles" tab. If Bot is not above Friend, you'll hit an error.

0
votes

Try this :

@bot.event
async def on_member_join(member):
    role = discord.utils.get(member.server.roles, id="<role ID>")
    await bot.add_roles(member, role)