I am having trouble assigning a role to a user when they type in a command
Code:
JavaScript
x
6
1
@client.command()
2
async def BMOrole(ctx, member: discord.Member = None):
3
role = discord.utils.get(ctx.guild.roles, name='BMO')
4
await member.add_roles(role)
5
await ctx.send('Role given!')
6
When the user types “!BMOrole”, I want to give them a role called, “BMO” and also display a message.
The error I’m receiving is:
JavaScript
1
9
1
Traceback (most recent call last):
2
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
3
await ctx.command.invoke(ctx)
4
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
5
await injected(*ctx.args, **ctx.kwargs)
6
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
7
raise CommandInvokeError(exc) from exc
8
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'add_roles'
9
Advertisement
Answer
you forget set a default user:
JavaScript
1
9
1
@client.command()
2
async def BMOrole(ctx, member: discord.Member = None):
3
if member is None:
4
member = ctx.author
5
6
role = discord.utils.get(ctx.guild.roles, name='BMO')
7
await member.add_roles(role)
8
await ctx.send('Role given!')
9