python - 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.
我该如何解决?谢谢。
解决方案
您需要将用户输入转换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])
推荐阅读
- c - 试图将指针复制到数组中
- javascript - 为什么不能改变文本行的颜色?将纯文本更改为 HTML
- ios - 没有在窗口上调用 traitCollectionDidChange
- python - 如何在python中将多列表转换为字典
- c++ - Windows 上的英特尔 C++ 编译器灾难性错误:无法打开源文件“bits/unique_ptr.h”
- sql - 如何选择具有相同列条件的多行
- python - 加快从 Golang 的 exec packaqe 访问 python 程序
- python - Pandas:在 Pandas 数据框中查找连续索引的起始值
- javascript - 如何以正确和高效的方式避免在 React 中改变本地状态
- python - GCP:无法连接到云 TPU