Home > Software design >  How can I set a DiscordColor in DiscordSharpPlus to a random RGB color?
How can I set a DiscordColor in DiscordSharpPlus to a random RGB color?

Time:08-05

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))
}; 

CodePudding user response:

I suspect you are trying to make use of 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