Random Account
Random Account

Reputation: 15

discord.py the bot doesn't give the role provided upon member join

I tried to set up this bot event where it would give a role to the member that joins. For some reason, it doesn't give the role, but it doesn't give any error output either.

@client.event
def on_member_join(member):
  role = discord.utils.get(member.server.roles, id="868708006504833034")
  await client.add_roles(member, role)

Upvotes: 1

Views: 315

Answers (3)

user15308374
user15308374

Reputation:

ID is an integer:

role = discord.utils.get(member.server.roles, id=868708006504833034)

Try it.

Upvotes: 0

Wasi Master
Wasi Master

Reputation: 1207

@client.event
def on_member_join(member):
  role = discord.utils.get(member.server.roles, id="868708006504833034")
  await client.add_roles(member, role)

There are a few issues with your code

  • in line 2, the function is not async, discord.py events need to be async so replace def with async def
  • in line 3, ids are always ints, so you would make it an int, but you shouldn't even use discord.utils.get since you can just do member.guild.get_role(id). Note: id has to be int
  • in line 4, client.add_roles is outdated, it was replaced by member.add_roles. So you would have to change client.add_roles(member, role) to member.add_roles(role)

So the full updated code would be

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

Upvotes: 2

Aslan
Aslan

Reputation: 33

Use this:

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

I think it can work correctly

Upvotes: 0

Related Questions