not.me
not.me

Reputation: 25

discord bot adding roles to a user in python

I started out to make a bot that gives a role to all the users in a server. I decided to start by giving roles when a user enters a certain message in the server. This is the code I'm using:

if message.content == 'test':
                member = message.author
                role = discord.utils.get(member.guild.roles, name="test")
                await discord.Member.add_roles(member, role)

But I'm getting an error as:

Ignoring exception in on_message
Traceback (most recent call last):
  File "E:\Program File\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "E:\folder for all folders\python\program\bot.py", line 73, in on_message
    await discord.Member.add_roles(member, role)
  File "E:\Program File\Python38-32\lib\site-packages\discord\member.py", line 641, in add_roles
    await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'

I feel lost on what to do here.

Upvotes: 1

Views: 380

Answers (2)

Raffon
Raffon

Reputation: 133

I'm not really good at Python but when you go in the doc it tells you that add_roles has 3 parameters roles,reason and atomic. You don't have to put member. https://discordpy.readthedocs.io/en/latest/api.html#discord.Member.add_roles

Upvotes: 0

Harjot
Harjot

Reputation: 943

here is the correct code -

if message.content == 'test':
                member = message.author
                role = discord.utils.get(message.guild.roles, name="test")
                await member.add_roles(role)

NOTE- Make sue the server has a role named test.

If this worked, please consider accepting this as an answer. Thanks!

Upvotes: 4

Related Questions