So, I want to create an embed that, every time it's used, outputs a random colour as it's accent.
Here's my current code:
const Discord = require('discord.js');
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Some title')
.setURL('https://discord.js.org/')
.setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription('Some description here')
.setThumbnail('https://i.imgur.com/wSTFkRM.png')
.addFields(
{ name: 'Regular field title', value: 'Some value here' },
{ name: '\u200B', value: '\u200B' },
{ name: 'Inline field title', value: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.addField('Inline field title', 'Some value here', true)
.setImage('https://i.imgur.com/wSTFkRM.png')
.setTimestamp()
.setFooter('Some footer text here', 'https://i.imgur.com/wSTFkRM.png');
message.channel.send(exampleEmbed);
How could I make this happen simply and efficiently?
This is actually super simple to create and is actually already baked right into Discord.js v12! To achieve this in the most basic form, just use this little bit of code:
const Discord = require('discord.js');
const exampleEmbed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setTitle('Some title')
.setURL('https://discord.js.org/')
.setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription('Some description here')
.setThumbnail('https://i.imgur.com/wSTFkRM.png')
.addFields(
{ name: 'Regular field title', value: 'Some value here' },
{ name: '\u200B', value: '\u200B' },
{ name: 'Inline field title', value: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.addField('Inline field title', 'Some value here', true)
.setImage('https://i.imgur.com/wSTFkRM.png')
.setTimestamp()
.setFooter('Some footer text here', 'https://i.imgur.com/wSTFkRM.png');
message.channel.send(exampleEmbed);
If you look into that a bit, you'll notice that all I changed was the .setColor('#0099ff')
bit of the code. This is all you have to do! By changing the hex value to "RANDOM" you're letting D.js know that you want it to create random colours for you. Please do note that this actually works kinda weirdly in the fact that it'll actually just rotate the colours through a "rainbow" pattern. So, if you run enough commands or the same command a bunch of times, you'll notice that the colours will repeat eventually.
If you wanted a more complicated option
This little bit of code here will generate random RGB values by using a RNG.
const randomBetween = (min, max) => Math.floor(Math.random()*(max-min+1)+min);
const color = [
randomBetween(0, 255),
randomBetween(0, 255),
randomBetween(0, 255),
];
Using this function, you could do:
const Discord = require('discord.js');
const randomBetween = (min, max) => Math.floor(Math.random()*(max-min+1)+min);
const color = [
randomBetween(0, 255),
randomBetween(0, 255),
randomBetween(0, 255),
];
const exampleEmbed = new Discord.MessageEmbed()
.setColor(`${color[0]}, ${color[1]}, ${color[2]}`)
.setTitle('Some title')
.setURL('https://discord.js.org/')
.setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription('Some description here')
.setThumbnail('https://i.imgur.com/wSTFkRM.png')
.addFields(
{ name: 'Regular field title', value: 'Some value here' },
{ name: '\u200B', value: '\u200B' },
{ name: 'Inline field title', value: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.addField('Inline field title', 'Some value here', true)
.setImage('https://i.imgur.com/wSTFkRM.png')
.setTimestamp()
.setFooter('Some footer text here', 'https://i.imgur.com/wSTFkRM.png');
message.channel.send(exampleEmbed);
This will result in any value between 0 and 255 for every part of the RGB values, making your embed accent colour 100% random at all times.
This little bit of code was taken from: Olian04's response on this question! I felt as if this wasn't well documented anywhere and decided to put it all here!