Reputation: 103
First, I would like to point out that I am a beginner with python.
I am trying to write a command that allows the user to change the color of his role via the bot. However, I have encountered many problems that I can not find an answer to.
The first problem was that I could not get access to the role of the user calling the command. I decided, however, to skip it and go straight to a specific role. So i made this code:
@client.command(pass_context=1)
async def changecolor(ctx, NewColor):
author = ctx.message.author
server = ctx.message.author.server
dictOfColors = { '1' : discord.Color.default(),
'2' : discord.Color.teal(),
'3' : discord.Color.dark_teal(),
'4' : discord.Color.green(),
'5' : discord.Color.dark_green(),
'6' : discord.Color.blue(),
'7' : discord.Color.purple(),
'8' : discord.Color.dark_purple(),
'9' : discord.Color.magenta(),
'10' : discord.Color.dark_magenta(),
'11' : discord.Color.gold(),
'12' : discord.Color.dark_gold(),
'13' : discord.Color.orange(),
'14' : discord.Color.dark_orange(),
'15' : discord.Color.red(),
'16' : discord.Color.dark_red() }
role = discord.utils.get(server.roles, name='New Member')
if NewColor in dictOfColors:
await client.edit_role(server, role, colour=NewColor)
But when I try: .changecolor 5
receives this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'name'
Can you give me a hint of what I'm doing wrong?
Upvotes: 0
Views: 3461
Reputation: 61014
You can use a role converter to get the role from a role mention. I would also make it so the user passes the name of the color instead of a number:
@client.command(pass_context=True)
async def changecolor(ctx, role: discord.Role, *, color):
if role not in ctx.message.author.roles:
await bot.say("You do not have the role " + role.name)
return
color = '_'.join(color.lower().split())
if not hasattr(discord.Color, color): # We could also use inspect.ismethod to only accept classmethod names
await bot.say("I do not recognize the color " + color)
return
await client.edit_role(ctx.message.server, role, colour=getattr(discord.Color, color)())
You would then call this with something along the lines of
!changecolor @NewMember dark gold
Upvotes: 2
Reputation: 2408
Change your last line to
await client.edit_role(server, role, colour=dictOfColors[NewColor])
You're assigning the number of the color you want from your dictionary to the colour
attribute instead of the value at that key which is the actual color.
Upvotes: 1