Discord.py: Using Variable As Discord Embed color

10,812

Solution 1

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 2

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 3

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)
Share:
10,812

Related videos on Youtube

Dom
Author by

Dom

Updated on June 04, 2022

Comments

  • Dom
    Dom almost 2 years

    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 I've tried:

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

    However when I do this I get the error:

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

    How do I fix this? Thanks.

    • StarbuckBarista
      StarbuckBarista over 3 years
      I think that this is an amazing question to be asked! I had a very similar issue when I was beginning to write Discord bots, and I spent hours scouring documentation trying to find an answer!
  • Cohen
    Cohen over 3 years
    At least an example
  • Dom
    Dom over 3 years
    No, in my situation I'm accepting different responses for different parts the embed until the bot gets to color. Here you can see where I'm waiting for the input: message = await self.bot.wait_for("message", check=check)
  • Dom
    Dom over 3 years
    Ahh, I understand. This worked, thank you (:
  • LiBa01
    LiBa01 over 3 years
    I'm glad I could help. :)
  • Dom
    Dom over 3 years
    I'll try this out, may be a bit difficult to implement it the way I'm trying but hey, worth a shot! Thanks for your contribution (:
  • Dom
    Dom over 3 years
    Okay, so, I tried implementing this, but I get the error discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: int() can't convert non-string with explicit base
  • StarbuckBarista
    StarbuckBarista over 3 years
    I made a simple error in my code, and I am so sorry! Everything should now be working properly.
  • bhucho
    bhucho over 3 years
    we appreciate your answer but please provide a bit explnation as well to your answer -- FROM REVIEW