admin 管理员组文章数量: 1086019
I want my Discord bot's embeds to appear in red or green (random between those two) so I want to create something that will make this possible.
I have the following code:
const randomcolors = ['#008000', '#E50000']
const randomizer = Math.floor((Math.random() * foreigncolors.length));
Then I have this, but it always sets the color to green.
const embed = new Discord.MessageEmbed()
.setColor(foreigncolors[colorss])
I want my Discord bot's embeds to appear in red or green (random between those two) so I want to create something that will make this possible.
I have the following code:
const randomcolors = ['#008000', '#E50000']
const randomizer = Math.floor((Math.random() * foreigncolors.length));
Then I have this, but it always sets the color to green.
const embed = new Discord.MessageEmbed()
.setColor(foreigncolors[colorss])
Share
Improve this question
edited Jan 17, 2022 at 8:08
Zsolt Meszaros
23.2k19 gold badges58 silver badges69 bronze badges
asked Oct 22, 2020 at 17:11
FOXTROXFOXTROX
211 silver badge2 bronze badges
0
3 Answers
Reset to default 4There is no need to write something plex all by yourself. Instead, you can use:
Embed.setColor('RANDOM')
This is an inbuilt feature of the discord.js
library.
If you want to pick a colour from an array of values, you can create a helper function and use that:
function random(colors) {
return colors[Math.floor(Math.random() * colors.length)];
};
random(['#008000', '#E50000']);
// => "#E50000" or "#008000"
In your example:
const embed = new Discord
.MessageEmbed()
.setColor(random(['#008000', '#E50000']));
If you want to pick a totally random colour value, the .setColor()
method accepts specific colour strings. One of them is 'RANDOM`, which sets the colour to a random value:
const embed = new Discord
.MessageEmbed()
.setColor('RANDOM');
From the docs:
The .setColor() method accepts an integer, HEX color string, an array of RGB values or specific color strings.
This means that we simply can generate an array of type [R, G, B]
where each R
, G
and B
are integer values between 0
and 255
.
const randomBetween = (min, max) => Math.floor(Math.random()*(max-min+1)+min);
const color = [
randomBetween(0, 255),
randomBetween(0, 255),
randomBetween(0, 255),
];
console.log(color);
document.body.style.background = `rgb(${color[0]}, ${color[1]}, ${color[2]})`;
本文标签: javascriptHow to make random colors for embeds DiscordjsStack Overflow
版权声明:本文标题:javascript - How to make random colors for embeds Discord.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744091017a2532035.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论