簡體   English   中英

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.

我該如何解決? 謝謝。

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

或者 map 一些顏色名稱對應的值:

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])

我將提前#ffffff並假設您期望的輸入類似於 #ffffff ,如果我弄錯了,請糾正我。 為了將其轉換為 Discord 可以讀取的內容,我們可以使用以下方法。 我將假設該message是您等待他們響應的消息 object 。

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

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

您甚至可以將兩個 integer 轉換語句合並為一個!

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)
@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)

這在你的情況下有效嗎?

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM