更改颜色角色不和谐 [英] Changing color roles discord

查看:54
本文介绍了更改颜色角色不和谐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想指出我是python的初学者.

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)

但是当我尝试: .changecolor 5 收到此错误:

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?

推荐答案

将最后一行更改为

await client.edit_role(server, role, colour=dictOfColors[NewColor])

您要将字典中所需的颜色编号分配给 colour 属性,而不是该键上的实际颜色值.

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.

这篇关于更改颜色角色不和谐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆