discord.py: Adding role to user throws error
Question:
I need help with a bot event. Whenever I add the code, it gives me this error:
Ignoring exception in on_member_join
Traceback (most recent call last):
File "/home/runner/Tragic-Bot/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 20, in on_member_join
await client.add_roles(member, role)
AttributeError: 'Bot' object has no attribute 'add_roles'
Here is the code:
@client.event
async def on_member_join(member):
guild = client.get_guild(999108955437551666)
channel = guild.get_channel(999108956020555798)
role = discord.utils.get(guild.roles, id=999356633023000586)
await client.add_roles(member, role)
await channel.send(f'Hello, {member.mention} :partying_face:! Welcome to Tragic Tech! Make sure to read the rules so you wont get banned right away!')
If anyone could help me that would be great. This happens on replit.com, and I am new so any help is great. Thanks!
Answers:
The error already tells you: 'Bot' object has no attribute 'add_roles'
-> Bot.add_roles()
doesn’t exist. But what exists is Member.add_roles()
.
So, your code would look like that:
@client.event
async def on_member_join(member):
guild = client.get_guild(999108955437551666)
channel = guild.get_channel(999108956020555798)
role = discord.utils.get(guild.roles, id=999356633023000586)
# add_roles() is a method of `discord.Member`
await member.add_roles(role)
await channel.send('your message')
References:
I need help with a bot event. Whenever I add the code, it gives me this error:
Ignoring exception in on_member_join
Traceback (most recent call last):
File "/home/runner/Tragic-Bot/venv/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 20, in on_member_join
await client.add_roles(member, role)
AttributeError: 'Bot' object has no attribute 'add_roles'
Here is the code:
@client.event
async def on_member_join(member):
guild = client.get_guild(999108955437551666)
channel = guild.get_channel(999108956020555798)
role = discord.utils.get(guild.roles, id=999356633023000586)
await client.add_roles(member, role)
await channel.send(f'Hello, {member.mention} :partying_face:! Welcome to Tragic Tech! Make sure to read the rules so you wont get banned right away!')
If anyone could help me that would be great. This happens on replit.com, and I am new so any help is great. Thanks!
The error already tells you: 'Bot' object has no attribute 'add_roles'
-> Bot.add_roles()
doesn’t exist. But what exists is Member.add_roles()
.
So, your code would look like that:
@client.event
async def on_member_join(member):
guild = client.get_guild(999108955437551666)
channel = guild.get_channel(999108956020555798)
role = discord.utils.get(guild.roles, id=999356633023000586)
# add_roles() is a method of `discord.Member`
await member.add_roles(role)
await channel.send('your message')