首页 > 解决方案 > Discord.py:使用变量作为 Discord 嵌入颜色

问题描述

所以我正在尝试为我的不和谐机器人发出一个命令,它是一个嵌入构建器。我希望命令的用户能够输入嵌入颜色的十六进制值。这是我尝试过的:

value = message.content

embed=discord.Embed(title='Hey', description="How are you?", color=value)
await output.edit(content=None, embed=embed)

但是,当我这样做时,我得到了错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: Expected discord.Colour, int, or Embed.Empty but received str instead.

我该如何解决?谢谢。

标签: pythondiscorddiscord.pyembeddiscord.py-rewrite

解决方案


您需要将用户输入转换message.content为 RGB 颜色值。

例如对于绿色,Embed期望看起来像这样:

discord.Embed(title="Hey", description="How are you?", color=0x00ff00)

因此,您可以让用户直接传递颜色值:

color = int(message.content, 16)  # content should look like this: "0x00ff00"
discord.Embed(title="Hey", description="How are you?", color=color)

或者将一些颜色名称映射到相应的值:

color_name = message.content  # content should look like this: "green"

colors = {"green": 0x00ff00, "red": 0xff0000, "blue": 0x0000ff}

discord.Embed(title="Hey", description="How are you?", color=colors[color_name])

推荐阅读