Reputation: 138
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.
Upvotes: 2
Views: 13491
Reputation: 1
try this:
embed=discord.Embed(title='Hey', description="How are you?", color=hex(value))
Upvotes: 0
Reputation: 1318
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)
Upvotes: 2
Reputation: 101
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])
Upvotes: 4
Reputation: 2415
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)
Upvotes: 1
Reputation: 2415
@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)
Does this work in your situation?
Upvotes: 0