将字符串转换为discord.Color()对象,然后再转换回来的问题 [英] issues converting a string to a discord.Color() object, and then back again
本文介绍了将字符串转换为discord.Color()对象,然后再转换回来的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为Discorde开发的机器人编译关于一个人的角色的信息,然后以几个嵌入的形式发送回一个角色工作表。
我已经停用了很多功能,所以现在我正在开发FLAIR。我想做的一件事是让人们改变嵌入物一侧的小条纹的颜色,结果证明这比我想象的要困难得多。
老实说,我已经忘记了我尝试过的所有事情,到目前为止,这已经是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()对象,
将对象转换为字符串,
将字符串保存在COLOR配置变量中,
将字符串转换回discord.Color()对象,
返回嵌入了正确颜色的预览,
打印十六进制代码
##character setcol 00fe00
:
将00fe00转换为discord.color()对象,
将对象转换为字符串,
将字符串保存在COLOR配置变量中,
返回错误TypeError: Expected int parameter, received str instead
,
函数终止
推荐答案
###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。这很痛苦,但嘿,它做了我想让它做的事情!
这篇关于将字符串转换为discord.Color()对象,然后再转换回来的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文