将字符串转换为discord.Color()对象,然后再次返回

时间:2018-12-26 02:11:17

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

我正在研究的不和谐机器人将有关人物角色的信息进行编译,然后以嵌入的形式发送回人物表。

我的功能很多,所以现在我要精打细算。我想做的一件事是让人们改变嵌入一侧的小条纹的颜色,事实证明这比我想象的要困难得多。

我已经失去了我试图说实话的所有内容,到目前为止这是一个5个小时的问题,而且我仍然在这里和那里尝试随机调整。

    ###allows the user to set the stripe color of their character sheet embed.
@character.command(name="setcol")
async def color_set(self, ctx, *, color:str):
    if "#" in color:
        color = color.replace("#", "")
    member = ctx.message.author
    col = discord.Color(value=int(color, 16))
    await self.config.member(member).color.set(col.to_rgb())
    await asyncio.sleep(1)
    col = await self.config.member(member).color()
    pvw = discord.Embed(name="Preview", description="Preview", color=discord.Color.from_rgb(*col))
    await ctx.send(embed=pvw)
    await ctx.send("{}".format(col))

预期: ##character setcol 00fe00: 将00fe00转换为discord.color()对象, 将对象转换为字符串, 将字符串保存在颜色Config变量中, 将字符串转换回discord.Color()对象, 返回带有正确颜色的预览, 打印十六进制代码

实际: ##character setcol 00fe00: 将00fe00转换为discord.color()对象, 将对象转换为字符串, 将字符串保存在颜色Config变量中, 返回错误TypeError: Expected int parameter, received str instead, 函数终止

1 个答案:

答案 0 :(得分:0)

###allows the user to set the stripe color of their character sheet embed.
@character.command(name="setcol")
async def color_set(self, ctx, *, color:str):
    if "#" in color:
        color = color.replace("#", "")
    member = ctx.message.author
    col = discord.Color(value=int(color, 16))
    await self.config.member(member).color.set(col.to_rgb())
    await asyncio.sleep(1)
    col = await self.config.member(member).color()
    pvw = discord.Embed(name="Preview", description="Preview", color=discord.Color.from_rgb(*col))
    await ctx.send(embed=pvw)
    await ctx.send("{}".format(col))

不知道为什么我不这样做而不是更新原始内容,但是,嘿,随便什么。是的。那就是固定的代码。基本上必须采用十六进制,将#从中删除,将其转换为RGB,将RGB保存为元组,并在调用该元组时将其转换回RGB。这很痛苦,但是,嘿,它确实满足了我的要求!

相关问题