Skip to content Skip to sidebar Skip to footer

Discord.py: Using Variable As Discord Embed Color

so I'm trying to make a command for my discord bot that is an embed builder. I want the user of the command to be able to input a hex value for the color of the embed. Here is what

Solution 1:

You need to convert the user input in message.content to a RGB color value.

E.g. for green, what Embed expects would look like this:

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

So you could either let the users pass the color values directly:

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

Or map some color names to the corresponding values:

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

Solution 2:

I'm going to go ahead and assume that the input you are expecting is something along the lines of #ffffff, and please do correct me if I am mistaken. In order to convert this into something that Discord can read, we can use the following method. I'm going to me assuming that message is the message object that you wait for them to respond with.

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

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

You could even merge the two integer conversion statements into one!

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

Solution 3:

questions = ["What should be the name of the embed?", 
            "What should be the desc",
            "What is the colour of the embed?"]

answers = []

defcheck(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!')
        returnelse:
        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)

Solution 4:

@client.command()asyncdefembed(ctx, content, colour):

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

Does this work in your situation?

Solution 5:

try this:

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

Post a Comment for "Discord.py: Using Variable As Discord Embed Color"