How can I set a DiscordColor in DiscordSharpPlus to a random RGB color?

Viewed 39

I have a Discord bot and I'm trying to make an embed with a random RGB color. When I try to use this command in discord, it simply returns nothing.

My code for this is:

DiscordEmbedBuilder Embed = new DiscordEmbedBuilder
{
    Color = new DiscordColor(Rand.Next(0, 256), Rand.Next(0, 256), Rand.Next(0, 256))
}; 
1 Answers

I suspect you are trying to make use of this constructor which takes in three byte parameters. However, when I tried your code in Visual Studio, it appears to try and use the constructor that makes uses of floats instead, and I get an exception:

float overload

Switching to this code, which explicitly casts the values to byte works:

Random rand = new Random();
DiscordEmbedBuilder Embed = new DiscordEmbedBuilder
{
    Color = new DiscordColor((byte)rand.Next(0, 255), (byte)rand.Next(0, 255), (byte)rand.Next(0, 255))
};

color!

Also, you probably want rand.Next(0, 255) since byte only goes up to 255.

Related