Discord.py:使用变量作为 Discord 嵌入颜色

Discord.py: Using Variable As Discord Embed color

所以我正在尝试为我的 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.

我该如何解决这个问题?谢谢

@client.command()
async def embed(ctx, content, colour):

embed=discord.Embed(title='Embed', description=f"{content}", color=colour)
await output.edit(content=None, embed=embed)

这对你的情况有效吗?

您需要将 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])

试试这个:

embed=discord.Embed(title='Hey', description="How are you?", color=hex(value))

我将继续并假设您期望的输入与 #ffffff 类似,如果我弄错了请纠正我。为了将其转换成 Discord 可以读取的内容,我们可以使用以下方法。我假设 message 是您等待他们响应的消息对象。

sixteenIntegerHex = int(message.content.replace("#", ""), 16)
readableHex = int(hex(sixteenIntegerHex), 0)

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

您甚至可以将两个整数转换语句合并为一个!

readableHex = int(hex(int(message.content.replace("#", ""), 16)), 0)
questions = ["What should be the name of the embed?", 
            "What should be the desc",
            "What is the colour of the embed?"]

answers = []

def check(m):
    return m.author == ctx.author and m.channel == ctx.channel 

for i in questions:
    await ctx.send(i)

    try:
        msg = await client.wait_for('message', timeout=15.0, check=check)
    except asyncio.TimeoutError:
        await ctx.send('You didn\'t answer in time, please be quicker next time!')
        return
    else:
        answers.append(msg.content)


title = answers[1]
desc = answers[2]
colour = answers[3]


embed = discord.Embed(title = f"{title}", description = f"{desc}", color = colour)

embed.set_footer(text = f"My embed")

await channel.send(embed = embed)