Changing color roles discord

萝らか妹 提交于 2021-01-29 09:42:53

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/52782098/changing-color-roles-discord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!