@bot.command(pass_context=True)
@commands.has_permissions(ban_members = True)
async def addrole(ctx, member : discord.Member, *,role="Members"):
role = discord.utils.get(ctx.guild.roles, name=role)
await member.add_roles(member, role)
await ctx.reply(f'I have added the {role} to {member}')
Last week, i was creating a bot and when i added this command, it gave me an error.
Ignoring exception in command addrole:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 130, in addrole
await member.add_roles(member, role)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 777, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 250, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10011): Unknown Role
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10011): Unknown Role
Can someone tell me why is this error happening?
CodePudding user response:
@bot.command(pass_context=True)
@commands.has_permissions(ban_members = True)
async def addrole(ctx, member : discord.Member, *,role="Members"):
role = discord.utils.get(ctx.guild.roles, name=role)
await member.add_roles(role)
await ctx.reply(f'I have added the {role} to {member}')
guys the error was the member coming before the role, the working piece of code is here
CodePudding user response:
The error that you're experiencing is because the bot cannot find the role. See the MDN documentation for more information on a status 404 error. Common causes of this issue with discord.py are that the bot isn't in the guild with the role, capitalization is wrong, or an assortment of other issues. It is easier to use role IDs to find roles rather than role names for this reason, however, this does not prevent the guild issue. Consult the documentation for more information on Member.add_roles
.