如何使用嵌入颜色输出配置

2024-04-24 14:03:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试根据config.json值生成嵌入颜色

with open('config.json') as f:
    config = json.load(f)

a_color = config.get("hexcolor") #Config Text {"hexcolor": "#FFFFF"}
s_color = a_color.replace("#", "0x")

@bot.command()
async def example(ctx):
    await ctx.message.delete()
    embed = discord.Embed(title="Title", description="Some description Text", color=discord.Colour(s_color))
    await ctx.send(embed=embed)

但我的输出是:

embed = discord.Embed(title="Title", description="Some description Text", colour=discord.Colo
ur(s_color))
  File "C:\Users\public\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\colour.py
", line 63, in __init__
    raise TypeError('Expected int parameter, received %s instead.' % value.__class__.__name__)      
TypeError: Expected int parameter, received str instead.

为什么?


Tags: textconfigjsontitlesomeembeddescriptionawait
1条回答
网友
1楼 · 发布于 2024-04-24 14:03:22

如果将十六进制颜色像这样放在json文件中会更容易:

{"hexcolor": "0xFFFFFF"}

要将字符串转换为十六进制整数:

>>> int('0xFFFFFF', 16)
16777215

您只需将其传递给colourkwarg

您的代码应该如下所示:

colour = int(config.get('hexcolor'), 16) # {"hexcolor": "0xFFFFFF"}

@bot.command()
async def example(ctx):
    embed = discord.Embed(title='whatever', colour=colour)
    await ctx.send(embed=embed)

相关问题 更多 >