更改用户的角色颜色以在discord.py中选择十六进制值

时间:2020-02-24 21:30:29

标签: python discord.py discord.py-rewrite

async def vanity(ctx, rolename=None, hexcolour=None):
    savepath = "--"
    userFile = os.path.join(savepath, str(ctx.author.id) + ".txt")
    server = client.get_guild(--)
    if os.path.exists(userFile):
        await ctx.send("No")
    if rolename == None:
        await ctx.send("Please include a name")
    elif hexcolour == None:
        await ctx.send("Please include a hexadecimal colour value")
    elif os.path.exists(userFile):
        vanityFile = open(userFile, "a+")
        vanityFile = vanityFile.read()
        await ctx.send(vanityFile)
        vanityrole = server.get_role(int(vanityFile))
        vanityrole = vanityrole.edit(name = str(rolename), colour = int("0x" + hexcolour, 16))
    else:
        vanityFile = open(userFile, "a+")
        vanityrole = await server.create_role(name = str(rolename), colour = int("0x" + hexcolour, 16))
        vanityFile = vanityFile.write(str(vanityrole.id))
        await client.add_roles(ctx.author, vanityrole)

这是我的代码(出于隐私原因省略了一些内容),当我运行它时出现错误

 vanityrole = await server.create_role(name = str(rolename), colour = int("0x" + hexcolour, 16))
  File "/home/--/.local/lib/python3.7/site-packages/discord/guild.py", line 1589, in create_role
    fields['color'] = colour.value
AttributeError: 'int' object has no attribute 'value'

我的代码供某人在服务器上编辑/创建其虚荣角色。目前,我在努力获取用户在命令中输入的颜色并使用十六进制将角色设置为该颜色。

1 个答案:

答案 0 :(得分:0)

代替:

colour = int("0x" + hexcolour, 16)

这应该有效:

colour = discord.Colour(int(f"0x{hexcolour}, 16))

尽管我是在提出问题 11 个月后才回答的。我在这里为将来可能遇到此问题并像我一样需要帮助的任何人回答。

希望这对遇到此问题的任何人都有帮助。